package com.cr.pages

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import com.cr.common.CrAudioUtil
import com.cr.common.CrUnitManager
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.data.DEFAULT_STR
import com.cr.data.Level
import com.cr.viewmodel.*
import dji.v5.utils.common.ContextUtil

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/8 15:52
 * 描述：大疆顶部信息视图
 */
open class FragmentTopInfo : CrFragment() {
    // define: 2023/3/9 APP注册信息
    private val msdkRegisterVm: CrMSDKRegisterVM by activityViewModels()

    // define: 2023/3/9 MSDK版本信息
    private val msdkInfoVm: CrMSDKInfoVM by activityViewModels()

    // define: 2023/3/9 遥控器信息
    private val remoteControlVm: CrRemoteControlVM by activityViewModels()

    // define: 2023/3/9 飞行器信息
    private val flightControlVm: CrFlightControlVM by activityViewModels()

    // define: 2023/3/14 链路信息
    private val linkVm: CrLinkVM by activityViewModels()

    // define: 2023/3/9 遥控器电量标志符号
    private var remoteControlBatteryIcon: MutableMap<Level, Drawable?> = mutableMapOf(
        Level.LEVEL1 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_rem1),
        Level.LEVEL2 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_rem2),
        Level.LEVEL3 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_rem3),
        Level.LEVEL4 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_rem4),
    )

    // define: 2023/3/9 飞行器电池电量标志符号
    private var flightControlBatteryIcon: MutableMap<Level, Drawable?> = mutableMapOf(
        Level.LEVEL1 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_air1),
        Level.LEVEL2 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_air2),
        Level.LEVEL3 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_air3),
        Level.LEVEL4 to ContextUtil.getContext().getDrawable(R.drawable.dji_battery_air4),
    )

    // define: 2023/3/14 链路质量标志
    private var linkSignalQualityIcon:MutableMap<Level,Drawable?> = mutableMapOf(
        Level.LEVEL1 to ContextUtil.getContext().getDrawable(R.drawable.dji_link_symbol1),
        Level.LEVEL2 to ContextUtil.getContext().getDrawable(R.drawable.dji_link_symbol3),
        Level.LEVEL3 to ContextUtil.getContext().getDrawable(R.drawable.dji_link_symbol4),
        Level.LEVEL4 to ContextUtil.getContext().getDrawable(R.drawable.dji_link_symbol6),
    )

    // todo: 2023/8/17 定义控件
    private var lblSDKVersion:TextView?=null  // define: 2023/9/14 SDK版本信息
    private var lblGPSCount:TextView?= null   // define: 2023/9/14 GPS卫星数量
    private var lblRCConnection:TextView?=null  // define: 2023/9/14 遥控器连接状态
    private var lblProductName:TextView?=null  // define: 2023/9/14 连接产品名称
    private var lblIMUState:TextView?=null   // define: 2023/9/14 IMU状态
    private var lblLongitude:TextView?=null   // define: 2023/9/14 当前经度
    private var lblLatitude:TextView?=null   // define: 2023/9/14 当前纬度
    private var lblSignal:TextView?= null  // define: 2023/9/14 信号质量
    private var imgSignal:ImageView?=null  // define: 2023/9/14 信号质量图
    private var lblRCBattery:TextView?=null  // define: 2023/9/14 遥控器电池电量
    private var imgRCBattery:ImageView?=null   // define: 2023/9/14 遥控器电池电量图
    private var lblFCBattery:TextView?=null   // define: 2023/9/14 飞行器电池电量
    private var imgFCBattery:ImageView?=null   // define: 2023/9/14 飞行器电池电量符号
    private var lblCompassState:TextView?=null  // define: 2023/9/14 指南针状态


    /**
     * 覆写创建视图方法
     * @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_top_info, container, false)
        // todo: 2023/8/17 挂载控件
        joinControls()
        // todo: 2023/8/17 订阅
        initObserver()
        return mainView
    }

    /**
     * 关联控件
     */
    override fun joinControls() {
        mainView?.let {
            lblSDKVersion = it.findViewById(R.id.dji_sdk_version)
            lblGPSCount = it.findViewById(R.id.dji_gps_count)
            lblIMUState = it.findViewById(R.id.dji_imu_state)
            lblRCConnection = it.findViewById(R.id.dji_rc_connection)
            lblProductName = it.findViewById(R.id.dji_product_connection)
            lblLongitude = it.findViewById(R.id.dji_gps_longitude)
            lblLatitude = it.findViewById(R.id.dji_gps_latitude)
            lblSignal = it.findViewById(R.id.dji_link_value)
            imgSignal = it.findViewById(R.id.dji_link_symbol)
            lblRCBattery = it.findViewById(R.id.dji_rc_battery_value)
            imgRCBattery = it.findViewById(R.id.dji_rc_battery_symbol)
            lblFCBattery = it.findViewById(R.id.dji_battery_value)
            imgFCBattery = it.findViewById(R.id.dji_battery_symbol)
            lblCompassState = it.findViewById(R.id.dji_compass)
        }
    }

    /**
     * 覆写视图创建完成方法
     * @param view View
     * @param savedInstanceState Bundle?
     */
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    /**
     * 由后台转前台显示
     */
    override fun onResume() {
        super.onResume()
    }



    /**
     * 初始化订阅
     */
    private fun initObserver() {
        // todo: 2023/3/9 订阅App注册信息
        msdkRegisterVm.registerInfo.observe(viewLifecycleOwner) {
            lblSDKVersion?.text = it.registerInfo
        }
        // todo: 2023/3/9 订阅MSDK版本信息
        msdkInfoVm.msdkInfo.observe(viewLifecycleOwner) {
            if (msdkRegisterVm.registerInfo.value?.isRegister == true) {
                lblSDKVersion?.text = it.SDKVersion
            }
        }
        // todo: 2023/8/17 订阅遥控器
        observerRemoteControl()
        // todo: 2023/8/17 订阅飞行器
        observerFlightControl()
        // todo: 2023/8/17 订阅链路
        observerLink()
    }

    /**
     * 订阅遥控器信息
     */
    private fun observerRemoteControl() {
        // todo: 2023/3/9 订阅遥控器信息
        remoteControlVm.remoteControlInfo.observe(viewLifecycleOwner) {
            it?.let {
                if (msdkRegisterVm.registerInfo.value?.isRegister == true) {
                    lblRCConnection?.text = it.connectionInfo
                    if(it.batteryPercent>0){
                        lblRCBattery?.text =
                            resources.getString(R.string.value_battery_percent, it.batteryPercent)
                    }else{
                        lblRCBattery?.text = DEFAULT_STR
                    }
                    imgRCBattery?.setImageDrawable(
                        remoteControlBatteryIcon[CrUtil.getLevel(
                            it.batteryPercent
                        )]
                    )
                }
            }
        }
    }

    /**
     * 订阅飞行器信息
     */
    private fun observerFlightControl() {
        flightControlVm.flightControlInfo.observe(viewLifecycleOwner) {
            it?.let {
                if (msdkRegisterVm.registerInfo.value?.isRegister == true) {
                    lblProductName?.text = it.productName
                    if (it.batteryPercent > 0) {
                        lblFCBattery?.text =
                            resources.getString(R.string.value_battery_percent, it.batteryPercent)
                    } else {
                        lblFCBattery?.text = DEFAULT_STR
                    }
                    imgFCBattery?.setImageDrawable(flightControlBatteryIcon[CrUtil.getLevel(it.batteryPercent)])
                    lblGPSCount?.text = it.gpsCount
                    lblIMUState?.text = it.imuState
                    lblCompassState?.text = it.compassState
                    // todo: 2023/8/15 更新经纬度
                    lblLongitude?.text = it.longitudeStr
                    lblLatitude?.text = it.latitudeStr
                    if(!it.isConnection){
                        linkVm.reset()
                    }
                    // todo: 2023/9/26 如果点机启动 播放语音
                    if(it.motorsIsOn){
                        //CrAudioUtil.getInstance().play(CrUnitManager.getStringFromResource(R.string.audio_motors_on))
                    }
                }
            }
        }
    }

    /**
     * 订阅链路
     */
    private fun observerLink(){
        linkVm.linkInfo.observe(viewLifecycleOwner){
            if (msdkRegisterVm.registerInfo.value?.isRegister == true) {
                it?.let {
                    imgSignal?.setImageDrawable(linkSignalQualityIcon[CrUtil.getLinkSignalQuality(it.signalQuality)])
                    lblSignal?.text = it.frequencyBand
                }
            }
        }
    }
}