package com.casic.br.view.device

import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.casic.br.R
import com.casic.br.extensions.combineImagePath
import com.casic.br.extensions.convertValue
import com.casic.br.extensions.initLayoutImmersionBar
import com.casic.br.utils.LoadingDialogHub
import com.casic.br.vm.DeviceViewModel
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.Constant
import com.pengxh.kt.lite.vm.LoadState
import kotlinx.android.synthetic.main.activity_gas_alarm_hw.*
import kotlinx.android.synthetic.main.include_device_title.*

/**
 * 汉威报警器
 * */
class HWGasAlarmActivity : KotlinBaseActivity() {

    private lateinit var deviceViewModel: DeviceViewModel

    override fun initLayoutView(): Int = R.layout.activity_gas_alarm_hw

    override fun observeRequestState() {
        deviceViewModel.loadState.observe(this, {
            when (it) {
                LoadState.Loading -> {
                    LoadingDialogHub.show(this, "加载数据中...")
                }
                LoadState.Success -> {
                    LoadingDialogHub.dismiss()
                    finish()
                }
                else -> LoadingDialogHub.dismiss()
            }
        })
    }

    override fun setupTopBarLayout() {
        initLayoutImmersionBar(rootView)
        ImmersionBar.with(this).statusBarDarkFont(true).init()

        leftBackView.setOnClickListener { finish() }
        rightOperateView.visibility = View.GONE
    }

    override fun initData() {
        val id = intent.getStringExtra(Constant.INTENT_PARAM)!!

        deviceViewModel = ViewModelProvider(this)[DeviceViewModel::class.java]
        deviceViewModel.obtainDeviceDetail(id)
        deviceViewModel.deviceModel.observe(this, {
            if (it.code == 200) {
                val sensorData = it.data.businessExt

                Glide.with(this)
                    .load(it.data.picUri.combineImagePath())
                    .apply(RequestOptions.circleCropTransform())
                    .placeholder(R.mipmap.load_image_error)
                    .into(deviceLogoView)

                deviceStateView.text = if (it.data.deviceActive == 1) {
                    "在线"
                } else {
                    "离线"
                }

                //取数据小数点位
                tempValueView.text =
                    "${sensorData.sensorVal2.convertValue(sensorData.deciPoint2.toInt())}${sensorData.sensorValUnit2}"
                sensorValueView.text =
                    "${sensorData.sensorVal1.convertValue(sensorData.deciPoint1.toInt())}${sensorData.sensorValUnit1}"

                updateTimeView.text = sensorData.updateTime

                tempStateView.text = sensorData.workStatus2
                sensorStateView.text = sensorData.workStatus1
            }
        })
    }

    override fun initEvent() {
        showMoreView.setOnClickListener {
            "此功能待后续完善".show(this)
        }
    }
}