package com.adealink.weparty.module.profile.data

import android.os.Parcelable
import com.adealink.frame.aab.util.getCompatString
import com.adealink.weparty.R
import com.adealink.weparty.module.image.data.PhotoData
import com.google.gson.annotations.GsonNullable
import com.google.gson.annotations.Must
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

enum class Gender(val gender: Int) {
    UNKNOWN(0),//未定义
    FEMALE(2), //女性
    MALE(1); //男性

    fun getDesc(): String {
        return when (this) {
            UNKNOWN -> {
                getCompatString(R.string.common_unknown)
            }

            MALE -> {
                getCompatString(R.string.common_male)
            }

            FEMALE -> {
                getCompatString(R.string.common_female)
            }
        }
    }

    companion object {

        fun map(gender: Int?): Gender {
            return when (gender) {
                FEMALE.gender -> {
                    FEMALE
                }

                MALE.gender -> {
                    MALE
                }

                else -> {
                    UNKNOWN
                }
            }
        }

    }

}

@Parcelize
data class UserInfo(
    @SerializedName("userNo") val uid: String,
    @GsonNullable
    @SerializedName("nickname") val nickName: String? = null,
    @GsonNullable
    @SerializedName("avatar") val avatar: String? = null,
    @GsonNullable
    @SerializedName("age") val age: Int? = null,
    @GsonNullable
    @SerializedName("birthday") val birthday: Long? = null,
    @GsonNullable
    @SerializedName("gender") val gender: Int? = null,
    @GsonNullable
    @SerializedName("intro") val intro: String? = null, //用户简介
    @GsonNullable
    @SerializedName("interests") val interests: List<UserInterest>? = null, //用户简介
    @GsonNullable
    @SerializedName("photos") val photos: List<String>? = null, //照片
    @GsonNullable
    @SerializedName("voiceBar") val voiceBar: String? = null, //语音条
    @GsonNullable
    @SerializedName("playmate") val playmate: Boolean? = null, //是否陪玩师
    @GsonNullable
    @SerializedName("languageNames") val languageNames: List<String>? = null, //用户会的语言列表
    @GsonNullable
    @SerializedName("star") val star: Float? = null, //评分
    @GsonNullable
    @SerializedName("skills") val skills: List<UserPlaymateSkill>? = null, //技能商品简易信息
    @GsonNullable
    @SerializedName("rated") val rated: Boolean? = null, //今天是否已打分
    @GsonNullable
    @SerializedName("fansCount") val fansCount: Int? = null, //粉丝数
    @GsonNullable
    @SerializedName("distance") val distance: Float? = null, //距离(单位:km)


    @GsonNullable
    @SerializedName("app_user_note") var userNote: UserNote? = null, //
) : Parcelable {

    fun isComplete(): Boolean {
        //用户个人信息是否完整
        return !avatar.isNullOrEmpty()
                && !nickName.isNullOrEmpty()
                && gender != null
                && !interests.isNullOrEmpty()
    }

    fun isPlaymate() = playmate == true

    fun isCacheValid(newAttrSet: Set<UserConfigType>?): Boolean {
//        if (newAttrSet.isNullOrEmpty()) {
//            return true
//        }
//        if (attrSet.containsAll(UserConfigType.values().toSet())) {
//            return true
//        }
//        if (attrSet.containsAll(newAttrSet)) {
//            return true
//        }
//        return false
        return true
    }

    fun getUserNoteName(): String? {
        val noteName = userNote?.note
        if (!noteName.isNullOrEmpty()) {
            return noteName
        }
        return nickName
    }

    companion object {

        fun emptyUserInfo(uid: String, avatar: String): UserInfo {
            return UserInfo(uid = uid, avatar = avatar)
        }

    }
}

/**
 * 编辑资料
 */
open class EditUserInfo() {

    //标记是否已修改
    var isEdited = false
    var avatar: PhotoData? = null
        set(value) {
            field = value
            isEdited = true
        }
    var nickname: String? = null
        set(value) {
            field = value
            isEdited = true
        }
    var gender: Gender? = null
        set(value) {
            field = value
            isEdited = true
        }
    var photos: List<PhotoData>? = null
        set(value) {
            field = value
            isEdited = true
        }
    var intro: String? = null
        set(value) {
            field = value
            isEdited = true
        }
    var birthday: Long? = null
        set(value) {
            field = value
            isEdited = true
        }
    var interest: List<UserInterest>? = null
        set(value) {
            field = value
            isEdited = true
        }

