package com.demo.xinrui

import android.content.Context
import android.text.TextUtils
import com.yyrh.utils.YYLog
import org.json.JSONException
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2020/12/7
 */
class CacheRoleInfo {

    data class RoleInfo(
        var roleId: String = "", var roleName: String = "", var roleLevel: String = "",
        var rolePower: String = "", var balance: String = "", var vipLevel: String = "",
        var serverId: String = "", var serverName: String = "", var partyId: String = "",
        var partyName: String = "", var reincarnationCount: String = "0", var totalPurchase: String = "0",
        var roleCTime: String = "", var gender: String = "男"
    ) {


        fun toJsonString(): String {
            try {
                val jsonObject = JSONObject()
                jsonObject.put("role_id", roleId)
                jsonObject.put("role_name", roleName)
                jsonObject.put("role_level", roleLevel)
                jsonObject.put("role_power", rolePower)
                jsonObject.put("balance", balance)
                jsonObject.put("vip_level", vipLevel)
                jsonObject.put("server_id", serverId)
                jsonObject.put("server_name", serverName)
                jsonObject.put("party_id", partyId)
                jsonObject.put("party_name", partyName)
                jsonObject.put("reincarnation_count", reincarnationCount)
                jsonObject.put("total_purchase", totalPurchase)
                jsonObject.put("role_ctime", roleCTime)
                jsonObject.put("gender", gender)
                return jsonObject.toString()
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            return ""
        }
    }

    companion object {
        fun setDemoRoleInfo(context: Context, userId: String): RoleInfo {
            val ts = (System.currentTimeMillis() / 1000).toString()
            val roleInfo = RoleInfo(
                roleId = "aaa$ts",
                roleName = "角色名123",
                roleLevel = "110",
                rolePower = "战力123456",
                balance = "600",
                vipLevel = "1",
                serverId = "333",
                serverName = "服务器名333",
                partyId = "party_1234",
                partyName = "帮派1234",
                reincarnationCount = "0",
                totalPurchase = "0",
                roleCTime = ts,
                gender = "男"
            )
            val info = roleInfo.toJsonString()
            val sp = context.getSharedPreferences("app_role_info", Context.MODE_PRIVATE)
            val editor = sp.edit()
            editor.putString(userId, info)
            editor.apply()
            return roleInfo
        }

        fun getDemoRoleInfo(context: Context, userId: String): RoleInfo? {
            try {
                val sp = context.getSharedPreferences("app_role_info", Context.MODE_PRIVATE)
                val info = sp.getString(userId, "")
                return if (TextUtils.isEmpty(info)) {
                    //没有则创建
                    setDemoRoleInfo(context, userId)
                } else {
                    val jsonObject = JSONObject(info!!)
                    RoleInfo(
                        roleId = jsonObject.getString("role_id"),
                        roleName = jsonObject.getString("role_name"),
                        roleLevel = jsonObject.getString("role_level"),
                        rolePower = jsonObject.getString("role_power"),
                        balance = jsonObject.getString("balance"),
                        vipLevel = jsonObject.getString("vip_level"),
                        serverId = jsonObject.getString("server_id"),
                        serverName = jsonObject.getString("server_name"),
                        partyId = jsonObject.getString("party_id"),
                        partyName = jsonObject.getString("party_name"),
                        reincarnationCount = jsonObject.getString("reincarnation_count"),
                        totalPurchase = jsonObject.getString("total_purchase"),
                        roleCTime = jsonObject.getString("role_ctime"),
                        gender = jsonObject.getString("gender")
                    )
                }

            } catch (e: Exception) {
                e.printStackTrace()
            }
            return null
        }
    }
}