package com.cr.pages

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.contrarywind.listener.OnItemSelectedListener
import com.cr.common.DataManager
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.dialog.DialogNormal
import com.cr.event.BarAction
import com.cr.event.EventFragmentBarAction
import com.cr.models.IPAndComModel
import com.cr.view.CrViewWheel
import com.cr.widget.CrEditTextWidget
import dji.v5.utils.common.ContextUtil

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/30 13:44
 * 描述：设置IP和COM
 */
class FragmentSetIpAndCom : CrNavigationFragment(),View.OnClickListener{
    // define: 2023/3/30 选择组件
    var wheelSelectIpAndCom: CrViewWheel? = null
    var lblIp: CrEditTextWidget? = null
    var lblCom: CrEditTextWidget? = null
    var lblServerName: CrEditTextWidget? = null
    var btnSetting:Button? = null

    // define: 2023/4/3 变量定义
    var networkLinks:List<IPAndComModel> = mutableListOf()

    /**
     * 创建视图
     * @param inflater LayoutInflater
     * @param container ViewGroup?
     * @param savedInstanceState Bundle?
     * @return View?
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        self = this
        mainView = inflater.inflate(R.layout.frag_set_ip_and_com, null)
        // todo: 2023/3/30 设置导航栏
        setBar(R.id.nv)
        setBarVisible(isVisible = false)
        // todo: 2023/3/30 关联组件
        joinControls()
        // todo: 2023/3/30 初始化显示
        initPage()
        return mainView
    }

    /**
     * 挂接控件
     */
    override fun joinControls() {
        // todo: 2023/3/30 挂接选择组件
        wheelSelectIpAndCom = mainView!!.findViewById(R.id.frm_ic_view_list)
        // todo: 2023/4/3 挂接展示组件
        lblIp = mainView!!.findViewById(R.id.frm_ic_txt_ip)
        lblCom = mainView!!.findViewById(R.id.frm_ic_txt_com)
        lblServerName = mainView!!.findViewById(R.id.frm_ic_txt_server)
        btnSetting = mainView!!.findViewById(R.id.frm_ic_btn_set)
        btnSetting?.setOnClickListener(this)

        // todo: 2023/3/31 最后调用超类的关联方法 以便初始化控件
        super.joinControls()
    }

    /**
     * 初始化控件
     */
    override fun initControls() {
        super.initControls()
        // todo: 2023/3/30 服务信息选择滑动组件初始化
        wheelSelectIpAndCom?.setTextColorCenter(Color.YELLOW) // TODO: 4/17/21 设置选中项颜色
        wheelSelectIpAndCom?.setItemsVisibleCount(8)
        wheelSelectIpAndCom?.setTextSize(CrUtil.getDimens(R.dimen.sp_6))
        wheelSelectIpAndCom?.setTypeface(CrUtil.getFont(ContextUtil.getContext()))
        wheelSelectIpAndCom?.setCyclic(false) // TODO: 6/9/21 禁止循环
        wheelSelectIpAndCom?.setOnItemSelectedListener(onItemSelectedListener)

        // todo: 2023/4/3 初始化网络连接信息
        var nLinkInfo: IPAndComModel = DataManager.getNetworkLinkInfo()
        setLinkView(nLinkInfo)
    }

    /**
     * 初始化页面
     */
    override fun initPage() {
        super.initPage()
        networkLinks = DataManager.getNetworkLinks()
        var strList = arrayListOf<String>()
        var selIndex:Int = 0
        for(index in networkLinks.indices){
            var link = networkLinks[index]
            strList.add(link!!.toString())
            if(link.isSelect) selIndex = index
        }
        wheelSelectIpAndCom!!.adapter = ArrayWheelAdapter(strList)
        wheelSelectIpAndCom!!.currentItem = selIndex
    }

    /**
     * 设置显示连接信息
     * @param link IPAndComModel
     */
    private fun setLinkView(link:IPAndComModel){
        link.let {
            lblIp?.setContent(it.ip)
            lblCom?.setContent(it.com)
            lblServerName?.setContent(it.serverName)
        }
    }

    /**
     * 滑动选择器的滑动选择事件
     */
    private var onItemSelectedListener: OnItemSelectedListener = OnItemSelectedListener {
        setLinkView(networkLinks[it])
    }

    /**
     * 设置连接
     */
    private fun setLink(){
        // todo: 2023/4/3 先进行检测
        if (lblIp?.getContent().equals("") || !CrUtil.checkIP(lblIp?.getContent())!!){
            DialogNormal(context!!,"警告","Ip地址不符合要求！").show()
            return
        }
        if (lblCom?.getContent().equals("") || !CrUtil.checkCOM(lblCom?.getContent())!!){
            DialogNormal(context!!,"警告","端口不符合要求！").show()
            return
        }
        if (lblServerName?.getContent().equals("")){
            DialogNormal(context!!,"警告","服务名不符合要求！").show()
            return
        }
        var link = IPAndComModel(lblIp!!.getContent(),lblCom!!.getContent(),lblServerName!!.getContent())
        // todo: 2023/4/3 查询
        var isExists:Boolean = DataManager.queryNetworkLink(link)
        if(isExists){
            // todo: 2023/4/3 存在则更新
            var isReset:Boolean = DataManager.cancelNetworkChecked()
            var isUpdate:Boolean = DataManager.updateNetworkChecked(link,true)
            DialogNormal(context!!,"提示",if(isReset && isUpdate) "设置成功" else "设置失败").show()
        }else{
            // todo: 2023/4/3 不存在 则追加
            var isReset:Boolean = DataManager.cancelNetworkChecked()
            var isInsert:Boolean = DataManager.insertNetworkLink(link)
            DialogNormal(context!!,"提示",if(isReset && isInsert) "设置成功" else "设置失败").show()
        }
        // todo: 2023/4/3 重置页面
        initPage()
    }

    /**
     * 点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when(view?.id){
            R.id.frm_ic_btn_set->{
                setLink()
            }
        }
    }

    /**
     * 重写导航栏回退事件
     */
    override fun gotoBack() {
        CrApplication.getEventBus().post(EventFragmentBarAction(self!!,BarAction.ACTION_BACK))
    }

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