    open fun resetBy(userInfo: UserInfo?) {
        avatar = PhotoData(url = userInfo?.avatar, null, null)
        nickname = userInfo?.nickName
        gender = Gender.map(userInfo?.gender)
        photos = userInfo?.photos?.map {
            PhotoData(url = it, null, null)
        }
        intro = userInfo?.intro
        birthday = userInfo?.birthday
        interest = userInfo?.interests
        isEdited = false
    }

    companion object {
        val MAX_INTEREST_COUNT = 5
    }
}

@Parcelize
data class UserInterest(
    @SerializedName("code") var code: String, //品类Code
    @SerializedName("name") var name: String, //品类名称
) : Parcelable

infix fun UserInfo.contentsTheSame(other: UserInfo?): Boolean {
    other ?: return false
    return this == other
}

@Parcelize
data class UserPlaymateSkill(
    @Must
    @SerializedName("id") var id: String, //技能商品id
    @SerializedName("name") var name: String, //技能商品品类名称
    @SerializedName("icon") var icon: String, //图标
    @SerializedName("price") var price: Float, //价格
    @SerializedName("unit") var unit: String, //单位
    @SerializedName("cover") var cover: String, //封面
    @SerializedName("orderCount") var orderCount: Long, //订单数量
) : Parcelable

data class VoiceData(
    //远端路径
    var url: String?,

    //本地路径
    var path: String?,

    var duration: Long? = null //单位:毫秒
) {
    fun isRemote(): Boolean {
        return !url.isNullOrEmpty()
    }

    fun isLocal(): Boolean {
        return !path.isNullOrEmpty()
    }

    fun isNull(): Boolean {
        return url.isNullOrEmpty() && path.isNullOrEmpty()
    }
}

/**
 * 获取音频秒(不足1秒,向上取整)
 */
fun VoiceData?.getDuration(): Int {
    val duration = this?.duration
    if (duration == null || duration <= 0) {
        return 0
    }
    return if (duration % 1000f == 0f) {
        (duration / 1000).toInt()
    } else {
        ((duration + 1000) / 1000).toInt()
    }
}

fun VoiceData?.getDurationMs(): Long {
    return this?.duration ?: 0
}

fun VoiceData?.sameWith(other: VoiceData?): Boolean {
    if (this == null && other == null) {
        return true
    }
    return this?.url == other?.url
            && this?.path == other?.path
}


data class LanguageData(
    val key: String,
    val language: String
)

data class MyUserInfoData(
    @SerializedName("userProfile") val userInfo: UserInfo,
    @GsonNullable
    @SerializedName("userFollowStat") val userFollowStat: UserFollowStat? = null, //用户编辑语音
    @GsonNullable
    @SerializedName("userVoice") val userVoiceEdit: UserVoiceEditData? = null, //用户编辑语音
)

@Parcelize
data class UserFollowStat(
    @SerializedName("fansCount") val fansCount: Int,
    @SerializedName("followCount") val followCount: Int,
    @SerializedName("visitCount") val visitCount: Int,
) : Parcelable

@Parcelize
data class UserVoiceEditData(
    @GsonNullable
    @SerializedName("voiceBar") val voiceBar: String?, //语音条
    @SerializedName("voiceBarDuration") val voiceBarDuration: Int, //时长:秒
    @SerializedName("status") val status: Int, //0:待上传，1:待审核，2:审核通过展示中
    @GsonNullable
    @SerializedName("applyingVoiceBar") val applyingVoiceBar: String?, //申请中的语音条
    @GsonNullable
    @SerializedName("applyingVoiceBarDuration") val applyingVoiceBarDuration: Int?, //请中的语音条-时长（秒）
) : Parcelable {

    fun isDisplay(): Boolean {
        return !voiceBar.isNullOrEmpty() && status == 2
    }

    fun isUnderReview(): Boolean {
        return status == 1
    }

    fun getVoiceIfPass(): String? {
        return if (status == 2) {
            voiceBar
        } else {
            null
        }
    }
}

@Parcelize
data class UserNote(
    @SerializedName("userNo") val uid: String,
    @GsonNullable
    @SerializedName("note") val note: String?,
) : Parcelable