package com.xty.common.util

import android.text.SpannableStringBuilder
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

object ChineseNumberAndNumberUtils {


    private   val NUMBER_CONSTRAINT = arrayOf('9', '8', '7', '6', '5', '4', '3', '2', '1', '0', '.')

    private  val DECIMAL_UNIT = arrayOf("角", "分", "厘", "毫")

    private  val  CHINESE_NUMBER = arrayOf("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")

    private  val  INTEGER_UNIT = arrayOf("", "万", "亿", "兆")

    private  val INTEGER_UNIT1 = arrayOf("", "拾", "佰", "仟")

    /*suspend fun numberToChineseNumber(number:String):String{
        val quantity = NUMBER_CONSTRAINT.size -1

        var newNum = ""
        number.forEach {unit ->
            for (i in 0 until NUMBER_CONSTRAINT.size){
                if(unit == NUMBER_CONSTRAINT[i]) break

                if (i == quantity) return "请输入有效数字"

            }
        }

        var quantity1 = 0
        number.forEachIndexed {index,unit->
            if('.' == unit && (index == 0 || index == number.length -1)) return "请输入有效数字"

            if('.' == unit){
                ++quantity1
                if (quantity1 == 2) return "请输入有效数字"
            }
        }

        if (quantity1 == 0){
            newNum = headNumberIsZero(number)
            if(newNum.length > 16) return "请输入有效数字"
        }else{
            var split = number.split("[.]").toMutableList()
            split[0] = headNumberIsZero(split[0])
            if(split[0].length >  16) return "请输入有效数字"

            if(split[1].length > 4){
                for ( i in 0 until split[1].length ){
                    if('0' == split[1].elementAt(i)) return "请输入有效数字"
                }
                split[1] = split[1].substring(0,4)
            }

            for (i in split[1].length until 0){
                if('0' == split[1].elementAt(i)) split[1] = split[1].substring(0,i)
                else break
            }
            if(split[1].isEmpty()) newNum = split[0]
            else newNum = split[0] + "."+split[1]

        }

        var result = arrayOf("","")
        var spliteStr = number.split("[.]")

        if(spliteStr.size == 2){
            var c = 0
            spliteStr[1].forEachIndexed { index, unit ->
                if(c == 0 && ('0'.toChar() == unit)){
                    result[1] += CHINESE_NUMBER[0]
                    c = 1
                }else if('0' != unit){
                    result[1] += (CHINESE_NUMBER[unit.digitToInt()] + DECIMAL_UNIT[index])
                    c= 0
                }
            }
        }

    }*/

    fun intToChinese(number:Int):String{
        var newNum = number.toString()
        var spannable = SpannableStringBuilder()
        var length = newNum.length
        if(length > 1){
            spannable.append(getChinese((number / Math.pow(10.0,(length-1).toDouble())).toInt(),length))
                .append(getUnitChinese(length))
            if(number %  Math.pow(10.0,(length-1).toDouble()).toInt() == 0){
                return spannable.toString()
            }
        }

        if (length == 1){
            spannable.append(getChinese(number,1))
        }

        if(length == 2){
            spannable.append(getChinese(number % 10,0))
        }
        if (length == 3){
            if (number % 100 < 10){
                spannable.append("零").append(getChinese(number % 100,3))
            }else{
                spannable.append(getChinese(number % 100,3))
                    .append("十")
                    .append(getChinese(number % 10,0))
            }
        }

        if(length == 4){
            if (number % 1000 < 10){
                spannable.append("零").append(getChinese(number % 1000,3))
            }else if(number % 1000 < 100){
                spannable.append("零").append(getChinese(number % 1000,3))
                    .append("十")
                    .append(getChinese(number % 10,0))
            }else{
                spannable.append(intToChinese(number % 1000))

            }
        }

        return spannable.toString()
    }

    fun getChinese(key:Int,length:Int):String{
        when(key){
            1 ->{
                if (length == 2){
                    return ""
                }
                return "一"
            }

            2->{
                return "二"
            }
            3->{
                return "三"
            }
            4->{
                return "四"
            }
            5->{
                return "五"
            }
            6->{
                return "六"
            }
            7->{
                return "七"
            }
            8->{
                return "八"
            }
            9->{
                return "九"
            }
            0->{
                if(length == 1){
                    return  "零"
                }
                return ""
            }
        }
        return ""
    }

    fun getUnitChinese(length:Int):String{
        when(length){
            2->{
                return "十"
            }

            3->{
                return "百"
            }

            4->{
                return "千"
            }
        }
        return ""
    }

    fun headNumberIsZero(number:String):String{
        try {
            if ('0' == number.elementAt(0)) return headNumberIsZero(number.substring(1))
            else return number
        }catch (e:IndexOutOfBoundsException){
            return "0"
        }
    }

    fun headNumberIsZero1(number:String):String{
        if(number.isEmpty()) return "0"
        if ('0' == number.elementAt(0)) return headNumberIsZero(number.substring(1))
        else return number
    }

    fun headNumberIsZero2(number:String):String{
        var  str:String = ""
        if('0' != number.elementAt(0)) return number
        else{
            number.forEachIndexed { index, unit ->
                if ('0' != unit){
                    str = number.substring(index)
                    return@forEachIndexed
                }else if (index == number.length -1){
                    str = "0"
                }
            }

            return str
        }
    }

}