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

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.FamilyBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityAddNewTracker1Binding
import com.abbidot.tracker.ui.activity.account.LoginV2Activity
import com.abbidot.tracker.ui.activity.pet.ShowInviteDeviceActivity
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.vm.FamilyViewModel

class AddNewTracker1Activity :
    BaseActivity<ActivityAddNewTracker1Binding>(ActivityAddNewTracker1Binding::inflate) {

    private val mFamilyViewModel: FamilyViewModel by viewModels()

    private var isFirstBind = false

    override fun getTopBar() = mViewBinding.ilAddNewTracker1TopBar.titleTopBar

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

        intent.extras?.apply {
            isFirstBind = getBoolean(ConstantString.isFirstBind, false)
        }

        mViewBinding.apply {
//            ivChargePowerOnGif.load(R.drawable.charge_power_on)

            if (!isFirstBind) {
                tvChargePowerOnBackLoginBtn.visibility = View.GONE

            }
            setOnClickListenerViews(btnChargePowerOnNext, tvChargePowerOnBackLoginBtn)
        }

        //这个页面判断有没有新的邀请,不用管删除的
        mFamilyViewModel.getDeviceInviteInfo(this)
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.RefreshDevice) {
            finish()
        }

        //获取设备邀请信息
        mFamilyViewModel.mInviteInfoLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.apply {
                        if (inviteArray.isNotEmpty()) {
                            Intent(mContext, ShowInviteDeviceActivity::class.java).let { intent ->
                                intent.putParcelableArrayListExtra(
                                    ConstantString.Type, inviteArray as ArrayList<FamilyBean>
                                )
                                startActivityFinish(intent)
                            }
                        }
                    }
                }
            })
        }
    }

    private var mTempTime = 0L
    override fun leftBackOnClick() {
        if (isFirstBind) {
            // 2s内再次选择有效
            if (System.currentTimeMillis() - mTempTime > 2000) {
                showToast(R.string.txt_exit_return)
                mTempTime = System.currentTimeMillis()
            } else {
                finish()
                System.gc()
            }
        } else {
            super.leftBackOnClick()
        }
    }

    /**
     * 检查获取定位蓝牙权限
     */
    private fun checkPermissions() {
        Util.checkBluetoothPermissionsEnabled(this, {
            val intent = Intent(mContext, AddNewTracker3Activity::class.java)
            intent.putExtra(ConstantString.isFirstBind, isFirstBind)
            startActivity(intent)
        })
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnChargePowerOnNext -> checkPermissions()
                tvChargePowerOnBackLoginBtn -> startActivityFinish(
                    Intent(
                        mContext, LoginV2Activity::class.java
                    )
                )
            }
        }
    }
}