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

import com.kdanmobile.android.lib.material.tokens.base.*
import com.kdanmobile.android.lib.material.tokens.category.IParseToken
import com.kdanmobile.android.lib.material.tokens.category.common.CommonXmlParserHandler
import org.json.JSONObject
import java.io.File

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2023/2/13
 * description: 解析BorderWidth相关token
 */
class BorderWidthParse : IParseToken {

    private var result = hashMapOf<String, String>()

    private var tag = "解析BorderWidth"


    override fun parse(rootKey: String, jsonObject: JSONObject) {
        println("开始解析${rootKey}类型Tokens")
        for (itemKey in jsonObject.keys()) {
            val key = itemKey as String
            val value = jsonObject.get(key)
            val names = (value as JSONObject).names()
            if (names.length() == 2 && names.getString(0).equals("type") || names.getString(0).equals("value")) {
                val sizingValue = value.getString("value").replaceInvalidStr()
                result["${rootKey}_${key}"] = sizingValue
            } else {
                parse("${rootKey}_$key", value)
            }
        }


    }


    /**
     * 生成token文件
     */
    override fun generateTokenFile(destFilePath: String?) {

        if (destFilePath.isNullOrEmpty()){
            logT(tag, "保存路径为空---return")
            return
        }

        logT(tag, "开始生成BorderWidth Token文件")
        Log.t(tag, "token文件输出路径:${destFilePath}")

        val file = File(destFilePath)
        val outputData = FileUtil.parseAndMergeOldData(tag, file, CommonXmlParserHandler(XmlParserDataCategory.dimen), result)
            .sortedBy {
                it.second.toInt()
            }.map {
                it.first to it.second +"dp"
            }

        FileUtil.writeTokens(file, XmlParserDataCategory.dimen, outputData)

        logT(tag, "解析生成文件完成-----")

    }



}

