package com.casic.br.view

import android.graphics.Color
import android.util.Log
import androidx.recyclerview.widget.DividerItemDecoration
import com.casic.br.R
import com.casic.br.adapter.AddedDeviceAdapter
import com.casic.br.extensions.selectCategory
import com.casic.br.utils.DeserializeModel
import com.casic.br.view.device.CookerActivity
import com.casic.br.view.device.FireplaceActivity
import com.casic.br.view.device.RangeHoodActivity
import com.casic.br.view.device.WaterHeaterActivity
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.convertColor
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.utils.ImmerseStatusBarUtil
import com.pengxh.kt.lite.widget.dialog.BottomActionSheet
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.home.sdk.bean.HomeBean
import com.tuya.smart.home.sdk.callback.ITuyaHomeResultCallback
import com.tuya.smart.sdk.api.IResultCallback
import com.tuya.smart.sdk.bean.DeviceBean
import kotlinx.android.synthetic.main.fragment_device.*
import kotlinx.android.synthetic.main.include_base_title.*
import java.util.*

class AllDeviceActivity : KotlinBaseActivity() {

    private val kTag = "AllDeviceActivity"
    private val context = this@AllDeviceActivity
    private var deviceModels: MutableList<DeviceBean> = ArrayList()
    private lateinit var addedDeviceAdapter: AddedDeviceAdapter

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

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        ImmerseStatusBarUtil.setColor(this, R.color.white.convertColor(this))

        leftBackView.setOnClickListener { finish() }
        titleView.text = "全部设备"
    }

    override fun initData() {
        TuyaHomeSdk.newHomeInstance(DeserializeModel.getHomeId())
            .getHomeDetail(object : ITuyaHomeResultCallback {
                override fun onSuccess(bean: HomeBean) {
                    deviceModels = bean.deviceList
                    addedDeviceAdapter = AddedDeviceAdapter(context, deviceModels)
                    deviceRecyclerView.addItemDecoration(
                        DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
                    )
                    deviceRecyclerView.adapter = addedDeviceAdapter
                    addedDeviceAdapter.setOnItemClickListener(object :
                        AddedDeviceAdapter.OnItemClickListener {
                        override fun onButtonClick(position: Int) {
                            moreOperate(position)
                        }

                        override fun onItemClick(position: Int) {
                            val deviceModel = deviceModels[position]
                            when {
                                deviceModel.selectCategory().contains("rs") -> {
                                    navigatePageTo<WaterHeaterActivity>(deviceModel.devId)
                                }
                                deviceModel.selectCategory().contains("yyj") -> {
                                    navigatePageTo<RangeHoodActivity>(deviceModel.devId)
                                }
                                deviceModel.selectCategory().contains("bgl") -> {
                                    navigatePageTo<FireplaceActivity>(deviceModel.devId)
                                }
                                deviceModel.selectCategory().contains("rqz") -> {
                                    navigatePageTo<CookerActivity>(deviceModel.devId)
                                }
                                deviceModel.selectCategory().contains("wg2") -> {

                                }
                            }
                        }

                        override fun onAddDeviceClick() {
                            navigatePageTo<AddDeviceActivity>()
                        }
                    })
                }

                override fun onError(errorCode: String?, errorMsg: String?) {

                }
            })
    }

    private fun moreOperate(clickPosition: Int) {
        BottomActionSheet.Builder()
            .setContext(this)
            .setActionItemTitle(arrayListOf("删除设备"))
            .setItemTextColor(Color.BLUE)
            .setOnActionSheetListener(object :
                BottomActionSheet.OnActionSheetListener {
                override fun onActionItemClick(position: Int) {
                    when (position) {
                        0 -> TuyaHomeSdk.newDeviceInstance(deviceModels[clickPosition].devId)
                            .removeDevice(object : IResultCallback {
                                override fun onError(code: String?, error: String?) {
                                    Log.d(kTag, "onError: $error")
                                }

                                override fun onSuccess() {
                                    deviceModels.removeAt(clickPosition)
                                    addedDeviceAdapter.notifyItemRemoved(clickPosition)
                                    addedDeviceAdapter.notifyItemRangeChanged(
                                        clickPosition, deviceModels.size - clickPosition
                                    )
                                }
                            })
                    }
                }
            }).build().show()
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {

    }
}