package com.cr.pages

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import com.cr.adapter.SettingAdapter
import com.cr.common.CrSaveManager
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.event.BarAction
import com.cr.event.EventFragmentBarAction
import com.cr.models.SetItemModel
import com.cr.models.SettingAction

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/11 09:10
 * 描述：设置-主页面
 */
class FragmentSetMain : CrNavigationFragment() {
    // define: 2023/4/11 菜单数组
    private var menuList: MutableList<SetItemModel> = mutableListOf()

    // define: 2023/4/11 定义组件
    private var listView: ListView? = null

    // define: 2023/4/11 定义监听器
    private var listener: SettingAdapter.SettingListener? = 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
        // todo: 2023/4/11 创建视图
        mainView = inflater.inflate(R.layout.frag_set_main, null)
        // todo: 2023/4/11 挂载控件
        joinControls()
        // todo: 2023/4/11 初始化菜单
        initMenu()
        return mainView
    }

    /**
     * 初始化菜单
     */
    private fun initMenu() {
        // todo: 2023/4/11 清除
        menuList.clear()
        menuList.add(
            SetItemModel(
                R.drawable.ico_user,
                "登录大疆账号",
                isArrow = false,
                isSwitch = false,
                action = SettingAction.SA_DJI_LOGIN,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.tools_download,
                "数据下载",
                isArrow = true,
                isSwitch = false,
                action = SettingAction.SA_DOWNLOAD_DATA,
                switchIsOn = false
            )
        )
        // TODO: 6/4/21 获取地图居中状态 设置是否开关
        val isCenter: Boolean = CrSaveManager.getInstance().getMapCenter() == true
        menuList.add(
            SetItemModel(
                R.drawable.ico_old_map,
                "飞行器地图居中",
                isArrow = false,
                isSwitch = true,
                action = SettingAction.SA_MAP_CENTER,
                switchIsOn = isCenter
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.tools_set,
                "设置服务器的IP和端口",
                isArrow = true,
                isSwitch = false,
                action = SettingAction.SA_SET_IP_COM,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.dji_aircraft,
                "历史航线",
                isArrow = true,
                isSwitch = false,
                action = SettingAction.SA_OLD_AIRLINE,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.goto_back,
                "重新登录系统",
                isArrow = false,
                isSwitch = false,
                action = SettingAction.SA_RE_LOGIN,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.tools_deleteall,
                "清除地图缓存",
                isArrow = false,
                isSwitch = false,
                action = SettingAction.SA_DEL_MAP_CACHE,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.tools_mession,
                "查看飞行记录",
                isArrow = true,
                isSwitch = false,
                action = SettingAction.SA_SHOW_DJI_LOG,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.tools_upload,
                "上传断网航点",
                isArrow = false,
                isSwitch = false,
                action = SettingAction.SA_UPLOAD_NO_UP_AIR_POINT,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.ico_multimage,
                "删除取证数据",
                isArrow = false,
                isSwitch = false,
                action = SettingAction.SA_DEL_QZ_DATA,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.ty_save,
                "删除航线数据",
                isArrow = false,
                isSwitch = false,
                action = SettingAction.SA_DEL_LINE_DATA,
                switchIsOn = false
            )
        )
        menuList.add(
            SetItemModel(
                R.drawable.ty_delete,
                "删除资源数据",
                isArrow = false,
                isSwitch = false,
                action = SettingAction.SA_DEL_RESOURCE_DATA,
                switchIsOn = false
            )
        )
        // todo: 2023/4/11 设置适配器
        var adapter = SettingAdapter(context, menuList)
        adapter.listener = listener
        listView?.adapter = adapter
    }

    /**
     * 重写挂载控件方法
     */
    override fun joinControls() {
        // todo: 2023/4/11 挂载列表
        listView = mainView?.findViewById(R.id.frm_set_main_listview)
    }


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

    /**
     * 设置回调监听
     * @param listener SettingListener
     */
    fun setCallback(listener: SettingAdapter.SettingListener){
        this.listener = listener
    }
}