package com.cr.cruav

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels

import androidx.fragment.app.commit
import com.cr.data.CrConfig
import com.cr.data.CrUtil
import com.cr.event.EventFragmentBarAction
import com.cr.map.EventMap
import com.cr.map.MapAction
import com.cr.pages.*
import com.cr.viewmodel.*
import com.squareup.otto.Subscribe
import dji.v5.common.error.IDJIError
import dji.v5.common.register.DJISDKInitEvent
import dji.v5.manager.interfaces.SDKManagerCallback
import dji.v5.utils.common.ToastUtils
import kotlinx.android.synthetic.main.av_main.*
import kotlinx.android.synthetic.main.tools_top.*

class AvMain : CrActivity(), View.OnClickListener {
    // define: 2023/3/10 将App视图绑定到模型
    private val msdkRegisterVm: CrMSDKRegisterVM by viewModels()

    // define: 2023/3/10 将MSDK视图绑定到模型
    private val msdkInfoVm: CrMSDKInfoVM by viewModels()

    // define: 2023/3/10 将遥控器视图绑定到模型
    private val remoteControlVm: CrRemoteControlVM by viewModels()

    // define: 2023/3/10 将飞行器视图绑定到模型
    private val flightControlVm: CrFlightControlVM by viewModels()

    // define: 2023/3/14 将链路视图绑定到模型
    private val linkVm: CrLinkVM by viewModels()

    // define: 2023/3/13 顶部视图容器
    private var fragmentTopInfo: FragmentTopInfo? = null

    // define: 2023/3/13 图传容器
    private var fragmentFPV: FragmentFPV? = null

    // define: 2023/3/14 地图容器
    private var fragmentMap: FragmentMap? = null

    // define: 2023/3/14 图传窗口是不是小窗口
    private var isFpvSmallWindow: Boolean = true

    // define: 2023/4/11 初始化设置页面
    private var fragmentSet: FragmentSet? = null

    // define: 2023/4/14 图层控制页面
    private var fragmentLayerControl: FragmentLayerControl? = null

    // define: 2023/4/17 涂鸦页面
    private var fragmentDoodle: FragmentDoodle? = null


    /**
     * 入口函数
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.av_main);
        // todo: 2023/4/11 挂载控件
        joinControls()
        // todo: 2023/4/11 初始化
//        init()
        // todo: 2023/4/3 订阅事件
        CrApplication.getEventBus().register(this)
    }

    /**
     * 挂载控件
     */
    override fun joinControls() {
        // todo: 2023/4/11 设置点击事件
        tools_set.setOnClickListener(this)
        tools_layer.setOnClickListener(this)
        tools_doodle.setOnClickListener(this)
        tools_ico.setOnClickListener(this)
        tools_wx.setOnClickListener(this)
        tools_tools.setOnClickListener(this)
    }

    /**
     * 初始化
     */
    private fun init() {
        // todo: 2023/3/11 注册App到DJI
        registerApp();
        // todo: 2023/3/11 初始化视图
        initView()
        // todo: 2023/4/10 初始化页面
        initPage()
    }

    /**
     * 初始化页面
     */
    private fun initPage() {
        // todo: 2023/4/10 显示登录账号
        av_main_lbl_user.text = CrConfig.user?.userName
        // todo: 2023/4/11 初始化设置页面
        fragmentSet = FragmentSet()
        fragmentSet?.let {
            addFragment(it, R.id.av_frm_right_panel)
            hideFragment(it)
        }
        // todo: 2023/4/14 初始化图层控制页面
        fragmentLayerControl = FragmentLayerControl()
        fragmentLayerControl?.let {
            addFragment(it, R.id.av_frm_right_panel)
            hideFragment(it)
        }
        // todo: 2023/4/17 初始化涂鸦操作页面
        fragmentDoodle = FragmentDoodle()
        fragmentDoodle?.setListener(doodleListener)
        fragmentDoodle?.let {
            addFragment(it,R.id.av_frm_left_panel)
            hideFragment(it)
        }
    }

