package com.adealink.weparty.setting.helpcenter.viewmodel

import androidx.lifecycle.MutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.setting.helpcenter.data.AddPictureItemData
import com.adealink.weparty.setting.helpcenter.data.BasePictureItemData
import com.adealink.weparty.setting.helpcenter.data.PictureData
import com.adealink.weparty.setting.helpcenter.data.PictureItemData
import kotlinx.coroutines.launch

class HelpCenterViewModel : BaseViewModel() {

    val pictureList = mutableListOf<PictureData>()

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

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

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

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

    fun submit() {

    }

}