package com.casic.br.view.device

import android.content.Context
import android.graphics.Typeface
import android.util.Log
import android.view.LayoutInflater
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.alibaba.fastjson.JSONObject
import com.bumptech.glide.Glide
import com.casic.br.R
import com.casic.br.adapter.TabPagerAdapter
import com.casic.br.extensions.initLayoutImmersionBar
import com.casic.br.fragment.waterheater.ControlPageFragment
import com.casic.br.fragment.waterheater.ServicePageFragment
import com.casic.br.fragment.waterheater.StatusPageFragment
import com.casic.br.utils.CommandManager
import com.casic.br.utils.LocaleConstant
import com.google.android.material.tabs.TabLayout
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.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.sdk.api.IDevListener
import com.tuya.smart.sdk.api.ITuyaDevice
import kotlinx.android.synthetic.main.activity_range_hood.*
import kotlinx.android.synthetic.main.activity_water_heater.*
import kotlinx.android.synthetic.main.activity_water_heater.deviceLogoView
import kotlinx.android.synthetic.main.activity_water_heater.deviceTabLayout
import kotlinx.android.synthetic.main.activity_water_heater.deviceViewPager
import kotlinx.android.synthetic.main.activity_water_heater.rootView
import kotlinx.android.synthetic.main.include_device_title.*
import java.util.*


/**
 * 热水器
 * */
class WaterHeaterActivity : KotlinBaseActivity() {

    private val kTag = "WaterHeaterActivity"
    private val context: Context = this@WaterHeaterActivity
    private val inflater by lazy { LayoutInflater.from(this) }
    private var fragmentPages: ArrayList<Fragment> = ArrayList()
    private lateinit var deviceInstance: ITuyaDevice

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

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

        leftBackView.setOnClickListener { finish() }
        titleView.text = intent.getStringExtra(Constant.INTENT_PARAM)
    }

    override fun initData() {
        val deviceId = intent.getStringExtra(Constant.INTENT_PARAM)!!
        //控制设备前必须初始家庭化数据，每次 App 活跃期间，初始化一次即可。
        deviceInstance = TuyaHomeSdk.newDeviceInstance(deviceId)

        val deviceBean = TuyaHomeSdk.getDataInstance().getDeviceBean(deviceId)
        if (deviceBean == null) {
            "查询设备信息失败，请检查设备是否已离线".show(this)
            return
        }
        titleView.text = "万家乐热水器"
        Glide.with(this)
            .load(deviceBean.iconUrl)
            .placeholder(R.mipmap.load_image_error)
            .into(deviceLogoView)

        fragmentPages.add(ControlPageFragment(deviceInstance))
        fragmentPages.add(StatusPageFragment(deviceInstance))
        fragmentPages.add(ServicePageFragment())

        deviceInstance.registerDevListener(object : IDevListener {

            override fun onDpUpdate(devId: String?, dpStr: String?) {
                Log.d(kTag, "onDpUpdate: $dpStr")

                val controlMsg = ControlPageFragment.weakReference.obtainMessage()
                controlMsg.what = 2022092601
                controlMsg.obj = dpStr
                ControlPageFragment.weakReference.sendMessage(controlMsg)

                val statusMsg = StatusPageFragment.weakReference.obtainMessage()
                statusMsg.what = 2022092602
                statusMsg.obj = dpStr
                StatusPageFragment.weakReference.sendMessage(statusMsg)
            }

            override fun onRemoved(devId: String?) {

            }

            override fun onStatusChanged(devId: String?, online: Boolean) {

            }

            override fun onNetworkStatusChanged(devId: String?, status: Boolean) {

            }

            override fun onDevInfoUpdate(devId: String?) {

            }

        })
    }

    override fun onResume() {
        super.onResume()
        //每次获取焦点都查询设备的开关机状态
        CommandManager.getTuyaDp(deviceInstance, "1")
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        rightOperateView.setOnClickListener {

        }

        deviceViewPager.adapter =
            TabPagerAdapter(fragmentPages, LocaleConstant.DEVICE_PAGE_TITLE, supportFragmentManager)
        deviceTabLayout.setupWithViewPager(deviceViewPager)
        deviceTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val textView = inflater.inflate(R.layout.item_top_tab, null) as TextView
                textView.textSize = 16f
                textView.typeface = Typeface.DEFAULT_BOLD
                textView.text = tab!!.text
                tab.customView = textView
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.customView = null
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        deviceInstance.unRegisterDevListener()
        deviceInstance.onDestroy()
    }
}