package com.casic.br.view

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.extensions.initLayoutImmersionBar
import com.casic.br.fragment.add.DeviceTypeFragment
import com.casic.br.fragment.add.NearbyDeviceFragment
import com.casic.br.fragment.add.ScanDeviceFragment
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import kotlinx.android.synthetic.main.activity_add_device_tab.*
import kotlinx.android.synthetic.main.include_left_back_title.*

/**
 * 添加非IoT设备
 * */
class AddDeviceTabActivity : KotlinBaseActivity() {

    private var menuItem: MenuItem? = null
    private var fragmentPages: ArrayList<Fragment> = ArrayList()

    init {
        fragmentPages.add(ScanDeviceFragment())
        fragmentPages.add(DeviceTypeFragment())
        fragmentPages.add(NearbyDeviceFragment())
    }

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

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        initLayoutImmersionBar(rootView)
        leftBackView.setOnClickListener { finish() }
        titleView.text = "扫码添加设备"
    }

    override fun initData() {

    }

    override fun initEvent() {
        bottomNavigation.setOnItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.nav_scan -> {
                    addDeviceViewPager.currentItem = 0
                    titleView.text = "扫码添加设备"
                }
                R.id.nav_type -> {
                    addDeviceViewPager.currentItem = 1
                    titleView.text = "按型号添加设备"
                }
                R.id.nav_nearby -> {
                    addDeviceViewPager.currentItem = 2
                    titleView.text = "附近设备"
                }
            }
            false
        }
        addDeviceViewPager.adapter = ViewPagerAdapter(fragmentPages, supportFragmentManager)
        addDeviceViewPager.offscreenPageLimit = fragmentPages.size //缓存页数
        addDeviceViewPager.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
            }
        })
    }
}