package com.cr.viewmodel

import androidx.lifecycle.MutableLiveData
import com.cr.common.CrCameraInfo
import com.cr.common.CrMediaInfo
import com.cr.common.CrUnitManager
import com.cr.data.CrUtil
import com.cr.data.DEFAULT_STR
import com.cr.data.DEFAULT_TIME
import dji.sdk.keyvalue.key.CameraKey
import dji.sdk.keyvalue.value.camera.CameraMode
import dji.sdk.keyvalue.value.camera.CameraStorageInfo
import dji.sdk.keyvalue.value.camera.MediaFileType
import dji.v5.et.create
import dji.v5.et.listen
import okhttp3.internal.notifyAll
import java.text.SimpleDateFormat
import java.time.format.DateTimeFormatter

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/8/2 10:10
 * 描述：相机模型
 */
class CrCameraVM : CrViewModel() {
    // define: 2023/8/2 相机信息
    var cameraInfo = MutableLiveData<CrCameraInfo>()

    // define: 2023/8/3 媒体信息
    var mediaInfo = MutableLiveData<CrMediaInfo>()

    /**
     * 初始化
     */
    init {
        // todo: 2023/8/3 初始化相机信息
        cameraInfo.postValue(CrCameraInfo())
        cameraInfo.value?.let {
            it.isConnection = false
            it.iso = DEFAULT_STR
            it.speed = DEFAULT_STR
            it.focal = DEFAULT_STR
            it.ev = DEFAULT_STR
            it.photoCount = DEFAULT_STR
            it.videoTime = DEFAULT_TIME
            it.cameraModel = CrCameraInfo.CrCameraMode.NON
        }
        // todo: 2023/8/3 初始化媒体信息
        mediaInfo.postValue(CrMediaInfo(name = ""))
        mediaInfo.value?.let {
            it.time = DEFAULT_TIME
            it.size = DEFAULT_STR
            it.name = DEFAULT_STR
        }
    }


    /**
     * 覆写注册监听
     */
    override fun initListener() {
        // todo: 2023/8/2 相机连接
        CameraKey.KeyConnection.create().listen(this) { key ->
            key?.let { itKey ->
                if (itKey) {
                    cameraInfo.value?.isConnection = true
                    // todo: 2023/8/2 相机连接后 注册其他
                    listenerOther()
                    refresh(cameraInfo)
                } else {
                    cameraInfo.value?.let {
                        it.isConnection = false
                        it.iso = DEFAULT_STR
                        it.speed = DEFAULT_STR
                        it.focal = DEFAULT_STR
                        it.ev = DEFAULT_STR
                        it.videoTime = DEFAULT_TIME
                        it.photoCount = DEFAULT_STR
                        it.cameraModel = CrCameraInfo.CrCameraMode.NON
                        refresh(cameraInfo)
                    }
                    mediaInfo.value?.let {
                        it.time = DEFAULT_TIME
                        it.size = DEFAULT_STR
                        it.name = DEFAULT_STR
                    }
                }
            }
        }

    }

    /**
     * 注册其他
     */
    private fun listenerOther() {
        // todo: 2023/8/2 ISO监听
        CameraKey.KeyISO.create().listen(this) {
            it?.let {
                cameraInfo.value?.iso = CrUtil.getISO(it)
                refresh(cameraInfo)
            }
        }
        // todo: 2023/8/2 焦距
        CameraKey.KeyCameraZoomFocalLength.create().listen(this) {
            it?.let {
                cameraInfo.value?.focal = it.toString()
                refresh(cameraInfo)
            }
        }
        // todo: 2023/8/2 补偿值
        CameraKey.KeyExposureCompensation.create().listen(this) {
            it?.let {
                cameraInfo.value?.ev = CrUtil.getEv(it)
                refresh(cameraInfo)
            }
        }
        // todo: 2023/8/2 快门
        CameraKey.KeyShutterSpeed.create().listen(this) {
            it?.let {
                cameraInfo.value?.speed = CrUtil.getShutterSpeed(it)
                refresh(cameraInfo)
            }
        }
        // todo: 2023/8/2 存储卡
        CameraKey.KeyCameraStorageInfos.create().listen(this) {
            it?.let {
                var info: List<CameraStorageInfo> = it.cameraStorageInfoList
                if (info != null && info.isNotEmpty()) {
                    cameraInfo.value?.photoCount = info?.get(0).availablePhotoCount.toString()
                    cameraInfo.value?.videoTime =
                        CrUtil.secondsToHMS(info?.get(0).availableVideoDuration)
                    refresh(cameraInfo)
                }
            }
        }
        // todo: 2023/8/2 创建新媒体
        CameraKey.KeyNewlyGeneratedMediaFile.create().listen(this) {
            it?.let {
                if (it.type == MediaFileType.JPEG) {
                    mediaInfo.value?.name = "FX_${CrUnitManager.toSystemDate()}"
                    mediaInfo.value?.size = CrUnitManager.toFileSize(it.fileSize.toInt())
                    mediaInfo.value?.time =
                        "${it.createTime.year}-${it.createTime.month}-${it.createTime.day} ${it.createTime.hour}:${it.createTime.minute}:${it.createTime.second}"
                }
            }
        }
        // todo: 2023/8/3 相机模式
        CameraKey.KeyCameraMode.create().listen(this){
            it?.let {
                when(it){
                    CameraMode.VIDEO_NORMAL->{
                        cameraInfo.value?.cameraModel = CrCameraInfo.CrCameraMode.VIDEO
                    }
                    CameraMode.UNKNOWN->{
                        cameraInfo.value?.cameraModel = CrCameraInfo.CrCameraMode.NON
                    }
                    else->{
                        cameraInfo.value?.cameraModel = CrCameraInfo.CrCameraMode.PHOTO
                    }
                }
            }
        }
    }
}