package com.adealink.weparty.profile.edit.viewmodel

import androidx.lifecycle.MutableLiveData
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.module.profile.data.EditUserInfo
import com.adealink.weparty.module.profile.data.Gender
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.module.profile.data.UserInterest
import com.adealink.weparty.profile.edit.data.AddPhotoItemData
import com.adealink.weparty.profile.edit.data.BaseMediaItemData
import com.adealink.weparty.profile.edit.data.PhotoItemData
import kotlinx.coroutines.launch

class EditProfileViewModel : BaseViewModel() {

    companion object {
        private const val TAG = "EditProfileViewModel"
        const val MAX_PHOTO_COUNT = 9
    }

    private val editUserInfo = EditUserInfo()
    val editUserInfoLD = MutableLiveData<EditUserInfo>()

    val photoList = mutableListOf<PhotoData>()

    val photoItemList = mutableListOf<BaseMediaItemData>()
    val photoItemListLD = MutableLiveData<MutableList<BaseMediaItemData>>()

    init {
        onPictureListChanged()
    }

    fun getEditUserInfo(): EditUserInfo {
        return editUserInfo
    }

    fun setUserInfo(userInfo: UserInfo?) {
        editUserInfo.resetBy(userInfo)
        //照片墙
        photoList.clear()
        photoList.addAll(editUserInfo.photos ?: emptyList())
        onPictureListChanged()

        editUserInfoLD.send(editUserInfo)
    }

    fun setAvatar(
        url: String?,
        path: String?,
        uri: String?
    ) {
        editUserInfo.avatar = PhotoData(url, path, uri)
        editUserInfoLD.send(editUserInfo)
    }

    fun addPhoto(path: String?, uri: String?) {
        viewModelScope.launch {
            photoList.add(
                0,
                PhotoData(
                    url = null,
                    path = path,
                    uri = uri
                )
            )
            editUserInfo.photos = photoList
            editUserInfoLD.send(editUserInfo)
            onPictureListChanged()
        }
    }

    fun removePhoto(data: PhotoData) {
        viewModelScope.launch {
            photoList.remove(data)
            editUserInfo.photos = photoList
            editUserInfoLD.send(editUserInfo)
            onPictureListChanged()
        }
    }

    private fun onPictureListChanged() {
        val photoList = photoList.take(MAX_PHOTO_COUNT)
        photoItemList.clear()
        for ((i, data) in photoList.withIndex()) {
            photoItemList.add(PhotoItemData(data))
        }
        //小于9张图片,可以继续添加图片
        if (photoItemList.size < MAX_PHOTO_COUNT) {
            photoItemList.add(0, AddPhotoItemData)
        }
        photoItemListLD.send(photoItemList)
    }

    //调整勋章位置
    val adjustPhotoPositionLD = MutableLiveData<Pair<Int, Int>>()
    fun adjustPhotoPosition(fromPosition: Int, toPosition: Int) {
        Log.d(TAG, "adjustPhotoPosition, from:$fromPosition, to:$toPosition")
        if (fromPosition < 0 || toPosition < 0) return
        val itemCount = photoItemList.size
        if (fromPosition > itemCount || toPosition > itemCount) return

        viewModelScope.launch {
            photoItemList.add(toPosition, photoItemList.removeAt(fromPosition))
            photoList.clear()
            for (itemData in photoItemList) {
                if (itemData is PhotoItemData) {
                    photoList.add(itemData.data)
                }
            }
            editUserInfo.photos = photoList
            editUserInfoLD.send(editUserInfo)
            adjustPhotoPositionLD.send(Pair(fromPosition, toPosition))
        }
    }

    fun setNickName(name: String?) {
        editUserInfo.nickname = name
        editUserInfoLD.send(editUserInfo)
    }

    fun setBirthday(birthday: Long) {
        editUserInfo.birthday = birthday
        editUserInfoLD.send(editUserInfo)
    }

    fun setGender(gender: Gender) {
        editUserInfo.gender = gender
        editUserInfoLD.send(editUserInfo)
    }

    fun setIntro(intro: String?) {
        editUserInfo.intro = intro
        editUserInfoLD.send(editUserInfo)
    }

    fun setInterest(interest: List<UserInterest>?) {
        editUserInfo.interest = interest
        editUserInfoLD.send(editUserInfo)
    }
}