package xn.hxp.ui.warehousing

import android.os.Handler
import android.os.Looper
import androidx.viewbinding.ViewBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.rc.core.ui.activity.BaseActivity
import com.rc.httpcore.HttpConfig
import com.rc.httpcore.client.HttpTool
import xn.hxp.app.ChemicalApp
import xn.hxp.databinding.ActivityElectronicLedgerBinding
import xn.hxp.utils.PrintTool
import xn.hxp.utils.TimeUpdater
import xn.hxp.utils.UiManager
import xn.hxp.weidith.AirBottleDialog
import xn.hxp.weidith.CustomDialog

//标签管理
class ElectronicLedgerActivity : BaseActivity() {

    private lateinit var timeUpdater: TimeUpdater
    lateinit var viewBinding: ActivityElectronicLedgerBinding
    override fun setViewBinding(): ViewBinding {
        viewBinding = ActivityElectronicLedgerBinding.inflate(layoutInflater)
        return viewBinding
    }

    override fun onInit() {
        viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
        viewBinding.tvOutLogin.setOnClickListener {
            HttpTool.logout()
        }
        viewBinding.tvReturn.setOnClickListener {
            finish()
        }

        viewBinding.chemicalLabeling.setOnClickListener {
            //化学品标签
            UiManager.switcher(this, ChemicalLabelingActivity::class.java)
        }

        viewBinding.cylinderLabel.setOnClickListener {
            //气瓶标签
            var airBottleDialog = AirBottleDialog(this, object : AirBottleDialog.ILintDate {
                override fun onLintDate(cont: String) {
                    if (cont.isNotEmpty()) {
                        PrintTool.INSTANCE.print(cont)
                    }
                }
            })
            airBottleDialog.show()
        }

        val handler = Handler(Looper.getMainLooper())
        timeUpdater = TimeUpdater(handler) { currentTime ->
            viewBinding.nowTime.text = "$currentTime"
        }
        // 启动定时更新
        timeUpdater.startUpdating()


        viewBinding.tvName.text = ChemicalApp.userData!!.userName
        val imageView = viewBinding.imageName
        // 使用 Glide 加载网络图片
        Glide.with(this)
            .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
            .into(imageView)

        // 使用 Glide 加载网络图片
        viewBinding.deptName.text = "${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"
        Glide.with(this)
            .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
            .into(viewBinding.image)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    override fun cdTime(cd: Int) {
        viewBinding.tvReturn.text = "返回${cd}s"
    }

    override fun onDestroy() {
        super.onDestroy()
        try {
            timeUpdater.stopUpdating()
        } catch (e: Exception) {
        }
    }

    override fun cdFinish() {
        finish()
    }

    /**
     * 0  没有图标 1 绿色(成功)   2红色（失败）
     *   失败或者成功的弹框
     */
    private fun customDialogView(types: Int, msg: String) {
        if (!this.isFinishing && !this.isDestroyed) {
            val customDialog = CustomDialog(this, types, msg)
            customDialog.show()
        }
    }
}