    /**
     * 初始化视图
     */
    @SuppressLint("SuspiciousIndentation")
    private fun initView() {
        // todo: 2023/3/10 加入大疆顶部视图
        fragmentTopInfo = FragmentTopInfo()
        supportFragmentManager.commit {
            replace(R.id.av_fragment_top, fragmentTopInfo!!)
        }
        // todo: 2023/3/10 加入图传视图
        fragmentFPV = FragmentFPV()
        fragmentFPV?.listener = object : FragmentFPV.FPVListener {
            // todo: 2023/3/13 切换图传窗口
            override fun triggerWindow() {
                CrUtil.print("切换图传窗口")
                av_fragment_fpv.removeAllViews()
                av_fragment_map.removeAllViews()
                if (isFpvSmallWindow) {
                    isFpvSmallWindow = false
                    av_fragment_map.addView(fragmentFPV!!.view)
                    av_fragment_fpv.addView(fragmentMap!!.view)
                } else {
                    isFpvSmallWindow = true
                    av_fragment_map.addView(fragmentMap!!.view)
                    av_fragment_fpv.addView(fragmentFPV!!.view)
                }

            }
        }
        supportFragmentManager.commit {
            replace(R.id.av_fragment_fpv, fragmentFPV!!)
        }
        // todo: 2023/3/14 加入地图视图
        fragmentMap = FragmentMap()
        supportFragmentManager.commit {
            replace(R.id.av_fragment_map, fragmentMap!!)
        }

    }

    /**
     * 注册App SDK
     */
    private fun registerApp() {
        msdkRegisterVm.registerApp(this, object : SDKManagerCallback {
            // todo: 2023/3/13 App注册成功回调
            override fun onRegisterSuccess() {
                CrUtil.print("App注册成功")
                // todo: 2023/3/9 MSDK初始化监听
                msdkInfoVm.initListener()
                // todo: 2023/3/9 遥控器初始化监听
                remoteControlVm.initListener()
                // todo: 2023/3/9 初始化飞行器监听
                flightControlVm.initListener()
                // todo: 2023/3/14 初始化链路监听
                linkVm.initListener()
            }

            // todo: 2023/3/13 App注册失败回调
            override fun onRegisterFailure(error: IDJIError?) {
                CrUtil.print("App注册失败" + error.toString())
            }

            override fun onProductDisconnect(productId: Int) {
                ToastUtils.showToast("Not yet implemented")
            }

            override fun onProductConnect(productId: Int) {
                ToastUtils.showToast("Not yet implemented")
            }

            override fun onProductChanged(productId: Int) {
                ToastUtils.showToast("Not yet implemented")
            }

            override fun onInitProcess(event: DJISDKInitEvent?, totalProcess: Int) {
                ToastUtils.showToast("Not yet implemented")
            }

            override fun onDatabaseDownloadProgress(current: Long, total: Long) {
                ToastUtils.showToast("Not yet implemented")
            }
        })
    }

    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.tools_set -> {
                showFragment(fragmentSet!!)
            }
            R.id.tools_layer -> {
                showFragment(fragmentLayerControl!!)
                fragmentLayerControl?.initPage()
            }
            R.id.tools_doodle->{
                showFragment(fragmentDoodle!!)
            }
        }
    }

    /**
     * 涂鸦页面操作监听
     */
    private val doodleListener = object:FragmentDoodle.OnOperationListener{
        // todo: 2023/4/17 开启绘制
        override fun onStartDraw() {
            CrApplication.getEventBus().post(EventMap(MapAction.MapTapDrawDoodle))
        }

        // todo: 2023/4/17 停止绘制
        override fun onStopDraw() {
            CrApplication.getEventBus().post(EventMap(MapAction.EventStopSketch))
        }

        // todo: 2023/4/17 开启选择
        override fun onSelect() {
            CrApplication.getEventBus().post(EventMap(MapAction.MapTapSelectDoodle))
        }

        // todo: 2023/4/17 关闭选择
        override fun onStopSelect() {
            CrApplication.getEventBus().post(EventMap(MapAction.EventStopTouch))
        }

        // todo: 2023/4/17 清除
        override fun onClear() {
            CrApplication.getEventBus().post(EventMap(MapAction.EventDoodleClear))
        }

        // todo: 2023/4/17 保存
        override fun onSave() {
            CrApplication.getEventBus().post(EventMap(MapAction.EventDoodleSave))
        }

        // todo: 2023/4/17 删除全部
        override fun onRemove() {
            CrApplication.getEventBus().post(EventMap(MapAction.EventDoodleRemove))
        }

        // todo: 2023/4/17 移除选择
        override fun onDelete() {
            CrApplication.getEventBus().post(EventMap(MapAction.EventDoodleDelete))
        }

    }

    /**
     * 销毁
     */
    override fun onDestroy() {
        CrApplication.getEventBus().unregister(this)
        super.onDestroy()
    }

    /**
     * App可见
     */
    override fun onResume() {
        super.onResume()
        init()
    }


    /**
     * 订阅Fragment管理事件
     * @param event EventFragmentBarAction
     */
    @Subscribe
    fun onFragmentBar(event: EventFragmentBarAction) {
        hideFragment(event.fragment!!)
    }
}
