package com.cr.pages

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ListView
import android.widget.TextView
import com.cr.adapter.CrWaypointDetailAdapter
import com.cr.common.CrInterfaceManager
import com.cr.common.DataManager
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.data.CrConfig
import com.cr.data.CrUtil
import com.cr.models.AircraftWaypointModel
import com.cr.models.CompletionModel
import com.cr.models.ICompletion

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/9/19 10:02
 * 描述：航点管理器页面
 */
class FragmentAirLineManager : CrNavigationFragment() ,View.OnClickListener{
    // todo: 2023/9/19 控件定义
    private var lblAllCount: TextView? = null // define: 2023/9/19 航点总数
    private var lblYesCount: TextView? = null // define: 2023/9/19 已上传航点数
    private var lblNoCount: TextView? = null  // define: 2023/9/19 未上传航点数
    private var listView: ListView? = null // define: 2023/9/19 待上传航点统计信息
    private var btnUpdate: Button? = null // define: 2023/9/19 上传航点
    private var btnDelete:Button?=null // define: 2023/9/19 删除航点

    /**
     * 初始化
     * @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_air_line_manager, null)
        // todo: 2023/9/19 挂载控件
        joinControls()
        // todo: 2023/9/19 初始化页面
        initPage()
        return mainView
    }

    /**
     * 覆写初始化页面
     */
    override fun initPage() {
        // todo: 2023/9/19 显示统计信息
        var countInfo = DataManager.queryAircraftStatisticsInfo(CrConfig.user!!.userId!!)
        lblAllCount?.text = "${countInfo.waypointAllCount}个"
        lblYesCount?.text = "${countInfo.yesUploadWaypointCount}个"
        lblNoCount?.text = "${countInfo.waitUploadWaypointCount}个"
        // todo: 2023/9/19 更新待上传航点视图
        var adapter = CrWaypointDetailAdapter(CrApplication.getContext(), countInfo.waypointList!!)
        listView?.adapter = adapter
    }

    /**
     * 覆写挂载控件
     */
    override fun joinControls() {
        mainView?.let {
            // todo: 2023/9/19 挂载统计信息控件
            lblAllCount = it.findViewById(R.id.lbl_all_count)
            lblYesCount = it.findViewById(R.id.lbl_yes_count)
            lblNoCount = it.findViewById(R.id.lbl_no_count)
            // todo: 2023/9/19 挂载明细视图
            listView = it.findViewById(R.id.list_view)
            // todo: 2023/9/19 挂载按钮
            btnUpdate = it.findViewById(R.id.btn_update)
            btnUpdate?.setOnClickListener(this)
            btnDelete = it.findViewById(R.id.btn_delete)
            btnDelete?.setOnClickListener(this)
        }
    }

    /**
     * 覆写视图点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when(view?.id){
            R.id.btn_update->{
                // todo: 2023/9/20 先查询待上传航点数据
                var waitWaypointList = DataManager.queryAllWaitUploadWaypoint(CrConfig.user!!.userId!!)
                if(waitWaypointList == null || waitWaypointList.isEmpty()){
                    showWarning("未查询到需要上传的航点！")
                    return
                }
                // todo: 2023/9/19 上传航点
                showConfirm("确定上传所有待上传航点吗？", arrayOf("上传","取消"),object:ICompletion<String>{
                    // todo: 2023/9/19 回调
                    override fun onCompletion(completion: CompletionModel<String>) {
                        if(completion.isSuccess){
                            CrInterfaceManager.getInstance().uploadAirlineWaypoint(waitWaypointList,object:ICompletion<List<AircraftWaypointModel>>{
                                // todo: 2023/9/20 执行成功回调
                                override fun onCompletion(completion: CompletionModel<List<AircraftWaypointModel>>) {
                                    if (completion.result!!.isNotEmpty()){
                                        // todo: 2023/9/20 更新航点存储状态
                                        DataManager.updateWaypointState(CrConfig.user!!.userId!!,completion.result!!)
                                        mainHandler.post { initPage() }
                                        showInformation("成功上传航点${completion.result!!.size}个！！")
                                    }else{
                                        showWarning("航点未全部上传成功！")
                                    }
                                }
                            })
                        }
                    }
                })
            }
            R.id.btn_delete->{
                // todo: 2023/9/19 删除航点
                showConfirm("删除后无法恢复，确定要删除吗？", arrayOf("删除","取消"),object:ICompletion<String>{
                    // todo: 2023/9/19 回调
                    override fun onCompletion(completion: CompletionModel<String>) {
                        if(completion.isSuccess){
                            deleteAllWaypoint()
                        }
                    }
                })
            }
        }
    }

    /**
     * 删除全部航点数据
     */
    private fun deleteAllWaypoint(){
        DataManager.deleteAircraftPoint(CrConfig.user!!.userId!!)
        initPage()
    }
}