package com.abbidot.tracker.vm

import android.content.Context
import android.text.TextUtils
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.ui.common.MyCropImageContract
import com.abbidot.tracker.util.FileUtil.Companion.size
import com.huantansheng.easyphotos.models.album.entity.Photo
import id.zelory.compressor.Compressor
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

/**
 *Created  by .yzq on 2022/3/23/023.
 * @link
 * @description:拍照和选取照片后，裁剪、压缩图片返回
 */
class TakePhotoAndCompressViewModel : ViewModel() {

    private lateinit var mCropImageActivityResultLauncher: ActivityResultLauncher<Photo>
    val mPhotoPathLiveData = MutableLiveData<MutableList<File>>()

    /**
     * 适用于:选了照片马上压缩的
     */
    fun dealCompressPhoto(context: Context, resultPhotos: ArrayList<Photo>) {
        val size = resultPhotos.size
        if (size > 0) {
            val fileList = mutableListOf<File>()
            viewModelScope.launch(Dispatchers.IO) {
                LogUtil.d("一共选了多少张照片:$size")
                for (i in 0 until size) {
                    val photo = resultPhotos[i]
                    val file = File(photo.path)
                    //压缩图片
                    val compressedImageFile = Compressor.compress(context, file)
                    fileList.add(compressedImageFile)

                    //打印第一张照片日志
                    if (i == 0) {
                        LogUtil.d("第一张:${photo},size=${photo.size}byte")
                        LogUtil.d(compressedImageFile.path)
                        LogUtil.d("size=${compressedImageFile.length()}")
                    }
                }
                mPhotoPathLiveData.postValue(fileList)
            }
        }
    }

    /**
     * 放在OnCreate()中注册
     */
    fun registerCropImageActivityResult(
        componentActivity: ComponentActivity, aspectRatioX: Int = 1, aspectRatioY: Int = 1
    ) {
        mCropImageActivityResultLauncher = componentActivity.registerForActivityResult(
            MyCropImageContract(
                aspectRatioX, aspectRatioY
            )
        ) { result ->
            if (result.isSuccessful) {
                // use the returned uri
                val uriContent = result.uriContent
                val uriFilePath = result.getUriFilePath(componentActivity) // optional usage
                LogUtil.e("裁剪图片成功----------->$uriContent\n$uriFilePath")
                compressImage(componentActivity, uriFilePath)
            }
        }
    }

    /**
     * 跳转裁剪图片并且压缩
     */
    fun goCropAndCompressImage(resultPhotos: ArrayList<Photo>?) {
        if (null == resultPhotos || resultPhotos.size == 0) return
        val photo = resultPhotos[0]
        mCropImageActivityResultLauncher.launch(photo)
    }

    /**
     * 压缩某张图片
     */
    private fun compressImage(context: Context, path: String?) {
        if (null == path || TextUtils.isEmpty(path)) return
        val fileList = mutableListOf<File>()
        viewModelScope.launch(Dispatchers.IO) {
            val file = File(path)
            //压缩图片
            val compressedImageFile = Compressor.compress(context, file)
            LogUtil.e(
                "裁剪图片压缩前：size=${file.size()},path=${file.path} \n压缩后：size=${compressedImageFile.size()},path=${compressedImageFile.path}"
            )
            fileList.add(compressedImageFile)
            mPhotoPathLiveData.postValue(fileList)
        }
    }
}