package cn.yyxx.eyuangame.core.entity.bean.init

import android.text.TextUtils
import cn.yyxx.eyuangame.base.utils.Logger
import cn.yyxx.support.JsonUtils
import org.json.JSONException
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2021/06/19
 */
class InitBean private constructor() {

    lateinit var noticeCfg: NoticeCfg
    lateinit var privacyCfg: PrivacyCfg
    lateinit var floatCfg: FloatCfg
    var rewardId = ""
    val areaCodeList = mutableListOf<String>()

    override fun toString(): String {
        return "InitBean(noticeCfg=$noticeCfg, privacyCfg=$privacyCfg, floatCfg=$floatCfg, rewardId='$rewardId')"
    }

    companion object {
        fun toBean(json: String): InitBean {
            val bean = InitBean()
            bean.noticeCfg = NoticeCfg()
            bean.privacyCfg = PrivacyCfg()
            bean.floatCfg = FloatCfg()

            if (TextUtils.isEmpty(json)) {
                return bean
            }

            try {
                val jsonObject = JSONObject(json)

                if (JsonUtils.hasJsonKey(jsonObject, "notice_cfg")) {
                    val cfg = jsonObject.getJSONObject("notice_cfg")
                    if (JsonUtils.hasJsonKey(cfg, "cfg_s")) {
                        bean.noticeCfg.switch = cfg.getInt("cfg_s")
                    }

                    if (JsonUtils.hasJsonKey(cfg, "line")) {
                        bean.noticeCfg.url = cfg.getString("line")
                    }

                    if (JsonUtils.hasJsonKey(cfg, "show_count")) {
                        bean.noticeCfg.showCount = cfg.getInt("show_count")
                    }
                }

                if (JsonUtils.hasJsonKey(jsonObject, "privacy_cfg")) {
                    val cfg = jsonObject.getJSONObject("privacy_cfg")
                    if (JsonUtils.hasJsonKey(cfg, "cfg_s")) {
                        bean.privacyCfg.switch = cfg.getInt("cfg_s")
                    }

                    if (JsonUtils.hasJsonKey(cfg, "line")) {
                        bean.privacyCfg.url = cfg.getString("line")
                    }
                }

                if (JsonUtils.hasJsonKey(jsonObject, "float_cfg")) {
                    val cfg = jsonObject.getJSONObject("float_cfg")
                    bean.floatCfg = FloatCfg.toBean(cfg)
                }

                if (JsonUtils.hasJsonKey(jsonObject, "reward_id")) {
                    bean.rewardId = jsonObject.getString("reward_id")
                }
                if (JsonUtils.hasJsonKey(jsonObject, "area_code_list")) {
                    val list = jsonObject.getJSONArray("area_code_list")
                    for (i in 0 until list.length()) {
                        bean.areaCodeList.add(list[i].toString())
                    }
                }
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            return bean
        }

    }

    class NoticeCfg {

        var switch = 0
        var url = ""
        var showCount = 0

        override fun toString(): String {
            return "NoticeCfg(switch=$switch, url='$url', showCount=$showCount)"
        }

    }

    class PrivacyCfg {
        var switch = 0
        var url = ""

        override fun toString(): String {
            return "PrivacyCfg(switch=$switch, url='$url')"
        }

    }

}