package com.example.makcar

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.makcar.databinding.ActivityMainBinding
import dji.sdk.keyvalue.value.common.ComponentIndexType
import dji.sdk.keyvalue.value.common.LocationCoordinate3D
import dji.v5.common.callback.CommonCallbacks
import dji.v5.common.error.IDJIError
import dji.v5.manager.intelligent.IMissionCapabilityListener
import dji.v5.manager.intelligent.IMissionInfoListener
import dji.v5.manager.intelligent.IntelligentFlightManager
import dji.v5.manager.intelligent.poi.POICapability
import dji.v5.manager.intelligent.poi.POIInfo
import dji.v5.manager.intelligent.poi.POIParam

import dji.v5.manager.intelligent.poi.POITarget




class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        //设置视频源
        binding.fpvWidget.updateVideoSource(ComponentIndexType.FPV)

        //环绕
        binding.bthuanrao.setOnClickListener {
            huanrao()
        }
        //识别降落
        binding.bttakedown.setOnClickListener {
            takedown()
        }
        //切换
        binding.swOrbit.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                Log.i("My","true")
                huanrao()
            } else {
                Log.i("My","false")
                stophuanrao()
            }
        }



    }



    //Poi需要用到的监听器
    val listenermissioninfo = object : IMissionInfoListener<POIInfo, POITarget> {
        override fun onMissionInfoUpdate(info: POIInfo) {
            Log.i("My", "半径=${info.radius}")
        }
        override fun onMissionTargetUpdate(target: POITarget) {
            Log.i("My", "位置=${target.targetLocation}")
        }
    }
    val listenermissioncapability = object : IMissionCapabilityListener<POICapability> {
        override fun onMissionCapabilityUpdate(capability: POICapability) {
            Log.i("My", "capability=${capability.supportedTargetTypes}")
        }
    }
    //环绕的方法
    private fun huanrao() {
        //参数
        var param= POIParam().apply {
            circleSpeed=10.0
        }
        //目标
        var poitarget=POITarget().apply {
            index=1
            targetLocation=LocationCoordinate3D().apply {
                latitude=31.03
                longitude=120.78
                altitude=50.0
            }
        }
        IntelligentFlightManager.getInstance().poiMissionManager.addMissionCapabilityListener(listenermissioncapability)
        IntelligentFlightManager.getInstance().poiMissionManager.addMissionInfoListener(listenermissioninfo)
        IntelligentFlightManager.getInstance().poiMissionManager.startMission(poitarget,param,object:CommonCallbacks.CompletionCallback{
            override fun onSuccess() {
                //
                Log.i("My","启动环绕任务")
            }

            override fun onFailure(p0: IDJIError) {
                //
                Log.i("My","启动环绕任务失败")
            }
        })

//锁定这个速度
//        IntelligentFlightManager.getInstance().poiMissionManager.lockCircularVelocity(true,object:CommonCallbacks.CompletionCallback{
//            override fun onSuccess() {
//                Log.i("My","锁定成功")
//            }
//            override fun onFailure(p0: IDJIError) {
//                Log.i("My","锁定失败")
//            }
//        })


    }

    private fun stophuanrao() {
        IntelligentFlightManager.getInstance().poiMissionManager.removeMissionCapabilityListener(listenermissioncapability)
        IntelligentFlightManager.getInstance().poiMissionManager.removeMissionInfoListener(listenermissioninfo)

        IntelligentFlightManager.getInstance().poiMissionManager.stopMission(object:CommonCallbacks.CompletionCallback{
            override fun onSuccess() {
                Log.i( "My","成功暂停环绕任务")
            }
            override fun onFailure(p0: IDJIError) {
                Log.i( "My","暂停环绕任务失败")
            }
        })
    }


    //识别降落
    private fun  takedown(){

    }


}


