package com.abbidot.tracker.vm

import android.content.Context
import android.graphics.Bitmap
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.util.FileUtil
import com.abbidot.tracker.util.ImageUtil
import com.baidu.mapapi.map.BaiduMap
import com.google.android.gms.maps.GoogleMap
import id.zelory.compressor.Compressor
import id.zelory.compressor.constraint.default
import id.zelory.compressor.constraint.destination
import id.zelory.compressor.saveBitmap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

/**
 *Created  by .yzq on 2022/4/15/015.
 * @link
 * @description:截屏
 */
class ScreenshotsViewModel : ViewModel() {

    val mMapSnapshotPathLiveData = MutableLiveData<String>()

    /**
     * 截屏谷歌地图围栏图片,并压缩返回文件路径
     */
    fun fencesGoogleMapSnapshotImage(activity: BaseActivity<*>,
                                     googleMap: GoogleMap,
                                     view: View,
                                     fileName: String = "${System.currentTimeMillis()}.png") {
        googleMap.snapshot {
            if (null != it) {
                fencesMapSnapshotImage(activity, it, view, fileName)
            }
        }
    }

    /**
     * 截屏谷歌地图围栏图片,并压缩返回文件路径
     */
    fun fencesBaiduMapSnapshotImage(activity: BaseActivity<*>,
                                    baiduMap: BaiduMap,
                                    view: View,
                                    fileName: String = "${System.currentTimeMillis()}.png") {
        baiduMap.snapshot {
            if (null != it) {
                fencesMapSnapshotImage(activity, it, view, fileName)
            }
        }
    }

    /**
     * 创建围栏保存地图截屏
     */
    private fun fencesMapSnapshotImage(activity: BaseActivity<*>,
                                       bitmap: Bitmap,
                                       view: View,
                                       fileName: String = "${System.currentTimeMillis()}.png") {
        activity.showLoading(true)
        viewModelScope.launch(Dispatchers.IO) {
            val file =
                File(FileUtil.getDiskCacheDirectory(activity, FileUtil.SCREENSHOT_DIRECTORY_NAME),
                        fileName)

            //获取圆圈view的图片
            var viewBitmap = ImageUtil.viewToBitmap(view)
            //压缩图片尺寸大小
            viewBitmap =
                ImageUtil.zoomImg(viewBitmap, viewBitmap.width / 3 * 2, viewBitmap.height / 3 * 2)

            //合成图片
            var newBitmap = ImageUtil.combineBitmap(bitmap, viewBitmap)

            //从原位图中指定坐标点(x,y)开始，从中挖取宽width、高height的一块出来，创建新的Bitmap对象
            //截取谷歌地图中间图片
            val height = newBitmap.height / 6 * 2
            newBitmap = Bitmap.createBitmap(newBitmap, 0, height, newBitmap.width,
                    newBitmap.height - height * 2)

            //保存Bitmap图片
//                        val out = FileOutputStream(file)
//                        if (newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
//                            out.flush()
//                            out.close()
//                        }

            //保存Bitmap图片
            saveBitmap(newBitmap, file)

            //压缩图片
            val compressedImageFile = Compressor.compress(activity, File(file.path))
            mMapSnapshotPathLiveData.postValue(compressedImageFile.path)
        }
    }

    /**
     * 历史记录,保存地图截屏
     */
    private fun saveMapSnapshotBitmap(context: Context, bitmap: Bitmap, fileName: String) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val imageName = "${fileName}_${FileUtil.DATA_HISTORY_FILE_NAME}"
                val file = File(FileUtil.getDiskCacheDirectory(context,
                        FileUtil.SCREENSHOT_DIRECTORY_NAME), imageName)
                val height = bitmap.height / 7 * 2
                val newBitmap = Bitmap.createBitmap(bitmap, 0, height, bitmap.width, height)
                //保存Bitmap图片
                saveBitmap(newBitmap, file)
                //压缩图片,将图像文件压缩到特定目标文件
                val compressedImageFile = Compressor.compress(context, File(file.path)) {
                    default()
                    //保存到指定文件
                    destination(file)
                }
                XEventBus.post(EventName.HistoryDataSnapshot, compressedImageFile)
            }
        }
    }

    /**
     * 获取历史记录详情谷歌地图的截屏
     */
    fun getHistoryDataGoogleMapSnapshotImage(context: Context,
                                             googleMap: GoogleMap,
                                             fileName: String) {
        googleMap.snapshot {
            if (null != it) {
                saveMapSnapshotBitmap(context, it, fileName)
            }
        }
    }

    /**
     * 获取历史记录详情百度地图的截屏
     */
    fun getHistoryDataBaiduMapSnapshotImage(context: Context,
                                            baiduMap: BaiduMap,
                                            fileName: String) {
        //发起截图请求
        baiduMap.snapshot {
            if (null != it) {
                saveMapSnapshotBitmap(context, it, fileName)
            }
        }
    }
}