package com.remax.visualnovel.utils

import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat
import java.util.regex.Matcher
import java.util.regex.Pattern

class EpalUtils {

    companion object {
        /**
         * 自动适配单位
         *
         * @param size
         * @return
         */
        fun formatNumberAutoSize(size: Long, pattern: String = "####.0"): String {
            if (size < 1000) {
                return size.toString()
            }
            val format = DecimalFormat(pattern)
            format.groupingSize = 0
            format.roundingMode = RoundingMode.FLOOR
            format.maximumFractionDigits = 1
            if (size < 1000000) {
                val bsize = size.toDouble()
                val ksize = bsize / 1000
                var str = format.format(ksize)
                str += "K"
                return str
            } else if (size < 1000000000) {
                val bsize = size.toDouble()
                val ksize = bsize / 1000000
                var str = format.format(ksize)
                str += "M"
                return str
            } else {
                val bsize = size.toDouble()
                val ksize = bsize / 1000000000
                var str = format.format(ksize)
                str += "B"
                return str
            }
        }

        fun parseEditBuff(amount: String): Long {
            runCatching {
                amount.replace(",", ".").toDouble()
            }.onSuccess {
                val b1 = BigDecimal(it.toString())
                val b2 = BigDecimal("100.0")
                return (b1.multiply(b2).toDouble()).toLong()
            }.onFailure {
                return 0
            }
            return 0
        }

        /**
         * 自动适配单位
         *
         * @param size
         * @return
         */
        fun formatNumberAutoSize(size: Int?): String {
            return formatNumberAutoSize((size?:0).toLong())
        }

        fun getRandom(): String {
            return System.currentTimeMillis().toString() + List(3) { ('a'..'z').random() }.joinToString("")
        }

        /**
         * 去掉字符串中的前后空格符、换行符
         */
        fun replaceBlank(str: String?): String {
            var dest = ""
            if (!str.isNullOrBlank()) {
                dest = str.trim()
                val p: Pattern = Pattern.compile("\t|\r|\n")
                val m: Matcher = p.matcher(dest)
                dest = m.replaceAll("")
            }
            return dest
        }

        /**
         * 去掉字符串左右换行
         *
         * @param str 原字符串
         * @return 转换后的字符串
         */
        fun trimN(str: String): String {
            var len = str.length
            var st = 0
            val text = str.toCharArray()
            while (st < len && text[st] <= '\r') {
                st++
            }
            while (st < len && text[len - 1] <= '\r') {
                len--
            }
            return if (st > 0 || len < str.length) str.substring(st, len) else str
        }

        fun viewToBitmap(view: View): Bitmap? {
            var width = view.width
            var height = view.height
            view.clearFocus()
            if (width <= 0 || height <= 0) {
                val specSize = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
                view.measure(specSize, specSize)
                width = view.measuredWidth
                height = view.measuredHeight
            }

            if (width <= 0 || height <= 0) {
                return null
            }

            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bitmap)
            if (view.right <= 0 || view.bottom <= 0) {
                view.layout(0, 0, width, height)
                view.draw(canvas)
            } else {
                view.layout(view.left, view.top, view.right, view.bottom)
                view.draw(canvas)
            }
            return bitmap
        }
    }
}