package com.zj365.health.weight

import android.content.Context
import android.text.InputFilter
import android.text.TextUtils
import android.view.View
import com.xty.base.dialog.BaseDialog
import com.xty.common.TimeSelect
import com.xty.common.util.CommonToastUtils
import com.zj365.health.databinding.DialogAddDataBinding

class AddDataDialog(
    context: Context,
    var dataType: String?,
    var confirm: (
        id: String,
        date: String,
        dataValue: String,
        tcStr: String,
        tgStr: String,
        hdlStr: String,
        ldlStr: String,
        height: String
    ) -> Unit,
    var cancel: (
        height: String
    ) -> Unit
) : BaseDialog(context) {
    var data: String = ""
    var id: String = ""
    val binding by lazy { DialogAddDataBinding.inflate(layoutInflater) }
    val timSelect by lazy {
        TimeSelect(context, true) {
            day = binding.tvTime.text.toString()
        }.apply {
            chooseMany = false
            showYear = true
            showMonth = true
            showDay = true
            showHour = true
            showMin = true
            showSec = false
        }
    }
    var day: String? = ""
    override fun setLayout() = binding.root
    override fun initView() {
        super.initView()

        dataType?.let {
//            changeMode(it.equals("2"))
            changeMode(it.toInt())
        }
        setDialogPadding(40, 0, 40, 0)

        val title = getDataTitleName()
        val hint = "请输入" + getDataName()
        binding.mcontent.text = title
        binding.etRemark.hint = hint
        //尿酸限制输入整形
        if (dataType == "4") {
            // 创建一个 InputFilter，用于阻止小数输入
            val filter = InputFilter { source, start, end, dest, dstart, dend ->
                // 检查用户输入的文本
                val input = dest.subSequence(0, dstart)
                    .toString() + source + dest.subSequence(dend, dest.length)
                    .toString()

                // 如果输入包含小数点，则不允许
                if (input.contains(".")) {
                    return@InputFilter ""
                }
                return@InputFilter null // 允许输入其他字符
            }
            binding.etRemark.filters = arrayOf(filter)

        }

//        binding.mTvConfirm.setOnClickListener {
//            val toString = binding.etRemark.text.toString()
//
//            confirm.invoke(id, toString, day!!)
//            dismiss()
//            clear()
//        }
        binding.llSelectItem.setOnClickListener {
            timSelect.selectTime(binding.tvTime, "yyyy-MM-dd HH:mm")
        }
        binding.mTvConfirm.setOnClickListener {
            dataType?.let {
                when (it) {
                    "2" -> {//血压
                        val toString = binding.ethddp.text.toString()
                        val toString1 = binding.etLow.text.toString()
                        if (toString.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (toString1.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (day.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请选择日期")
                            return@setOnClickListener
                        }
                        val result = "$toString,$toString1"
                        confirm.invoke(id, day!!, result, "", "", "", "", "")
                    }

                    "3" -> {//体重
                        val tvTizhong = binding.ethddp.text.toString()
                        val tvShengao = binding.etLow.text.toString()
                        if (tvTizhong.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (tvShengao.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (day.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请选择日期")
                            return@setOnClickListener
                        }
                        confirm.invoke(id, day!!, tvTizhong, "", "", "", "", tvShengao)
                    }

                    "5" -> {//血脂
                        val tcStr = binding.etRemark.text.toString()
                        val tgStr = binding.ethddp.text.toString()
                        val hdlStr = binding.etLow.text.toString()
                        val ldlStr = binding.etTc.text.toString()

                        if (tcStr.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (tgStr.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (hdlStr.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (ldlStr.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (day.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请选择日期")
                            return@setOnClickListener
                        }
                        confirm.invoke(id, day!!, "", tcStr, tgStr, hdlStr, ldlStr, "")
                    }

                    else -> {
                        val toString = binding.etRemark.text.toString()
                        val time = binding.tvTime.text.toString()

                        if (toString.isNullOrEmpty()) {
                            CommonToastUtils.showToast("请填写数据")
                            return@setOnClickListener
                        }
                        if (day.isNullOrEmpty() || time.isEmpty() || time == "请选择测量时间") {
                            CommonToastUtils.showToast("请选择日期")
                            return@setOnClickListener
                        }
                        confirm.invoke(id, day!!, toString, "", "", "", "", "")
                    }
                }

            }
            dismiss()
        }
        binding.mTvCancel.setOnClickListener {
            clear(dataType!!.toInt())
            binding.etLow.setText("")
            cancel.invoke(binding.etLow.text.toString())
            dismiss()
        }
    }

    private fun changeMode(type: Int) {
        when (type) {
            3 -> {//体重
                binding.hddp.visibility = View.VISIBLE
                binding.ethddp.visibility = View.VISIBLE
                binding.ethddpUnit.visibility = View.VISIBLE
                binding.ethddpLayout.visibility = View.VISIBLE
                binding.tvLow.visibility = View.VISIBLE
                binding.etLow.visibility = View.VISIBLE
                binding.etLowUnit.visibility = View.VISIBLE
                binding.etLowLayout.visibility = View.VISIBLE

                binding.etRemark.visibility = View.GONE
                binding.mcontent.visibility = View.GONE
                binding.tvTc.visibility = View.GONE
                binding.etTc.visibility = View.GONE

                binding.hddp.text = "体重数值"
                binding.ethddp.hint = "请输入体重"
                binding.tvLow.text = "身高数值"
                binding.etLow.hint = "请输入身高"
            }

            2 -> {//血压
                binding.etRemark.visibility = View.GONE
                binding.mcontent.visibility = View.GONE
                binding.tvTc.visibility = View.GONE
                binding.etTc.visibility = View.GONE
                binding.hddp.visibility = View.VISIBLE
                binding.ethddp.visibility = View.VISIBLE
                binding.tvLow.visibility = View.VISIBLE
                binding.etLow.visibility = View.VISIBLE
                binding.etLowLayout.visibility = View.VISIBLE
                binding.ethddpLayout.visibility = View.VISIBLE
                binding.ethddpUnit.visibility = View.GONE
                binding.etLowUnit.visibility = View.GONE
            }

            5 -> {//血脂
                binding.etRemark.visibility = View.VISIBLE
                binding.mcontent.visibility = View.VISIBLE
                binding.tvTc.visibility = View.VISIBLE
                binding.etTc.visibility = View.VISIBLE
                binding.hddp.visibility = View.VISIBLE
                binding.ethddp.visibility = View.VISIBLE
                binding.tvLow.visibility = View.VISIBLE
                binding.etLow.visibility = View.VISIBLE
                binding.etLowLayout.visibility = View.VISIBLE
                binding.ethddpLayout.visibility = View.VISIBLE
                binding.ethddpUnit.visibility = View.GONE
                binding.etLowUnit.visibility = View.GONE

                binding.hddp.text = "甘油三酯"
                binding.ethddp.hint = "请输入甘油三酯"
                binding.tvLow.text = "高密度脂蛋白"
                binding.etLow.hint = "请输入高密度脂蛋白"
            }

            else -> {
                binding.etRemark.visibility = View.VISIBLE
                binding.mcontent.visibility = View.VISIBLE
                binding.hddp.visibility = View.GONE
                binding.ethddp.visibility = View.GONE
                binding.tvLow.visibility = View.GONE
                binding.etLow.visibility = View.GONE
                binding.etLowLayout.visibility = View.GONE
                binding.ethddpLayout.visibility = View.GONE
                binding.ethddpUnit.visibility = View.GONE
                binding.etLowUnit.visibility = View.GONE
            }
        }
    }

    private fun getDataTitleName(): String {
        var showName = ""
        when (dataType?.toInt()) {
            1 -> {
                showName = "血糖数值"
            }

            2 -> {
                showName = "血压数值"
            }

            3 -> {
                showName = "体重数值"
            }

            4 -> {
                showName = "尿酸数值"
            }

            5 -> {
                showName = "总胆固醇"
            }
        }
        return showName
    }

    private fun getDataName(): String {
        var showName = ""
        when (dataType?.toInt()) {
            1 -> {
                showName = "血糖值"
            }

            2 -> {
                showName = "血压值"
            }

            3 -> {
                showName = "体重值"
            }

            4 -> {
                showName = "尿酸值"
            }

            5 -> {
                showName = "总胆固醇"
            }
        }
        return showName
    }

    fun showWithData(
        id: Int,
        mtime: String,
        data: String,
        tcStr: String,
        tgStr: String,
        hdlStr: String,
        ldlStr: String,
        height: String,
        dialogType: Int
    ) {
        this.data = data
        this.id = id.toString()
        this.day = mtime
        binding.mTitle.text = "修改数据"
        binding.tvTime.text = mtime
        when (dialogType) {
            3 -> {//体重
                binding.ethddp.setText(data)
                binding.etLow.setText(height)
            }

            5 -> {//血脂
                binding.etRemark.setText(tcStr)
                binding.ethddp.setText(tgStr)
                binding.etLow.setText(hdlStr)
                binding.etTc.setText(ldlStr)
            }

            else -> {
                data.let {
                    if (data.contains(",")) {
                        val split = data.split(",")
                        val h = split[0]
                        val l = split[1]
                        binding.ethddp.setText(h)
                        binding.etLow.setText(l)
                    } else {
                        binding.etRemark.setText(data)
                    }
                }
            }
        }
        super.show()
    }

    override fun show() {
        binding.mTitle.text = "添加数据"
        super.show()
    }

    fun clear(dialogType: Int, height: String = "") {
        id = ""
        day = ""
        when (dialogType) {
            2 -> {
                binding.ethddp.setText("")
                binding.etLow.setText("")
                binding.tvTime.text = "请选择测量时间"
            }

            3 -> {
                binding.ethddp.setText("")
                if (!TextUtils.isEmpty(height)) {
                    binding.etLow.setText(height)
                }
                binding.tvTime.text = "请选择测量时间"
            }

            5 -> {
                binding.etRemark.setText("")
                binding.ethddp.setText("")
                binding.etLow.setText("")
                binding.etTc.setText("")
                binding.tvTime.text = "请选择测量时间"
            }

            else -> {
                val hint = "请输入" + getDataName()
                binding.etRemark.hint = hint
                binding.etRemark.setText("")
                binding.tvTime.text = "请选择测量时间"
            }
        }
    }
}