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

import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.provider.Settings
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
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.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityAddWifiPowerZone2Binding
import com.abbidot.tracker.ui.activity.HomeV2Activity
import com.abbidot.tracker.ui.common.map.WifiZone2MapCommon
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

class AddWifiPowerZone2Activity :
    BaseActivity<ActivityAddWifiPowerZone2Binding>(ActivityAddWifiPowerZone2Binding::inflate) {

    private lateinit var mWifiZone2MapCommon: WifiZone2MapCommon
    private lateinit var mFragment: Fragment

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

    override fun getTopBar() = mViewBinding.ilAddWifiZone2TopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle("")

        intent.extras?.apply {
            isFirstBind = getBoolean(ConstantString.isFirstBind, false)
            mPetBean = Util.getParcelableAdaptive(intent, ConstantString.Pet, PetBean::class.java)
            mPetCurrentLat = getDouble(ConstantString.Latitude, 0.0)
            mPetCurrentLon = getDouble(ConstantString.Longitude, 0.0)
        }

        if (isFirstBind) setLeftBackImage(0)
        else setLeftBackImage(R.drawable.icon_white_back_svg)

        mWifiZone2MapCommon = WifiZone2MapCommon()
        mFragment = mWifiZone2MapCommon.getMapFragment(mContext)

        mViewBinding.apply {
            tvWifiZone2LaterBtn.visibility = if (isFirstBind) View.VISIBLE
            else View.INVISIBLE
            supportFragmentManager.commit {
                add(R.id.fc_wifi_power_zone2_map_fragment, mFragment)
            }

            setOnClickListenerViews(
                btnAddWifiZone2Next,
                tbRefreshLocationBtn,
                tvWifiZone2LaterBtn,
                ivWifiPowerZone2MapTypeBtn
            )
        }
    }

    override fun liveDataObserve() {
        //添加成功结束页面
        XEventBus.observe(this, EventName.RefreshWiFiZone) {
            finish()
        }
    }

    private fun goNext() {
        val petAddressData = mWifiZone2MapCommon.getDecWiFiAddressData()
        if (null == petAddressData || petAddressData.latitude == 0.0 || petAddressData.longitude == 0.0) {
        } else {
            mPetBean?.let {
                if (it.deviceType == ConstantInt.Type2) {
                    val wifiManager =
                        applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
                    if (!wifiManager.isWifiEnabled) {
                        val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
                        startActivity(intent)
                        return
                    }
                }
                val intent = Intent(mContext, AddWifiPowerZone3Activity::class.java)
                intent.putExtra(ConstantString.Pet, it)
                intent.putExtra(ConstantString.Latitude, mPetCurrentLat)
                intent.putExtra(ConstantString.Longitude, mPetCurrentLon)
                intent.putExtra(ConstantString.isFirstBind, isFirstBind)
                intent.putExtra(ConstantString.HistoryDataBean, petAddressData)
                startActivity(intent)
            }
        }
    }

    override fun listenBackPressed() {
        if (!isFirstBind) super.listenBackPressed()
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnAddWifiZone2Next -> Util.checkBluetoothPermissionsEnabled(this@AddWifiPowerZone2Activity, {
                    goNext()
                })

                tvWifiZone2LaterBtn -> {
//                    XEventBus.post(EventName.RefreshWiFiZone)
                    startActivityFinish(Intent(mContext, HomeV2Activity::class.java))
                }

                tbRefreshLocationBtn -> {
                    ViewUtil.instance.viewAlphaAndRotationObjectAnimator(v)
                    mWifiZone2MapCommon.refreshLocation()
                }

                ivWifiPowerZone2MapTypeBtn -> mWifiZone2MapCommon.switchSatelliteAndNormalMapType()
            }
        }
    }
}