package com.zj365.health.weight

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import com.xty.base.dialog.BaseDialog
import com.xty.common.FileUtils
import com.xty.common.util.CommonToastUtils
import com.zj365.health.databinding.DialogSignBinding
import java.io.File
import java.io.IOException

class SignDialog(context: Context, var confirm: (paht: String) -> Unit) : BaseDialog(context) {
    val binding by lazy { DialogSignBinding.inflate(layoutInflater) }

    override fun setLayout() = binding.root

    override fun initView() {
        super.initView()
        binding.mTvSub.setOnClickListener {
            val path =
                FileUtils.getCacheFile(context) + File.separator + System.currentTimeMillis() + ".jpg"
            try {
                if (!clearBlank(getSingView(), 0)) {
                    CommonToastUtils.showToast("请签名")
                    return@setOnClickListener
                }

                binding.mViewSign.save(path, true, 0)
                binding.mViewSign.clear()
                confirm.invoke(path)
            } catch (e: IOException) {
                e.printStackTrace()
            }
            binding.mViewSign.clear()
            dismiss()
        }
        binding.mTvCancel.setOnClickListener {
            binding.mViewSign.clear()
        }
    }

    private fun clearBlank(bmp: Bitmap, blank: Int): Boolean {
        var blank = blank
        val height = bmp.height
        val width = bmp.width
        var top = 0
        var left = 0
        var right = 0
        var bottom = 0
        var pixs = IntArray(width)
        var isStop: Boolean
        //扫描上边距不等于背景颜色的第一个点
        for (i in 0 until height) {
            bmp.getPixels(pixs, 0, width, 0, i, width, 1)
            isStop = false
            for (pix in pixs) {
                if (pix != Color.WHITE) {
                    top = i
                    isStop = true
                    break
                }
            }
            if (isStop) {
                break
            }
        }
        //扫描下边距不等于背景颜色的第一个点
        for (i in height - 1 downTo 0) {
            bmp.getPixels(pixs, 0, width, 0, i, width, 1)
            isStop = false
            for (pix in pixs) {
                if (pix != Color.WHITE) {
                    bottom = i
                    isStop = true
                    break
                }
            }
            if (isStop) {
                break
            }
        }
        pixs = IntArray(height)
        //扫描左边距不等于背景颜色的第一个点
        for (x in 0 until width) {
            bmp.getPixels(pixs, 0, 1, x, 0, 1, height)
            isStop = false
            for (pix in pixs) {
                if (pix != Color.WHITE) {
                    left = x
                    isStop = true
                    break
                }
            }
            if (isStop) {
                break
            }
        }
        //扫描右边距不等于背景颜色的第一个点
        for (x in width - 1 downTo 1) {
            bmp.getPixels(pixs, 0, 1, x, 0, 1, height)
            isStop = false
            for (pix in pixs) {
                if (pix != Color.WHITE) {
                    right = x
                    isStop = true
                    break
                }
            }
            if (isStop) {
                break
            }
        }
        if (blank < 0) {
            blank = 0
        }
        //计算加上保留空白距离之后的图像大小
        left = if (left - blank > 0) left - blank else 0
        top = if (top - blank > 0) top - blank else 0
        right = if (right + blank > width - 1) width - 1 else right + blank
        bottom = if (bottom + blank > height - 1) height - 1 else bottom + blank
        if (left == 0 && top == 0 && right == 0 && bottom == 0) {
            return false
        }
        return true
    }

    private fun getSingView(): Bitmap {
        val clazz = binding.mViewSign.javaClass
        val filed = clazz.getDeclaredField("cacheBitmap")
        filed.isAccessible = true
        return filed.get(binding.mViewSign) as Bitmap
    }
}