package com.example.modifier.utils

import org.apache.commons.lang3.RandomStringUtils
import java.util.Locale
import kotlin.math.floor

fun genIMEI(): String {
    val str = ("3568" + RandomStringUtils.randomNumeric(11)).map { it.toString().toInt() }
        .toIntArray()
    var sum = 0
    var t: Int
    val len = 15
    var imei = ""

    var pos = 0
    while (pos < len - 1) {
        if ((pos % 2) != 0) {
            t = str[pos] * 2
            if (t > 9) {
                t -= 9
            }
            sum += t
        } else {
            sum += str[pos]
        }
        pos++
    }

    val finalDigit = (10 - (sum % 10)) % 10
    str[len - 1] = finalDigit

    for (d in str) {
        imei += d.toString()
    }

    return imei
}

fun genSerialNo(): String {
    return RandomStringUtils.randomAlphanumeric(14).uppercase()
}

fun genMacAddress(): String {
    val macAddress = List(4) {
        "%02x".format((0..255).random())
    }.joinToString(":")
    return "00:16:$macAddress"
}

fun generateIMEI1(): String {
    val str = intArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    var sum = 0
    var t: Int
    val len_offset: Int
    val len = 15
    var imei = ""

    val rbi = arrayOf(
        "01",
        "10",
        "30",
        "33",
        "35",
        "44",
        "45",
        "49",
        "50",
        "51",
        "52",
        "53",
        "54",
        "86",
        "91",
        "98",
        "99"
    )
    val arr = rbi[floor(Math.random() * rbi.size)
        .toInt()].split("".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
    str[0] = arr[0].toInt()
    str[1] = arr[1].toInt()
    var pos = 2

    while (pos < len - 1) {
        str[pos++] = (floor(Math.random() * 10) % 10).toInt()
    }

    len_offset = (len + 1) % 2
    pos = 0
    while (pos < len - 1) {
        if ((pos + len_offset) % 2 != 0) {
            t = str[pos] * 2
            if (t > 9) {
                t -= 9
            }
            sum += t
        } else {
            sum += str[pos]
        }
        pos++
    }

    val final_digit = (10 - (sum % 10)) % 10
    str[len - 1] = final_digit

    for (d in str) {
        imei += d.toString()
    }

    return imei
}

fun genICCID(mnc: String, areaCode: String): String {
    val prefix = String.format(Locale.US, "89%02d%s", areaCode.toInt(), mnc)
    return luhn(prefix + RandomStringUtils.randomNumeric(20 - prefix.length))
}

fun genIMSI(mccmnc: String): String {
    return luhn(mccmnc + RandomStringUtils.randomNumeric(15 - mccmnc.length))
}

fun luhn(number: String): String {
    if (!number.matches(Regex("[0-9]+"))) {
        throw IllegalArgumentException("Invalid number: $number")
    }
    val checks = "0987654321".toCharArray()
    val chs = number.toCharArray()
    var count = 0

    var i = chs.size - 2
    var k = 1
    while (i >= 0) {
        // 偶位数字 * 2，奇位不乘
        val num = (chs[i].code - '0'.code) shl (k and 1)

        // 累加
        count += num % 10 + num / 10
        i--
        k++
    }

    chs[chs.size - 1] = checks[count % 10]
    return String(chs)
}