package com.base.browserwhite.bean

import com.base.browserwhite.utils.AssetUtils.WebsiteJson
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject
import java.lang.reflect.Type


data class WebCategoryBean(
    val id: Int,
    val name: String,
    val iconUrl: String,
)

data class WebSiteGroupBean(
    val category_id: Int = 0,
    val category_name: String = "",
    val items: List<WebSiteBean> = listOf()
)

data class WebSiteBean(
    val id: Int = 0,
    val icon_url: String = "",
    val url: String = "",
    val name: String = "",
    val desc: String = "",
) {
    var localIcon: Int = 0
    var increase = false
    var isAdd: Boolean = false
}

fun defaultValue(name: String): Boolean {
    val defaultWebSiteName =
        arrayOf("Amazon", "Youtube", "ChatGPT", "Bing", "Twitter", "Wikipedia", "Facebook", "Ebay", "Tiktok", "Whatsapp")
    return defaultWebSiteName.contains(name)
}

val webCategoryBeanList by lazy {
    webCategoryBeanList(WebsiteJson)
}

val webSiteGroupBeanList by lazy {
    webSiteGroupBeanList(WebsiteJson)
}

fun webCategoryBeanList(json: String): ArrayList<WebCategoryBean> {

    val jsonObject = JSONObject(json)
    val dataJsonObject = jsonObject.getJSONObject("data")
    val categoryList = dataJsonObject.getJSONArray("category_list")

    val list = arrayListOf<WebCategoryBean>()
    for (i in 0 until categoryList.length()) {
        val webSiteBeanJsonObject = categoryList.getJSONObject(i)
        val id = webSiteBeanJsonObject.getInt("id")
        val name = webSiteBeanJsonObject.getString("name")
        val iconUrl = webSiteBeanJsonObject.getString("icon_url")
        list.add(WebCategoryBean(id, name, iconUrl))
    }

    return list
}

fun webSiteGroupBeanList(json: String): List<WebSiteGroupBean> {
    val jsonObject = JSONObject(json)
    val dataJsonObject = jsonObject.getJSONObject("data")

    val groupsList = dataJsonObject.getJSONArray("groups")

    val webSiteGroupBeanType: Type = object : TypeToken<List<WebSiteGroupBean>>() {}.type

    return Gson().fromJson(groupsList.toString(), webSiteGroupBeanType)
}
