package com.adealink.weparty.setting.report.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.oss.data.UploadFile
import com.adealink.weparty.App
import com.adealink.weparty.image.EVIDENCE_IMAGE_MAX_HEIGHT
import com.adealink.weparty.image.EVIDENCE_IMAGE_MAX_SIZE_KB
import com.adealink.weparty.image.EVIDENCE_IMAGE_MAX_WIDTH
import com.adealink.weparty.image.EVIDENCE_IMAGE_MIN_QUALITY
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.setting.datasource.remote.SettingHttpService
import com.adealink.weparty.setting.helpcenter.data.AddPictureItemData
import com.adealink.weparty.setting.helpcenter.data.BasePictureItemData
import com.adealink.weparty.setting.helpcenter.data.PictureItemData
import com.adealink.weparty.setting.report.ReportReq
import com.adealink.weparty.util.uploadPhotos
import kotlinx.coroutines.launch

class ReportViewModel : BaseViewModel() {

    private val reportHttpService by fastLazy {
        App.instance.networkService.getHttpService(SettingHttpService::class.java)
    }

    val pictureList = mutableListOf<PhotoData>()

    val pictureItemListLD = MutableLiveData<List<BasePictureItemData>>()

    fun addPicture(path: String?, uri: String?) {
        viewModelScope.launch {
            pictureList.add(PhotoData(null, path, uri))
            onPictureListChanged()
        }
    }

    fun removePicture(data: PhotoData) {
        viewModelScope.launch {
            pictureList.remove(data)
            onPictureListChanged()
        }
    }

    private fun onPictureListChanged() {
        val itemList = mutableListOf<BasePictureItemData>()
        itemList.addAll(
            pictureList.map {
                PictureItemData(it)
            }
        )
        //小于6张图片,可以继续添加图片
        if (pictureList.size < 6) {
            itemList.add(AddPictureItemData)
        }
        pictureItemListLD.send(itemList)
    }

    fun submit(
        uid: String,
        content: String?
    ): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {

            val uploadImages = mutableListOf<PhotoData>()
            pictureList.forEach { picture ->
                if (picture.isLocal()) {
                    uploadImages.add(picture)
                }
            }
            //上传证据
            val uploadRlt = uploadPhotos(
                uploadImages,
                EVIDENCE_IMAGE_MAX_WIDTH,
                EVIDENCE_IMAGE_MAX_HEIGHT,
                EVIDENCE_IMAGE_MAX_SIZE_KB,
                EVIDENCE_IMAGE_MIN_QUALITY,
                UploadFile.FileType.ATTACHMENT
            )
            when (uploadRlt) {
                is Rlt.Failed -> {
                    liveData.send(uploadRlt)
                    return@launch
                }

                is Rlt.Success<List<PhotoData>> -> {

                }
            }
            val rlt = reportHttpService.report(
                ReportReq(
                    uid,
                    content ?: "",
                    pictureList.mapNotNull { it.url }
                )
            )
            liveData.send(rlt)
        }
        return liveData
    }

}