package com.jihe.punchnet.helper

import com.jihe.punchnet.data.RouteItem
import com.jihe.punchnet.sdlan.network.maskDigitToInt

sealed class Screen(val route: String) {
    object ScreenRoutes: Screen("routes")
    object ScreenMain: Screen("main")
}

fun IntToIPString(ip: Int): String {
    val one = ((ip ushr 24) and 0x000000ff)
    val two = ((ip ushr 16) and 0x000000ff)
    val three = ((ip ushr 8) and 0x000000ff)
    val four = ((ip) and 0x000000ff)
    return "${one}.${two}.${three}.${four}"
}

fun IPStringToInt(ipStr: String): Int? {
    val digits = ipStr.split('.').filter{
        val single = try {
            it.toInt()
        } catch(e: Exception) {
            return@filter false
        }
        single in 0..255
    }
    if (digits.size != 4) {
        return null
    }
    val result: Int
    try {
        result = (digits[0].toInt() shl 24) +
                (digits[1].toInt() shl 16) +
                (digits[2].toInt() shl 8) +
                (digits[3].toInt())
    } catch(e: Exception) {
        return null
    }

    return result
}

fun parseCIDRAndGW(cidr: String, gw: String): RouteItem? {
    val cidrSplit = cidr.split('/')
    var ip = 0
    var digit = 0

    if (cidrSplit.size == 1) {
        digit = 32
    } else if (cidrSplit.size == 2) {
        try {
            val k = cidrSplit[1].toInt()
            if (k in 1..32) {
                digit = k
            } else {
                return null
            }
        } catch (e: Exception) {
            return null
        }
    } else {
        return null
    }

    val ipvalue = IPStringToInt(cidrSplit[0])
    val gateway = IPStringToInt(gw)
    if (ipvalue == null) {
        return null
    }
    if (gateway == null) {
        return null
    }
    val mask = maskDigitToInt(digit)!!
    return RouteItem(
        net_ip = ipvalue,
        gateway = gateway,
        mask_ip = mask,
    )
}