package com.dlc.chemical.ui.input

import android.annotation.SuppressLint
import android.content.Intent
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.ActivityInputStoreCompletedBinding
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.dialog.SuccessDialog
import com.dlc.chemical.ui.msds.MsdsDetailActivity
import com.dlc.chemical.ui.widget.TitleBar
import com.rc.httpcore.vo.response.ChemicalBean
import java.util.*

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

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

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

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

        // 继续入库
        viewBinding.continueInput.setOnClickListener {
            // MainActivity -> QueryChemicalsActivity -> InputStoreActivity(finish) -> InputStoreCompletedActivity
            finish()
        }
    }

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

    private fun dispatchInputSuccess() {
        AudioPlayerManager.speakVoice(R.raw.audio_success_input)
        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
        // 库存净重
        contentViewBinding.suttle.text = "${mChemicalBean.suttle ?: "0.0"}${mChemicalBean.chemicalUnit ?: "g"}"
    }

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

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

}