package com.zj365.mime.act

import android.text.TextUtils
import android.view.View
import androidx.core.view.isVisible
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.base.act.BaseVmAct
import com.xty.common.TimeSelect
import com.xty.common.arouter.ARouterUrl
import com.xty.common.event.IntegralEvent
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.CommonUtils
import com.zj365.mime.databinding.ActAddIntegralBinding
import com.zj365.mime.vm.ScoreDetailVm
import com.xty.network.model.MyIntegralBean
import org.greenrobot.eventbus.EventBus

/**
 * 添加积分页面
 */
@Route(path = ARouterUrl.ADD_INTEGRAL)
class AddIntegralAct : BaseVmAct<ScoreDetailVm>() {

    private val binding by lazy { ActAddIntegralBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root

    private var resultData: MyIntegralBean? = null

    private val timSelect by lazy {
        TimeSelect(this) {

        }.apply {
            chooseMany = false
            showYear = true
            showMonth = true
            showDay = true
            showHour = true
            showMin = true
            showSec = false
        }
    }

    override fun initData() {
        super.initData()
        val result = intent.getStringExtra("data")
        result.let {
            resultData = GsonUtils.fromJson(it, MyIntegralBean::class.java)
            if (resultData != null) {
                when (resultData!!.type) {
                    10 -> {//血糖
                        binding.title.mTvTitle.text = "添加血糖"
                        binding.llOne.isVisible = true
                        binding.tvOneName.text = "血糖数值"
                        binding.etOneValue.hint = "请输入血糖数值"
                        binding.oneLine.isVisible = true
                        binding.llTwo.isVisible = false
                        binding.twoLine.isVisible = false
                        binding.llThree.isVisible = false
                        binding.threeLine.isVisible = false
                        binding.llFour.isVisible = false
                        binding.fourLine.isVisible = false
                    }

                    14 -> {//血脂
                        binding.title.mTvTitle.text = "添加血脂"
                        binding.llOne.isVisible = true
                        binding.tvOneName.text = "总胆固醇"
                        binding.etOneValue.hint = "请输入总胆固醇"
                        binding.oneLine.isVisible = true
                        binding.llTwo.isVisible = true
                        binding.tvTwoName.text = "甘油三酯"
                        binding.etTwoValue.hint = "请输入甘油三酯"
                        binding.twoLine.isVisible = true
                        binding.llThree.isVisible = true
                        binding.tvThreeName.text = "高密度脂蛋白"
                        binding.etThreeValue.hint = "请输入高密度脂蛋白"
                        binding.threeLine.isVisible = true
                        binding.llFour.isVisible = true
                        binding.tvFourName.text = "低密度脂蛋白"
                        binding.etFourValue.hint = "请输入低密度脂蛋白"
                        binding.fourLine.isVisible = true
                    }

                    13 -> {//尿酸
                        binding.title.mTvTitle.text = "添加尿酸"
                        binding.llOne.isVisible = true
                        binding.tvOneName.text = "尿酸数值"
                        binding.etOneValue.hint = "请输入尿酸数值"
                        binding.oneLine.isVisible = true
                        binding.llTwo.isVisible = false
                        binding.twoLine.isVisible = false
                        binding.llThree.isVisible = false
                        binding.threeLine.isVisible = false
                        binding.llFour.isVisible = false
                        binding.fourLine.isVisible = false
                    }

                    11 -> {//血压
                        binding.title.mTvTitle.text = "添加血压"
                        binding.llOne.isVisible = true
                        binding.tvOneName.text = "收缩压"
                        binding.etOneValue.hint = "请输入收缩压"
                        binding.oneLine.isVisible = true
                        binding.llTwo.isVisible = true
                        binding.tvTwoName.text = "舒张压"
                        binding.etTwoValue.hint = "请输入舒张压"
                        binding.twoLine.isVisible = true
                        binding.llThree.isVisible = false
                        binding.threeLine.isVisible = false
                        binding.llFour.isVisible = false
                        binding.fourLine.isVisible = false
                    }

                    12 -> {//体重
                        binding.title.mTvTitle.text = "添加体重"
                        binding.llOne.isVisible = true
                        binding.tvOneName.text = "体重数值"
                        binding.etOneValue.hint = "请输入体重数值"
                        binding.oneLine.isVisible = true
                        binding.llTwo.isVisible = true
                        binding.tvTwoName.text = "身高数值"
                        binding.etTwoValue.hint = "请输入身高数值"
                        binding.twoLine.isVisible = true
                        binding.llThree.isVisible = false
                        binding.threeLine.isVisible = false
                        binding.llFour.isVisible = false
                        binding.fourLine.isVisible = false
                    }
                }
            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }

        //点击选择测量时间
        binding.tvTime.setOnClickListener {
            RxKeyboardTool.hideSoftInput(this)
            timSelect.selectTime(binding.tvTime, "yyyy-MM-dd HH:mm")
        }

        //点击取消按钮
        binding.mTvCancel.setOnClickListener { finish() }

        //点击确定按钮
        binding.mTvConfirm.setOnClickListener {
            if (resultData == null) {
                CommonToastUtils.showToast("数据出错啦！")
                return@setOnClickListener
            }
            //提交数据处理
            submitDispose(resultData?.type)
        }
    }

    /**
     * 提交数据处理
     */
    private fun submitDispose(type: Int?) {
        //dataType：1,血糖，2，血压，3，体重，4，尿酸，5，血脂
        when (type) {
            10 -> {//血糖
                val xtValueStr = binding.etOneValue.text.toString()
                val xtTime = binding.tvTime.text.toString()
                if (TextUtils.isEmpty(xtValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(xtTime)) {
                    CommonToastUtils.showToast("请选择测量时间")
                    return
                }
                mViewModel.addData(
                    CommonUtils.getMyUserId(),
                    "1",
                    xtTime,
                    xtValueStr,
                    "",
                    "",
                    "",
                    "",
                    ""
                )
            }

            14 -> {//血脂
                //总胆固醇
                val zdgcValueStr = binding.etOneValue.text.toString()
                //甘油三酯
                val gyszValueStr = binding.etTwoValue.text.toString()
                //高密度脂蛋白
                val gmdzdbValueStr = binding.etThreeValue.text.toString()
                //低密度脂蛋白
                val dmdzdbValueStr = binding.etFourValue.text.toString()
                //测量时间
                val xzTime = binding.tvTime.text.toString()
                if (TextUtils.isEmpty(zdgcValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(gyszValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(gmdzdbValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(dmdzdbValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(xzTime)) {
                    CommonToastUtils.showToast("请选择测量时间")
                    return
                }
                mViewModel.addData(
                    CommonUtils.getMyUserId(),
                    "5",
                    xzTime,
                    "",
                    zdgcValueStr,
                    gyszValueStr,
                    gmdzdbValueStr,
                    dmdzdbValueStr,
                    ""
                )
            }

            13 -> {//尿酸
                val xtValueStr = binding.etOneValue.text.toString()
                val xtTime = binding.tvTime.text.toString()
                if (TextUtils.isEmpty(xtValueStr)) {
                    CommonToastUtils.showToast("请输入尿酸数值")
                    return
                }
                if (TextUtils.isEmpty(xtTime)) {
                    CommonToastUtils.showToast("请选择测量时间")
                    return
                }
                mViewModel.addData(
                    CommonUtils.getMyUserId(),
                    "4",
                    xtTime,
                    xtValueStr,
                    "",
                    "",
                    "",
                    "",
                    ""
                )
            }

            11 -> {//血压
                //收缩压
                val ssyValueStr = binding.etOneValue.text.toString()
                //舒张压
                val szyValueStr = binding.etTwoValue.text.toString()
                //测量时间
                val xyTime = binding.tvTime.text.toString()
                if (TextUtils.isEmpty(ssyValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(szyValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(xyTime)) {
                    CommonToastUtils.showToast("请选择测量时间")
                    return
                }
                val result = "$ssyValueStr,$szyValueStr"
                mViewModel.addData(
                    CommonUtils.getMyUserId(),
                    "2",
                    xyTime,
                    result,
                    "",
                    "",
                    "",
                    "",
                    ""
                )
            }

            12 -> {//体重
                //体重数值
                val tzValueStr = binding.etOneValue.text.toString()
                //身高数值
                val sgValueStr = binding.etTwoValue.text.toString()
                //测量时间
                val tzTime = binding.tvTime.text.toString()
                if (TextUtils.isEmpty(tzValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(sgValueStr)) {
                    CommonToastUtils.showToast("请填写数据")
                    return
                }
                if (TextUtils.isEmpty(tzTime)) {
                    CommonToastUtils.showToast("请选择测量时间")
                    return
                }
                mViewModel.addData(
                    CommonUtils.getMyUserId(),
                    "3",
                    tzTime,
                    tzValueStr,
                    "",
                    "",
                    "",
                    "",
                    sgValueStr
                )
            }
        }
    }

    override fun liveObserver() {
        mViewModel.addDataOb.observe(this) {
            if (it.code == 0) {
                EventBus.getDefault().post(IntegralEvent())
                finish()
            } else {
                CommonToastUtils.showToast("数据提交失败：${it.msg}")
            }
        }
    }
}