package com.bc_chat.bc_base.entity

import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import com.zhaohaoting.framework.utils.CharacterParser
import com.zhaohaoting.framework.view.QuickIndexBar

/**
 * author：Created by zhaohaoting on 2019/3/11
 * email：526309416@qq.com
 * desc：
 */
open class UserInfo constructor(@SerializedName(value = "uid", alternate = ["id"]) var userId: String? = "") : Comparable<UserInfo>, QuickIndexBar.QuickIndexEntity, Parcelable {

    override fun compareTo(other: UserInfo): Int {
        return firstLetter.compareTo(other.firstLetter)
    }

    var photo: String? = null
    var nickname: String? = null
    var remark: String? = null

    /**
     * 扩展字段
     */
    var isSelect: Boolean = false
    var isTop: Boolean = false
    var pinyin: String? = null
        get() {
            return field
                    ?: CharacterParser.getInstance().getSpelling(remark ?: nickname)
        }
    var letter: String = ""

    constructor(parcel: Parcel) : this(parcel.readString()) {
        photo = parcel.readString()
        nickname = parcel.readString()
        remark = parcel.readString()
        isSelect = parcel.readByte() != 0.toByte()
        isTop = parcel.readByte() != 0.toByte()
    }

    override fun getPinyinName(): String? {
        return pinyin
    }

    override fun getFirstLetter(): String {
        if (letter.isNullOrEmpty()) {
            var sortletter = "#"
            if (pinyin!!.isNotEmpty()) {
                sortletter = pinyin!!.substring(0, 1).toUpperCase()
            }
            // 正则表达式，判断首字母是否是英文字母
            letter = if (sortletter.matches("[A-Z]".toRegex())) {
                sortletter
            } else {
                ("#")
            }
        }
        return letter
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(userId)
        parcel.writeString(photo)
        parcel.writeString(nickname)
        parcel.writeString(remark)
        parcel.writeByte(if (isSelect) 1 else 0)
        parcel.writeByte(if (isTop) 1 else 0)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<UserInfo> {
        override fun createFromParcel(parcel: Parcel): UserInfo {
            return UserInfo(parcel)
        }

        override fun newArray(size: Int): Array<UserInfo?> {
            return arrayOfNulls(size)
        }
    }

}