package com.yyrh.core.entity.bean;

import android.text.TextUtils;

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

import java.util.ArrayList;
import java.util.List;

import cn.yyxx.support.JsonUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/19
 */
public class InitBean {

    public int sdkLogin = 1;
    public String h5WebHost = "";
    public int bswitch = 0;
    public List<String> regTabs = new ArrayList<>();
    public String agreementPrivacy = "";
    public String serviceCenter = "";
    public String agreementUser = "";

    public static InitBean toBean(String json) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        InitBean bean = new InitBean();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(json);
            if (JsonUtils.hasJsonKey(jsonObject, "sdk_login")) {
                bean.sdkLogin = jsonObject.getInt("sdk_login");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "h5_web_host")) {
                bean.h5WebHost = jsonObject.getString("h5_web_host");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "bswitch")) {
                bean.bswitch = jsonObject.getInt("bswitch");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "reg_tabs")) {
                JSONArray tabs = jsonObject.getJSONArray("reg_tabs");
                if (tabs.length() > 0) {
                    for (int i = 0; i < tabs.length(); i++) {
                        bean.regTabs.add(tabs.getString(i));
                    }
                }
            }
            if (JsonUtils.hasJsonKey(jsonObject, "links")) {
                JSONObject links = jsonObject.getJSONObject("links");
                if (JsonUtils.hasJsonKey(links, "agreement_privacy")) {
                    bean.agreementPrivacy = links.getString("agreement_privacy");
                }
                if (JsonUtils.hasJsonKey(links, "service_center")) {
                    bean.serviceCenter = links.getString("service_center");
                }
                if (JsonUtils.hasJsonKey(links, "agreement_user")) {
                    bean.agreementUser = links.getString("agreement_user");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public String toString() {
        return "InitBean{" +
                "sdkLogin=" + sdkLogin +
                ", h5WebHost='" + h5WebHost + '\'' +
                ", bswitch=" + bswitch +
                ", regTabs=" + regTabs +
                ", agreementPrivacy='" + agreementPrivacy + '\'' +
                ", serviceCenter='" + serviceCenter + '\'' +
                ", agreementUser='" + agreementUser + '\'' +
                '}';
    }
}
