package com.dlc.chemical.ui.revert

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import com.dlc.chemical.R
import com.dlc.chemical.databinding.ActivityRevertCompletedBinding
import com.dlc.chemical.databinding.ContentChemicalsInfoBinding
import com.dlc.chemical.iflytek.AudioPlayerManager
import com.dlc.chemical.ui.MainActivity
import com.dlc.chemical.ui.common.BaseCountDownActivity
import com.dlc.chemical.ui.common.ModuleEnum
import com.dlc.chemical.ui.common.TagScanActivity
import com.dlc.chemical.ui.dialog.SuccessDialog
import com.dlc.chemical.ui.msds.MsdsDetailActivity
import com.dlc.chemical.ui.widget.TitleBar
import com.rc.core.util.trimEndZero
import com.rc.httpcore.vo.response.ChemicalBean
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * info
 *
 * @author ReiChin_
 */
class RevertCompletedActivity : BaseCountDownActivity<ActivityRevertCompletedBinding>(),
    TitleBar.TitleListener {

    private val mChemicalBean: ChemicalBean by lazy {
        intent.getParcelableExtra("chemicalBean")!!
    }

    override fun createViewBinding() = ActivityRevertCompletedBinding.inflate(layoutInflater)

    override fun initViews(savedInstanceState: Bundle?) {
        viewBinding.titleBar.setTitleListener(this)
        viewBinding.titleBar.setTitleInfoFromSp()

        // 继续归还
        viewBinding.continueRevert.setOnClickListener {
            // MainActivity -> TagScanActivity -> RevertActivity(finish) -> RevertCompletedActivity
            val intent = Intent(this, TagScanActivity::class.java)
            intent.putExtra("moduleEnum", ModuleEnum.REVERT)
            startActivity(intent)
            finish()
        }
    }

    override fun initData() {
        // 延迟加载内容页
        Handler(Looper.getMainLooper()).postDelayed({
            dispatchInputSuccess()
            inflateContentView()
        }, 50)
    }

    private fun dispatchInputSuccess() {
        AudioPlayerManager.speakVoice(R.raw.audio_success_revert)
        SuccessDialog(this, "归还成功").apply {
            setCancelable(false)
        }.show()
    }

    @SuppressLint("SetTextI18n")
    private fun inflateContentView() {
        val contentViewBinding =
            ContentChemicalsInfoBinding.bind(viewBinding.content.inflate())

        // MSDS
        contentViewBinding.msds.setOnClickListener {
            if (mChemicalBean.hazardId.isNullOrEmpty()) {
                showToast("暂无MSDS信息")
                return@setOnClickListener
            }
            MsdsDetailActivity.startAction(this, mChemicalBean.hazardId)
        }

        // 化学品名
        contentViewBinding.chemicalName.text = mChemicalBean.chemicalName
        // 所在位置
        contentViewBinding.cabinetName.text = mChemicalBean.cabinetName
        // 别名
        contentViewBinding.anotherName.text = mChemicalBean.anotherName
        // 负责人
        contentViewBinding.safeUserName.text = mChemicalBean.safeUserName
        // 分类
        contentViewBinding.classifyName.text = mChemicalBean.classifyName
        // 过期时间
        contentViewBinding.expirationTime.text = mChemicalBean.expirationTime
        // 编号
        contentViewBinding.chemicalNum.text = mChemicalBean.chemicalNum
        // CAS号
        contentViewBinding.casNum.text = mChemicalBean.casNum
        // 容量规格
        contentViewBinding.chemicalAmount.text =
            "${mChemicalBean.chemicalAmount ?: ""}${mChemicalBean.chemicalAmountUnit ?: ""}"
        // 形态
        contentViewBinding.chemicalShape.text = mChemicalBean.chemicalShapeName
        // 容器规格
        contentViewBinding.tare.text = if ("g" == mChemicalBean.chemicalUnit) "${mChemicalBean.tare ?: "0.0"}g" else "--"
        // 纯度
        contentViewBinding.purity.text = mChemicalBean.purity

        // 归还时净重=归还时称重量-容器规格
        val tare = if ("g" == mChemicalBean.chemicalUnit) mChemicalBean.tare else "0"
        val revertSuttle = try {
            BigDecimal(mChemicalBean.returnStockNum) // 归还时称重量
                .subtract(BigDecimal(tare))
                .setScale(5, RoundingMode.HALF_UP)
        } catch (e: Exception) { BigDecimal("0.00") }

        // 领用时净重=领用时库存-容器规格
        val stockSuttle = try {
            BigDecimal(mChemicalBean.hxpUserecord.collectStockNum)
                .subtract(BigDecimal(tare))
                .setScale(5, RoundingMode.HALF_UP)
        } catch (e: Exception) { BigDecimal("0.00") }

        // 本次使用量=领用时净重-归还时净重
        val useSuttle = try {
            stockSuttle.subtract(revertSuttle).setScale(5, RoundingMode.HALF_UP)
        } catch (e: Exception) { BigDecimal("0.00") }

        // 库存净重=归还时净重
        contentViewBinding.suttle.text = "${revertSuttle.toPlainString().trimEndZero()}${mChemicalBean.chemicalUnit ?: "g"}"
        // 领用净重
        contentViewBinding.lossAmountGroup.visibility = View.VISIBLE
        contentViewBinding.lossAmountLabel.text = "领用净重"
        contentViewBinding.lossAmount.text = "${stockSuttle.toPlainString().trimEndZero()}${mChemicalBean.chemicalUnit ?: "g"}"
        // 本次使用量
        contentViewBinding.useSuttleGroup.visibility = View.VISIBLE
        contentViewBinding.useSuttleLabel.text = "本次使用量"
        contentViewBinding.useSuttle.text = "${useSuttle.toPlainString().trimEndZero()}${mChemicalBean.chemicalUnit ?: "g"}"
        contentViewBinding.useSuttleLabel.setTextColor(Color.parseColor("#FF2296FB"))
    }

    override fun onHomeViewClicked(view: View) {
        startActivity(Intent(this, MainActivity::class.java))
    }

    override fun onBackViewClicked(view: View) {
        finish()
    }

}