package com.dlc.chemical.ui.input

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.dlc.chemical.R
import com.dlc.chemical.databinding.ActivityTareSettingBinding
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.InputWeightDialog
import com.dlc.chemical.ui.widget.TitleBar
import com.dlc.chemical.serial.usb.steelyard.SteelyardHelper
import com.dlc.chemical.serial.usb.steelyard.SteelyardManager
import com.rc.core.log.RcLog

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

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

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

        // 重量
        viewBinding.weighingValue.setOnClickListener {
            InputWeightDialog(this) { weight ->
                RcLog.info("停止称重，手动录入重量：${weight}")
                mSteelyardHelper.stopWeighing = true
                viewBinding.weighingValue.text = "${weight}g"
                viewBinding.weighingValue.setTag(R.id.tag_join_weight, weight)
            }.show()
        }

        // 置零
        viewBinding.peel.setOnClickListener {
            mSteelyardHelper.sendPeelCMD()
        }

        // 确认
        viewBinding.save.setOnClickListener {
            dispatchSaveClicked()
        }

        // 请将容器放置在称重台上
        viewBinding.weighingTips.setText(R.string.weight_tare_idle)
        AudioPlayerManager.speakVoice(R.raw.audio_tare)
        viewBinding.weighingValue.text = "0.0g"
    }

    private fun dispatchSaveClicked() {
        val weight = viewBinding.weighingValue.getTag(R.id.tag_join_weight) as String?
        if (weight.isNullOrEmpty()) {
            showToast("请称量容器重量！")
            return
        }

        val intent = Intent()
        intent.putExtra("tare", weight)
        setResult(RESULT_OK, intent)
        finish()
    }

    override fun onResume() {
        super.onResume()

        mSteelyardHelper.onResume()
    }

    override fun onPause() {
        super.onPause()

        mSteelyardHelper.onPause()
    }

    private val mSteelyardHelper by lazy {
        SteelyardHelper(this, object : SteelyardHelper.OnSteelyardListener {
            override fun showToast(message: String) {
                this@TareSettingActivity.showToast(message)
            }

            @SuppressLint("SetTextI18n")
            override fun refreshWeight(weight: String) {
                viewBinding.weighingValue.text = "${weight}g"
                viewBinding.weighingValue.setTag(R.id.tag_join_weight, weight)
            }

            override fun refreshTips(tipsResId: Int) {
                viewBinding.weighingTips.setText(tipsResId)
            }

            override fun refreshViewStatus(cmdFn: String) {
                if (SteelyardManager.FN_PEEL == cmdFn) {
                    viewBinding.peel.isEnabled = true
                }
            }

        })
    }

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

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

}