package com.kdanmobile.android.lib.material.tokens.category

import com.kdanmobile.android.lib.material.tokens.TokenJsonType
import com.kdanmobile.android.lib.material.tokens.base.logE
import org.json.JSONObject
import java.io.File

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/11/9
 * description:
 */
class MdParseUtil(var tokenJsonType: TokenJsonType, private var filePath: String, var destFolderPath: String) {


    fun start() {

        try {
            val file = File(filePath)
            val json = file.readText()
            parseCategoryMap(json)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun parseCategoryMap(json: String): HashMap<String, TokenBean> {
        val jsonObj = JSONObject(json)
        val map = getTokensCategoryData(jsonObj)
        for (entry in map) {
            val key = entry.key.replace("-", "_")
            val parse = ParseTokenFactory.factory(tokenJsonType = tokenJsonType, key)
            parse?.parse(key, entry.value)
            parse?.generateTokenFile(ParseTokenFactory.getSavePathByCategory(tokenJsonType, destFolderPath, key))
            if (parse == null) {
                logE("解析器", "未解析tokens类型:${entry.key}")
            }
        }


        return hashMapOf()
    }

    /**
     * 获取Token分类数据
     */
    private fun getTokensCategoryData(obj: JSONObject): Map<String, JSONObject> {

        val iterator = obj.keys()
        val maps = mutableMapOf<String, JSONObject>()
        for (any in iterator) {
            val valueObj = obj.get(any.toString())
            maps[any.toString()] = valueObj as JSONObject
        }
        println(maps)
        return maps
    }


    /**
     * 解析尺寸
     */
    private fun parseSizing(json: String): List<Pair<String, String>> {

        return emptyList()
    }

}

data class TokenBean(
    var type: String,
    var value: String,
    var currentLevelTokenName: String,
    var parentLevelTokenNames: List<String>
)