package com.abbidot.tracker.ui.activity.set

import android.view.View
import androidx.core.view.isGone
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.adapter.FirstPermissionsAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.databinding.ActivityFirstPermissionsBinding
import com.abbidot.tracker.ui.common.PermissionsActivityCommon
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil


class FirstPermissionsActivity :
    BaseActivity<ActivityFirstPermissionsBinding>(ActivityFirstPermissionsBinding::inflate) {

    private lateinit var mPermissionsAdapter: FirstPermissionsAdapter

    lateinit var mPermissionsCommon: PermissionsActivityCommon

    override fun getTopBar() = null

    override fun initData() {
        super.initData()

        mPermissionsCommon = PermissionsActivityCommon(this).apply {
            registerGpsReceiver()
        }
        mPermissionsAdapter =
            FirstPermissionsAdapter(mContext, mPermissionsCommon.addListData(mContext)).apply {
                setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                    override fun onItemClick(itemView: View?, pos: Int) {
                        mPermissionsCommon.updateDataBeanSelectedItem(
                            mPermissionsAdapter, getData(), pos
                        )
                    }
                })
            }

        mViewBinding.apply {

            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvFirstPermissionList, mPermissionsAdapter, top = AppUtils.dpToPx(12)
            )

            btnFirstPermissionNext.visibility = View.GONE

            setOnClickListenerViews(btnFirstPermissionNext)
        }
    }

    override fun liveDataObserve() {
        //蓝牙开关操作
        XEventBus.observe(this, EventName.BluetoothSwitch) { conState: Int ->
            mPermissionsCommon.updatePermissionsState(mPermissionsAdapter, ConstantInt.SpecialType)
        }
        //gps开关操作
        XEventBus.observe(this, EventName.GPSSwitchState) { switchState: Int ->
            mPermissionsCommon.updatePermissionsState(mPermissionsAdapter, ConstantInt.SpecialType)
        }
    }

    override fun onResume() {
        super.onResume()
        LogUtil.e("PermissionsActivity----------------->>>onResume")
        mPermissionsCommon.updatePermissionsState(mPermissionsAdapter, ConstantInt.SpecialType)
    }


    override fun listenBackPressed() {

    }

    /**
     * 更新下一步按钮状态
     */
    fun updateNextBtnState() {
        val list = mPermissionsAdapter.getData()
        var showBtn = true
        for (data in list) {
            if (data.state == ConstantInt.Type0) {
                showBtn = false
                break
            }
        }
        mViewBinding.apply {
            if (showBtn && btnFirstPermissionNext.isGone) {
                ViewUtil.instance.viewShowFadeInAnimation(btnFirstPermissionNext, 1000)
            }
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnFirstPermissionNext -> {
                    setButtonEnabled(v)
                    startActivityFinish(Util.checkBindDeviceAndPetGoActivity(mContext))
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mPermissionsCommon.unregisterGpsReceiver()
    }
}