package com.casic.br.view

import android.util.Log
import android.view.KeyEvent
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.casic.br.R
import com.casic.br.adapter.ViewPagerAdapter
import com.casic.br.fragment.*
import com.casic.br.utils.LocaleConstant
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.SaveKeyValues
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.home.sdk.bean.HomeBean
import com.tuya.smart.home.sdk.callback.ITuyaHomeResultCallback
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : KotlinBaseActivity() {

    private val kTag = "MainActivity"
    private var menuItem: MenuItem? = null
    private var fragmentPages: ArrayList<Fragment> = ArrayList()
    private var clickTime: Long = 0

    init {
        fragmentPages.add(HomePageFragment())
        fragmentPages.add(DevicePageFragment())
        fragmentPages.add(ShopPageFragment())
        fragmentPages.add(MessagePageFragment())
        fragmentPages.add(MinePageFragment())
    }

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

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
    }

    override fun initData() {
        /**
         * TODO 需要改为实际情况
         *
         * 家庭是智能生活 App SDK 开发下实际场景的最大单位。
         * IoT 设备的添加、编辑、移除、状态变化的监听基于家庭下。
         * 用户账号下创建任意多个家庭。
         * */
        val homeId = SaveKeyValues.getValue(LocaleConstant.HOME_ID, "") as String
        if (homeId == "") {
            TuyaHomeSdk.getHomeManagerInstance()
                .createHome("北燃生活体验馆", 116.486394, 39.885734, "西直门内南小街", arrayListOf("1"), object :
                    ITuyaHomeResultCallback {
                    override fun onSuccess(bean: HomeBean?) {
                        SaveKeyValues.putValue(LocaleConstant.HOME_ID, bean?.homeId.toString())
                    }

                    override fun onError(errorCode: String?, errorMsg: String?) {
                        Log.e(kTag, "errorCode: $errorCode ---- $errorMsg")
                    }
                })
        }
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        bottomNavigation.itemIconTintList = null
        bottomNavigation.setOnItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.nav_home -> {
                    mainViewPager.currentItem = 0
                }
                R.id.nav_device -> {
                    mainViewPager.currentItem = 1
                }
                R.id.nav_shop -> {
                    mainViewPager.currentItem = 2
                }
                R.id.nav_message -> {
                    mainViewPager.currentItem = 3
                }
                R.id.nav_mine -> {
                    mainViewPager.currentItem = 4
                }
            }
            false
        }
        mainViewPager.adapter = ViewPagerAdapter(fragmentPages, supportFragmentManager)
        mainViewPager.offscreenPageLimit = fragmentPages.size //缓存页数
        mainViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                if (menuItem != null) {
                    menuItem!!.isChecked = false
                } else {
                    bottomNavigation.menu.getItem(0).isChecked = false
                }
                menuItem = bottomNavigation.menu.getItem(position)
                menuItem!!.isChecked = true
            }
        })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - clickTime > 2000) {
                "再按一次退出程序".show(this)
                clickTime = System.currentTimeMillis()
                true
            } else {
                super.onKeyDown(keyCode, event)
            }
        } else super.onKeyDown(keyCode, event)
    }
}