package com.abbidot.tracker.ui.activity.device.wifi

import android.content.Intent
import android.content.res.ColorStateList
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.WiFiZoneAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.bean.WiFiZoneBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityPowerSavingSetBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.WiFiZoneViewModel
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButtonDrawable

class PowerSavingSetActivity :
    BaseActivity<ActivityPowerSavingSetBinding>(ActivityPowerSavingSetBinding::inflate) {

    private val mWiFiZoneViewModel: WiFiZoneViewModel by viewModels()

    private lateinit var mZoneAdapter: WiFiZoneAdapter

    private var mPetBean: PetBean? = null
    private var mPetCurrentLat = 0.0
    private var mPetCurrentLon = 0.0

    //禁用模式,无法点击的情况
    private var isDisabledMode = true

    override fun getTopBar() = mViewBinding.ilPowerSavingSetTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_power_saving_set)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        mPetBean = Util.getParcelableAdaptive(intent, ConstantString.Pet, PetBean::class.java)

        mZoneAdapter = WiFiZoneAdapter(mContext, null).apply {
            setOnAgainClickListener(object : BaseRecyclerAdapter.OnAgainClickListener {
                override fun onAgainListener() {
                    goAddWifiZone()
                }
            })
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    goEditWiFiZone(pos)
                }
            })
            setDisabledMode(false)
        }

        mViewBinding.apply {
            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvPowerSavingZone, mZoneAdapter, top = QMUIDisplayHelper.dpToPx(16)
            )

            setOnClickListenerViews(btnPowerSavingSetAdd)
        }

        mPetBean?.apply {
            mWiFiZoneViewModel.getWifiZoneList(this@PowerSavingSetActivity, deviceId)
        }
    }

    override fun liveDataObserve() {
        //添加成功刷新页面
        XEventBus.observe(this, EventName.RefreshWiFiZone) {
            mPetBean?.apply {
                mWiFiZoneViewModel.getWifiZoneList(this@PowerSavingSetActivity, deviceId)
            }
        }
        XEventBus.observe(this, EventName.DeleteWiFiZone) {
            mPetBean?.apply {
                mWiFiZoneViewModel.getWifiZoneList(this@PowerSavingSetActivity, deviceId)
            }
        }

        mWiFiZoneViewModel.mWiFiZoneListLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.apply {
                        mZoneAdapter.setData(null)
                        mPetCurrentLat = latitude
                        mPetCurrentLon = longitude
                        isDisabledMode = powerSwitch == ConstantInt.Type0
                        list?.let { wifi ->
                            setWifiListData(wifi)
                        }
                    }
                }
            })

            val share = MMKVUtil.getInt(MMKVKey.Shared)
            if (share != ConstantInt.NoShare) {
                mZoneAdapter.showNoDataAddButton(false)
            }
        }
    }

    private fun goEditWiFiZone(pos: Int) {
        val share = MMKVUtil.getInt(MMKVKey.Shared)
        if (share != ConstantInt.NoShare) {
            return
        }
        Util.checkLocationPermissionsGpsEnabled(this, {
            if (mZoneAdapter.getData().isNotEmpty()) {
                val wiFiZoneBean = mZoneAdapter.getData()[pos]
                val intent = Intent(mContext, EditWifiPowerZoneActivity::class.java)
                intent.putExtra(ConstantString.WiFiZone, wiFiZoneBean)
//                    mDecWiFiAddressData?.let {
//                        intent.putExtra(ConstantString.HistoryDataBean, it)
//                    }
                intent.putExtra(ConstantString.Pet, mPetBean)
                intent.putExtra(ConstantString.Latitude, mPetCurrentLat)
                intent.putExtra(ConstantString.Longitude, mPetCurrentLon)
                intent.putExtra(ConstantString.isCheck, isDisabledMode)
                startActivity(intent)
            }
        })
    }

    private fun setWifiListData(list: MutableList<WiFiZoneBean>) {
        if (list.size > 0) {
            val aDrawable = mViewBinding.btnPowerSavingSetAdd.background as QMUIRoundButtonDrawable
            val dColor = if (isDisabledMode) R.color.grey_color
            else R.color.btn_color
            val tColor = ContextCompat.getColor(mContext, dColor)
            aDrawable.setStrokeColors(ColorStateList.valueOf(tColor))
            mViewBinding.btnPowerSavingSetAdd.setTextColor(tColor)
        }
        list.apply {
            mViewBinding.let {
                if (size > 0) {
                    it.btnPowerSavingSetAdd.visibility = if (size == 2) View.INVISIBLE
                    else View.VISIBLE
                    mZoneAdapter.setData(list, true)
                } else {
                    it.btnPowerSavingSetAdd.visibility = View.INVISIBLE
                }

                val share = MMKVUtil.getInt(MMKVKey.Shared)
                if (share != ConstantInt.NoShare) {
                    it.btnPowerSavingSetAdd.visibility = View.INVISIBLE
                }
            }
        }
    }

    private fun goAddWifiZone() {
        Intent(mContext, AddWifiPowerZone1Activity::class.java).apply {
            mPetBean?.let {
                putExtra(ConstantString.Pet, it)
            }
            putExtra(ConstantString.Latitude, mPetCurrentLat)
            putExtra(ConstantString.Longitude, mPetCurrentLon)
            startActivity(this)
        }
    }


    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnPowerSavingSetAdd -> {
                    if (isDisabledMode) return
                    goAddWifiZone()
                }
            }
        }
    }
}