package cn.yyxx.commsdk.base.entity.bean;

import android.app.Activity;
import android.text.TextUtils;



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

import java.text.DecimalFormat;

import cn.yyxx.support.AppUtils;
import cn.yyxx.support.device.DeviceInfoUtils;

public class InitBean {

    private static final String switchState = "1";

    public String agreement_privacy = "";

    public String service_center = "";

    public String access_token = "";

    public String agreement_user = "";

    public String isEmulator = "0";

    public JSONObject links = null;

    public JSONArray reg_tabs = null;

    public int sdk_login = 1;

    public int bswitch = 0;

    public boolean isSwitchAccount = false;

    public int rn_bh = 1800;



    public  boolean isLandscape,isShowPhoneRegFrist,isOpenRealName,isOpenFloatView,isForceRealName,isShowKf,isSMSBlocking;

    public static InitBean toBean(String json, Activity activity) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }


        InitBean initBean = new InitBean();
        try {
            JSONObject jsonObject = new JSONObject(json);


            initBean.reg_tabs = jsonObject.optJSONArray("reg_tabs");
            initBean.sdk_login = jsonObject.optInt("sdk_login", 1);

            initBean.bswitch = jsonObject.optInt("bswitch");

            parseSwitchCode(initBean,initBean.bswitch);

            initBean.access_token = jsonObject.optString("access_token");

            initBean.rn_bh = jsonObject.optInt("rn_bh", 1800);

            initBean.isLandscape = AppUtils.isLandscape(activity);

            if (DeviceInfoUtils.isEmulator(activity) &&DeviceInfoUtils.isEmulator2(activity)){
                initBean.isEmulator = "1";
            }

            if (initBean.reg_tabs != null) {

                if (initBean.reg_tabs.get(0).toString().equals("phone")) {
                     initBean.isShowPhoneRegFrist = true;
                }

            }
            initBean.links = jsonObject.optJSONObject("links");

            if (initBean.links != null) {

                initBean.agreement_privacy = initBean.links.optString("agreement_privacy");
                initBean.service_center = initBean.links.optString("service_center");
                initBean.agreement_user = initBean.links.optString("agreement_user");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


        return initBean;

    }

    @Override
    public String toString() {
        return "InitBean{" +
                "agreement_privacy='" + agreement_privacy + '\'' +
                ", service_center='" + service_center + '\'' +
                ", access_token='" + access_token + '\'' +
                ", agreement_user='" + agreement_user + '\'' +
                ", isEmulator='" + isEmulator + '\'' +
                ", links=" + links +
                ", reg_tabs=" + reg_tabs +
                ", sdk_login=" + sdk_login +
                ", bswitch=" + bswitch +
                ", isSwitchAccount=" + isSwitchAccount +
                ", rn_bh=" + rn_bh +
                ", isLandscape=" + isLandscape +
                ", isShowPhoneRegFrist=" + isShowPhoneRegFrist +
                ", isOpenRealName=" + isOpenRealName +
                ", isOpenFloatView=" + isOpenFloatView +
                ", isForceRealName=" + isForceRealName +
                ", isShowKf=" + isShowKf +
                ", isSMSBlocking=" + isSMSBlocking +
                '}';
    }

    static void parseSwitchCode(InitBean initBean, int code){
        {
            String binaryStr = Integer.toBinaryString(code);
            DecimalFormat df=new DecimalFormat("000000");
            String bwstr=df.format(Integer.parseInt(binaryStr));


            for (int i = 0; i < bwstr.length(); i++) {
                int a = bwstr.length()-i -1;
                int b = bwstr.length()-i;
                String s2 = bwstr.substring(a,b);
                switch (i) {
                    case 0:
                        if (s2.equals(switchState)){
                            initBean.isOpenRealName = true;
                        }

                        break;
                    case 1:
                        if (s2.equals(switchState)){
                            initBean.isOpenFloatView = true;
                        }
                        break;
                    case 2:
                        if (s2.equals(switchState)){
                            initBean.isForceRealName = true;
                        }
                        break;
                    case 3:
                        if (s2.equals(switchState)){
                            initBean.isShowKf = true;
                        }
                        break;
                    case 4:
                        if (s2.equals(switchState)){
                            initBean.isSMSBlocking = true;
                        }
                        break;

                }

            }

        }

    }
}