package com.rdiot.yx485.ui.bind

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.viewpager.widget.ViewPager
import com.drake.net.utils.scope
import com.google.android.material.tabs.TabLayout
import com.lindroy.iosdialog.IAlertDialog
import com.rdiot.yx485.R
import com.rdiot.yx485.adapter.SelectDeviceTypeAdapter
import com.rdiot.yx485.adapter.SelectDeviceTypeAdapter.OnItemClickListener
import com.rdiot.yx485.base.BaseFragment
import com.rdiot.yx485.base.BindModelFactory
import com.rdiot.yx485.bean.DeviceTypeData
import com.rdiot.yx485.databinding.FraSelectDeviceTypeBinding
import com.rdiot.yx485.net.DeviceCode
import com.rdiot.yx485.net.getDeviceTypeList
import com.rdiot.yx485.ui.bind.model.BindViewModel
import com.rdiot.yx485.util.RoomUtils

/**
 * 选择设备类型页
 */
class SelectDeviceTypeFragment : BaseFragment<FraSelectDeviceTypeBinding>() {
    override fun getLayoutId() = R.layout.fra_select_device_type
    private val bindViewModel: BindViewModel by activityViewModels { BindModelFactory() }
    private lateinit var pagerAdapter: SelectDeviceTypeAdapter
    private lateinit var list: ArrayList<DeviceTypeData>
    private var dialog: IAlertDialog? = null

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) {
        binding.vm = bindViewModel
        initTab()
    }

    private fun initTab() {
        list = ArrayList()
        pagerAdapter = SelectDeviceTypeAdapter(context, list, object : OnItemClickListener {
            override fun onClick(position: Int, deviceTypeData: DeviceTypeData?) {
//                if ((deviceTypeData?.code == DeviceCode.WATER_INTELLIGENCE.typeCode)){
//                    showNoticeDialog()
//                    return
//                }
                bindViewModel.deviceTypeId.set(deviceTypeData?.recordId)
                bindViewModel.isBindSub = !RoomUtils.isMaster(deviceTypeData?.code)
                bindViewModel.isBindInfrared = RoomUtils.isInfrared(deviceTypeData?.code)
                bindViewModel.code = deviceTypeData?.code.toString()
                findNavController().navigate(R.id.action_selectDeviceTypeFragment_to_resetFragment)
            }
        })
        binding.viewPager.adapter = pagerAdapter
        binding.viewPager.offscreenPageLimit = list.size
        binding.tabLayout.setupWithViewPager(binding.viewPager)

        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })

        scope {
            getDeviceTypeList().await().apply {
                list.clear()
                list.addAll(this)
                pagerAdapter.notifyDataSetChanged()
                for (i in list.indices) { // 循环添加自定义的tab
                    val tab: TabLayout.Tab? = binding.tabLayout.getTabAt(i)
                    tab?.text = list[i].name
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        (act as BindActivity).setNBTitle(getString(R.string.select_device_type))
    }

//    override fun onSaveInstanceState(outState: Bundle) {
//        super.onSaveInstanceState(outState)
//        outState.putInt("selected_tab", binding.tabLayout.selectedTabPosition)
//        val titles = ArrayList<String>()
//        list.forEach {
//            titles.add(it.name)
//        }
//        outState.putStringArrayList("tab_titles", titles)
//    }

//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        super.onViewCreated(view, savedInstanceState)
//        savedInstanceState?.getInt("selected_tab", 0).let {
//            if (it != null) {
//                binding.tabLayout.getTabAt(it)?.select()
//            }
//        }
//
//        savedInstanceState?.getStringArrayList("tab_titles")?.forEachIndexed { index, title ->
//            binding.tabLayout.getTabAt(index)?.text = title
//        }
//    }

    private fun showNoticeDialog() {
        dialog?.dismiss()
        dialog = IAlertDialog.build(this)
            .setTitle(R.string.notice)
            .setCancelOutside(false)
            .setShowNegButton(false)
            .setAlpha(1.0F)
            .setPosButtonText(R.string.got_it)
            .setMessage(R.string.coding)

        dialog?.isCancelable = false
        dialog?.show()
    }
}