package com.abbidot.tracker.vm

import android.content.Context
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.DataBean
import com.abbidot.tracker.bean.LabelBean
import com.abbidot.tracker.bean.PetAllInfoBean
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2024/5/27/027.
 * @link
 * @description: 宠物信息相关
 */
class PetInfoViewModel : ViewModel() {

    val mPetLabelsLiveData = MutableLiveData<Result<MutableList<String>>>()
    val mAddPetLiveData = MutableLiveData<Result<PetBean>>()
    val mUpdatePetInfoLiveData = MutableLiveData<Result<String>>()
    val mPetInfoLiveData = MutableLiveData<Result<PetBean>>()
    val mPetAllInfoLiveData = MutableLiveData<Result<PetAllInfoBean>>()

    fun getPetLabels(activity: BaseActivity<*>) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getPetLabels()
            mPetLabelsLiveData.value = result
        }
    }

    fun getPetAllInfo(activity: BaseActivity<*>, petId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getPetAllInfo(petId)
            mPetAllInfoLiveData.value = result
        }
    }

    fun getPetInfo(activity: BaseActivity<*>, petId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getPetInfo(petId)
            mPetInfoLiveData.value = result
        }
    }

    fun addPet(activity: BaseActivity<*>, petBean: PetBean) {
        activity.showLoading(true)
        petBean.apply {
            viewModelScope.launch {
                val result = NetworkApi.addPet(
                    userId,
                    petName,
                    breed,
                    gender,
                    petType,
                    birthdayDate,
                    imgurl,
                    weight,
                    height,
                    labels
                )
                mAddPetLiveData.value = result
            }
        }
    }

    fun updatePetInfo(activity: BaseActivity<*>, petBean: PetBean) {
        activity.showLoading(true)
        petBean.apply {
            viewModelScope.launch {
                val result = NetworkApi.updatePetInfo(
                    userId,
                    petName,
                    breed,
                    gender,
                    petType,
                    birthdayDate,
                    petId,
                    imgurl,
                    weight,
                    height,
                    labels
                )
                mUpdatePetInfoLiveData.value = result
            }
        }
    }


    /**
     * 多个标签，隔开的字符串转数组
     */
    fun getLabelData(label: String): MutableList<LabelBean> {
        val labelList = mutableListOf<LabelBean>()
        if (!TextUtils.isEmpty(label)) {
            val labels = label.split(",")
            if (labels.isNotEmpty()) {
                labels.forEach { str ->
                    val labelBean = LabelBean(str)
                    labelList.add(labelBean)
                }
            }
        }
        return labelList
    }

    /**
     * 获取宠物类型名称
     */
    fun getTypeName(context: Context, type: Int): String {
        return when (type) {
            ConstantInt.DogPetType -> {
                context.getString(R.string.txt_dog)
            }

            ConstantInt.CatPetType -> {
                context.getString(R.string.txt_cat)
            }

            else -> {
                context.getString(R.string.txt_other)
            }
        }
    }

    /**
     * 获取宠物类型名称
     */
    fun getSexName(context: Context, sex: Int): String {
        return when (sex) {
            ConstantInt.Man -> {
                context.getString(R.string.pet_manage_gender_male)
            }

            else -> {
                context.getString(R.string.pet_manage_gender_female)
            }
        }
    }

    /**
     * 获取勋章
     */
    fun getBadges(
        context: Context, createDate: String, showShort: Boolean = false, shortCount: Int = 4
    ): MutableList<DataBean> {
        LogUtil.e("勋章时间：$createDate")
        val toTimestamp = Utils.stringToTimestamp(createDate)
        val diffTimestamp = if (TextUtils.isEmpty(createDate)) System.currentTimeMillis()
        else System.currentTimeMillis() - toTimestamp
        val dayTimestamp = 24 * 60 * 60 * 1000L
        val monthTimestamp = 30 * dayTimestamp
        val yearTimestamp = 12 * monthTimestamp
        val list = mutableListOf<DataBean>()
        var unlockNum = 0

        DataBean().apply {
            unlockNum++
            name = context.getString(R.string.txt_fresh_paws)
            imageId = R.drawable.icon_fresh_paws_unlock_svg
            //解锁状态
            state = ConstantInt.Open
            //勋章解锁时间
            value = Utils.stringToDate(createDate, resultFormat = Utils.DATE_FORMAT_PATTERN_EN3)
            //勋章解锁说明
            unit = context.getString(R.string.txt_fresh_paws_dec)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= dayTimestamp) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 1), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_day_1_devotee_dec1)
                R.drawable.icon_day_1_devotee_unlock_svg
            } else {
                unit = context.getString(R.string.txt_day_1_devotee_dec2)
                R.drawable.icon_day_1_devotee_lock_svg
            }
            name = context.getString(R.string.txt_day_1_devotee)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (3 * dayTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 3), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_newbie_navigator_dec1)
                R.drawable.icon_newbie_navigator_unlock_svg
            } else {
                unit = context.getString(R.string.txt_newbie_navigator_dec2)
                R.drawable.icon_newbie_navigator_lock_svg
            }
            name = context.getString(R.string.txt_newbie_navigator)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (7 * dayTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 7), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_furry_friend_dec1)
                R.drawable.icon_furry_friend_unlock_svg
            } else {
                unit = context.getString(R.string.txt_furry_friend_dec2)
                R.drawable.icon_furry_friend_lock_svg
            }
            name = context.getString(R.string.txt_furry_friend)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (15 * dayTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 15), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_pawtential_pal_dec1)
                R.drawable.icon_pawtential_pal_unlock_svg
            } else {
                unit = context.getString(R.string.txt_pawtential_pal_dec2)
                R.drawable.icon_pawtential_pal_lock_svg
            }
            name = context.getString(R.string.txt_pawtential_pal)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= monthTimestamp) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_month_mate_dec1)
                R.drawable.icon_month_mate_unlock_svg
            } else {
                unit = context.getString(R.string.txt_month_mate_dec2)
                R.drawable.icon_month__mate_lock_svg
            }
            name = context.getString(R.string.txt_month_mate)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (3 * monthTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 3), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_tailwag_explorer_dec1)
                R.drawable.icon_tailwag_explorer_unlock_svg
            } else {
                unit = context.getString(R.string.txt_tailwag_explorer_dec2)
                R.drawable.icon_tailwag_explorer_lock_svg
            }
            name = context.getString(R.string.txt_tailwag_explorer)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (6 * monthTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 6), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_pawsome_partner_dec1)
                R.drawable.icon_pawsome_partner_unlock_svg
            } else {
                unit = context.getString(R.string.txt_pawsome_partner_dec2)
                R.drawable.icon_pawsome_partner_lock_svg
            }
            name = context.getString(R.string.txt_pawsome_partner)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= yearTimestamp) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 12), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_1_year_hugger_dec1)
                R.drawable.icon_1_year_hugger_unlock_svg
            } else {
                unit = context.getString(R.string.txt_1_year_hugger_dec2)
                R.drawable.icon_1_year_hugger_lock_svg
            }
            name = context.getString(R.string.txt_1_year_hugger)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (18 * monthTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 18), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_purrfect_partner_dec1)
                R.drawable.icon_purrfect_partner_unlock_svg
            } else {
                unit = context.getString(R.string.txt_purrfect_partner_dec2)
                R.drawable.icon_purrfect_partner_lock_svg
            }
            name = context.getString(R.string.txt_purrfect_partner)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (2 * yearTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 24), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_2_year_companion_dec1)
                R.drawable.icon_2_year_companion_unlock_svg
            } else {
                unit = context.getString(R.string.txt_2_year_companion_dec2)
                R.drawable.icon_2_year_companion_lock_svg
            }
            name = context.getString(R.string.txt_2_year_companion)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (30 * monthTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 30), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_faithful_friend_dec1)
                R.drawable.icon_socialite_unlock_svg
            } else {
                unit = context.getString(R.string.txt_faithful_friend_dec2)
                R.drawable.icon_socialite_lock_svg
            }
            name = context.getString(R.string.txt_faithful_friend)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (3 * yearTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 36), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_3_year_harmony_dec1)
                R.drawable.icon_3_year_harmony_unlock_svg
            } else {
                unit = context.getString(R.string.txt_3_year_harmony_dec2)
                R.drawable.icon_3_year_harmony_lock_svg
            }
            name = context.getString(R.string.txt_3_year_harmony)
            list.add(this)
        }

        DataBean().apply {
            imageId = if (diffTimestamp >= (5 * yearTimestamp)) {
                unlockNum++
                state = ConstantInt.Open
                value = Utils.formatTime(
                    Utils.getAfterHowTimestamp(toTimestamp, 30 * 60), Utils.DATE_FORMAT_PATTERN_EN3
                )
                unit = context.getString(R.string.txt_furr_timer_friend_dec1)
                R.drawable.icon_furr_timer_friend_unlock_svg
            } else {
                unit = context.getString(R.string.txt_furr_timer_friend_dec2)
                R.drawable.icon_furr_timer_friend_lock_svg
            }
            name = context.getString(R.string.txt_furr_timer_friend)
            list.add(this)
        }

        if (showShort) {
            return if (unlockNum > shortCount) {
                list.subList(unlockNum - shortCount, unlockNum)
            } else {
                list.subList(0, 4)
            }
        }

        return list
    }
}