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 androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
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
import kotlinx.android.synthetic.main.frag_top_info.*

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/8 15:52
 * 描述：大疆顶部信息视图
 */
open class FragmentTopInfo : Fragment() {
    // 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),
    )

    /**
     * 覆写创建视图方法
     * @param inflater LayoutInflater
     * @param container ViewGroup?
     * @param savedInstanceState Bundle?
     * @return View?
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.frag_top_info, container, false)
    }

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

    /**
     * 由后台转前台显示
     */
    override fun onResume() {
        super.onResume()
        // todo: 2023/3/9 初始化订阅
        initObserver()
    }

    /**
     * 初始化订阅
     */
    private fun initObserver() {
        // todo: 2023/3/9 订阅App注册信息
        msdkRegisterVm.registerInfo.observe(requireActivity()) {
            dji_sdk_version.text = it.registerInfo
        }
        // todo: 2023/3/9 订阅MSDK版本信息
        msdkInfoVm.msdkInfo.observe(requireActivity()) {
            if (msdkRegisterVm.registerInfo.value?.isRegister == true) {
                dji_sdk_version.text = it.SDKVersion
            }
        }

        observerRemoteControl()
        observerFlightControl()
        observerLink()

    }

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

    /**
     * 订阅飞行器信息
     */
    private fun observerFlightControl() {
        flightControlVm.flightControlInfo.observe(requireActivity()) {
            it?.let {
                if (msdkRegisterVm.registerInfo.value?.isRegister == true) {
                    dji_product_connection.text = it.productName
                    if (it.batteryPercent > 0) {
                        dji_battery_value.text =
                            resources.getString(R.string.value_battery_percent, it.batteryPercent)
                    } else {
                        dji_battery_value.text = DEFAULT_STR
                    }
                    dji_battery_symbol.setImageDrawable(flightControlBatteryIcon[CrUtil.getLevel(it.batteryPercent)])
                    dji_gps_count.text = it.gpsCount
                    dji_imu_state.text = it.imuState
                    dji_compass.text = it.compassState
                    if(!it.isConnection){
                        linkVm.reset()
                    }
                }
            }
        }
    }

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