package com.yyxx.eyuangame.demo;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author #Suyghur.
 * Created on 2020/12/21
 */
public class CacheInfo {

    public static RoleInfo setDemoRoleInfo(Context context, String userId) {
        RoleInfo roleInfo = new RoleInfo();
        long ts = System.currentTimeMillis() / 1000;
        roleInfo.roleId = "aaa" + ts;
        roleInfo.roleName = "角色名123";
        roleInfo.roleLevel = "110";
        roleInfo.serverCode = "333";
        roleInfo.serverName = "服务器名333";
        roleInfo.vipLevel = "1";
        roleInfo.balance = "600";
        roleInfo.totalPurchase = 0;
        roleInfo.roleCTime = ts;
        String info = roleInfo.toJsonString();
        SharedPreferences sp = context.getSharedPreferences("app_role_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(userId, info);
        editor.apply();
        return roleInfo;
    }

    public static RoleInfo getDemoRoleInfo(Context context, String userId) {
        try {
            SharedPreferences sp = context.getSharedPreferences("app_role_info", Context.MODE_PRIVATE);
            String info = sp.getString(userId, "");
            if (TextUtils.isEmpty(info)) {
                //不存在则创建
                return setDemoRoleInfo(context, userId);
            } else {
                JSONObject jsonObject = new JSONObject(info);
                RoleInfo roleInfo = new RoleInfo();
                roleInfo.roleId = jsonObject.getString("role_id");
                roleInfo.roleName = jsonObject.getString("role_name");
                roleInfo.roleLevel = jsonObject.getString("role_level");
                roleInfo.serverCode = jsonObject.getString("server_id");
                roleInfo.serverName = jsonObject.getString("server_name");
                roleInfo.vipLevel = jsonObject.getString("vip_level");
                roleInfo.balance = jsonObject.getString("balance");
                roleInfo.totalPurchase = jsonObject.getInt("total_purchase");
                roleInfo.roleCTime = jsonObject.getLong("role_ctime");
                return roleInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final class RoleInfo {
        public String roleId = "";
        public String roleName = "";
        public String roleLevel = "";
        public String serverCode = "";
        public String serverName = "";
        public String vipLevel = "";
        public String balance = "";
        public int totalPurchase = 0;
        public long roleCTime = 0L;


        public final String toJsonString() {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("role_id", this.roleId);
                jsonObject.put("role_name", this.roleName);
                jsonObject.put("role_level", this.roleLevel);
                jsonObject.put("server_code", this.serverCode);
                jsonObject.put("server_name", this.serverName);
                jsonObject.put("vip_level", this.vipLevel);
                jsonObject.put("balance", this.balance);
                jsonObject.put("total_purchase", this.totalPurchase);
                jsonObject.put("role_ctime", this.roleCTime);
                return jsonObject.toString();
            } catch (JSONException e) {
                e.printStackTrace();
                return "";
            }
        }

        @Override
        public String toString() {
            return "RoleInfo{" +
                    "roleId='" + roleId + '\'' +
                    ", roleName='" + roleName + '\'' +
                    ", roleLevel='" + roleLevel + '\'' +
                    ", serverCode='" + serverCode + '\'' +
                    ", serverName='" + serverName + '\'' +
                    ", vipLevel='" + vipLevel + '\'' +
                    ", balance='" + balance + '\'' +
                    ", totalPurchase=" + totalPurchase +
                    ", roleCTime=" + roleCTime +
                    '}';
        }
    }
}
