package com.cr.pages

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.cr.adapter.SettingAdapter
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.event.BarAction
import com.cr.event.EventFragmentBarAction
import com.cr.models.SettingAction

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/10 17:45
 * 描述：设置管理页面
 */
class FragmentSet : CrNavigationFragment() {
    // define: 2023/4/11 定义变量
    private var adapter:CrPageAdapter? = null

    // define: 2023/4/11 定义页面
    private var pageMain:FragmentSetMain? = null
    private var pageSetIpAndCom:FragmentSetIpAndCom? = null
    private var pageDownloadData:FragmentSetDataDownload? = null

    // define: 2023/4/11 定义控件
    private var viewPager: ViewPager2?=null
    /**
     * 创建视图
     * @param inflater LayoutInflater 视图管理器
     * @param container ViewGroup? 视图组
     * @param savedInstanceState Bundle?
     * @return View?
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // todo: 2023/4/11 设置动画方向
        setAnimationDirection(AnimationDirection.RIGHT)
        self = this
        mainView = inflater.inflate(R.layout.frag_set,null)
        // todo: 2023/4/10 设置导航栏
        setBar(R.id.nv)
        // todo: 2023/4/11 挂载控件
        joinControls()
        // todo: 2023/4/11 初始化页面
        initPages()
        return mainView
    }

    /**
     * 初始化导航栏
     */
    private fun initBar(){
        nvBar?.crSetTitle(R.string.nv_title_set)
        nvBar?.crSetVisible(backIsVisible = false, dismissIsVisible = true)
        showPage(pageMain!!)
    }

    /**
     * 初始化页面
     */
    private fun initPages(){
        adapter = CrPageAdapter(this.activity!!)
        // todo: 2023/4/11 初始化主页面
        pageMain = FragmentSetMain()
        pageMain?.setCallback(mainListener)
        adapter?.addFragment(pageMain!!)

        // todo: 2023/4/11 初始化设置Ip和Com页面
        pageSetIpAndCom = FragmentSetIpAndCom()
        pageSetIpAndCom?.setAnimationDirection(AnimationDirection.RIGHT)
        adapter?.addFragment(pageSetIpAndCom!!)

        // todo: 2023/4/11 初始化数据下载页面
        pageDownloadData = FragmentSetDataDownload()
        adapter?.addFragment(pageDownloadData!!)

        // todo: 2023/4/11 设置监听
        viewPager?.adapter = adapter

        // todo: 2023/4/11 初始化导航栏和页面
        initBar()
    }

    /**
     * 显示页面
     * @param fragment CrFragment
     */
    private fun showPage(fragment: CrNavigationFragment){
        var fragments = adapter?.getFragments()
        for(i in 0 until fragments!!.size){
            if(fragments?.get(i) == fragment){
                fragment.nvBar?.visibility = View.GONE
                viewPager?.currentItem = i
//                fragment?.initPage()
                break
            }
        }

    }

    /**
     * 菜单点击监听
     */
    private var mainListener =  object:SettingAdapter.SettingListener{
        // todo: 2023/4/11 动作监听
        override fun onAction(action: SettingAction) {
            when(action){
                SettingAction.SA_SET_IP_COM->{
                    showPage(pageSetIpAndCom!!)
                    nvBar?.crSetVisible(backIsVisible = true, dismissIsVisible = false)
                    nvBar?.crSetTitle(R.string.nv_title_set_ip_and_com)
                }
                SettingAction.SA_DOWNLOAD_DATA->{
                    showPage(pageDownloadData!!)
                    nvBar?.crSetVisible(backIsVisible = true, dismissIsVisible = false)
                    nvBar?.crSetTitle(R.string.nv_title_download)
                }
            }
        }

        // todo: 2023/4/11 开关监听
        override fun onSwitch(action: SettingAction, isChecked: Boolean) {

        }

    }

    /**
     * 页面适配器
     * @constructor
     */
    private class CrPageAdapter@JvmOverloads constructor(
        activity:FragmentActivity
    ): FragmentStateAdapter(activity){
        private val mFragmentList: MutableList<CrNavigationFragment> = ArrayList()

        /**
         * 添加页面
         * @param fragment CrFragment
         */
        fun addFragment(fragment:CrNavigationFragment){
            mFragmentList.add(fragment)
        }

        /**
         * 获取加载的页面
         * @return MutableList<Fragment>
         */
        fun getFragments(): MutableList<CrNavigationFragment> {
            return mFragmentList
        }

        // todo: 2023/4/11 页面数量
        override fun getItemCount(): Int {
            return mFragmentList.size
        }

        // todo: 2023/4/11 当前页面
        override fun createFragment(position: Int): Fragment {
            return mFragmentList[position]
        }

    }

    /**
     * 挂载控件
     */
    override fun joinControls() {
        viewPager = mainView?.findViewById(R.id.frag_set_pages)
        viewPager?.isUserInputEnabled = false
    }

    /**
     * 重写关闭事件
     */
    override fun dismiss() {
        CrApplication.getEventBus().post(EventFragmentBarAction(self!!,BarAction.ACTION_DISMISS))
    }

    /**
     * 重写回退事件
     */
    override fun onGotoBack() {
        initBar()
    }
}