package com.eyuancomm.demo

import android.content.Context
import android.text.TextUtils
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 serverCode: String = "", var serverName: String = "", var vipLevel: String = "",
        var balance: String = "", var totalPurchase: Int = 0, var roleCTime: Long = 0
    ) {
        fun toJsonString(): String {
            try {
                val jsonObject = JSONObject()
                jsonObject.put("role_id", roleId)
                jsonObject.put("role_name", roleName)
                jsonObject.put("role_level", roleLevel)
                jsonObject.put("server_code", serverCode)
                jsonObject.put("server_name", serverName)
                jsonObject.put("vip_level", vipLevel)
                jsonObject.put("balance", balance)
                jsonObject.put("total_purchase", totalPurchase)
                jsonObject.put("role_ctime", roleCTime)
                return jsonObject.toString()
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            return ""
        }
    }

    companion object {

        fun setDemoRoleInfo(context: Context, userId: String): RoleInfo {
            val ts = System.currentTimeMillis() / 1000
            val roleInfo = RoleInfo(
                roleId = "aaa$ts",
                roleName = "角色名123",
                roleLevel = "110",
                serverCode = "333",
                serverName = "服务器名333",
                vipLevel = "1",
                balance = "600",
                totalPurchase = 0,
                roleCTime = ts
            )
            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"),
                        serverCode = jsonObject.getString("server_code"),
                        serverName = jsonObject.getString("server_name"),
                        vipLevel = jsonObject.getString("vip_level"),
                        balance = jsonObject.getString("balance"),
                        totalPurchase = jsonObject.getInt("total_purchase"),
                        roleCTime = jsonObject.getLong("role_ctime")
                    )
                }

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