package com.adealink.weparty.account.register.viewmodel

import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.account.constant.TAG_ACCOUNT_LOGIN
import com.adealink.weparty.account.login.data.RandomProfile
import com.adealink.weparty.account.login.datasource.remote.LoginHttpService
import com.adealink.weparty.account.register.Page
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 kotlinx.coroutines.launch

class RegisterProfileViewModel : BaseViewModel() {

    private val loginHttpService by lazy {
        App.instance.networkService.getHttpService(LoginHttpService::class.java)
    }

    val randomProfileLD = MutableLiveData<RandomProfile>()

    fun loadRandomProfile() {
        viewModelScope.launch {
            val rlt = loginHttpService.randomProfile()
            when (rlt) {
                is Rlt.Failed -> {
                    Log.e(TAG_ACCOUNT_LOGIN, "register, load Profile fail, ${rlt.error}")
                }

                is Rlt.Success -> {
                    randomProfileLD.send(rlt.data.data)
                }
            }
        }
    }

    fun getNextRandomAvatar(): String? {
        val randomProfile = randomProfileLD.value ?: return null
        return when (editUserInfo.gender) {
            Gender.MALE -> {
                randomProfile.male.avatars.randomOrNull()
            }

            Gender.FEMALE -> {
                randomProfile.female.avatars.randomOrNull()
            }

            else -> {
                null
            }
        }
    }

    fun getNextRandomNickname(): String? {
        val randomProfile = randomProfileLD.value ?: return null
        return when (editUserInfo.gender) {
            Gender.MALE -> {
                randomProfile.male.nicknames.randomOrNull()
            }

            Gender.FEMALE -> {
                randomProfile.female.nicknames.randomOrNull()
            }

            else -> {
                null
            }
        }
    }

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

    fun initEditUserInfo(userInfo: UserInfo?) {
        editUserInfo.resetBy(userInfo)
    }

    fun getEditUserInfo(): EditUserInfo {
        return editUserInfo
    }

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

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

    fun selectAvatarPath(avatarPath: String?, uri: String?) {
        this.editUserInfo.avatar = PhotoData(url = null, path = avatarPath, uri = uri)
        editUserInfoLD.send(editUserInfo)
    }

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

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

    fun setInterests(interests: List<UserInterest>) {
        this.editUserInfo.interest = interests
        editUserInfoLD.send(editUserInfo)
    }


    val pageLD = ExtMutableLiveData<Page>()
    fun goPage(page: Page) {
        pageLD.send(page)
    }
}