package com.cretin.webservices.helper

import com.cretin.webcore.utils.GsonGet
import com.google.gson.internal.LinkedTreeMap
import org.apache.http.util.TextUtils

object PHPFileParserHelper {

    /**
     * 将php源码转换成json
     */
    fun parserPhpToMap(content: String, isSingle: Boolean): MutableList<MutableMap<String, Any>> {
        val map = GsonGet.getGson().fromJson<MutableMap<String, Any>>(content, MutableMap::class.java)
        val list = (map.get("expr") as? LinkedTreeMap<*, *>)?.get("items") as? ArrayList<LinkedTreeMap<*, *>>
        val keyValuesList = mutableListOf<MutableMap<String, Any>>()
        list?.forEach {
            if (isSingle) {
                parserPhpToSingleMap("", it, keyValuesList)
            } else {
                parserPhpToMultiMap("", null, it, keyValuesList)
            }
        }
        return keyValuesList
    }

    /**
     * 生成php文件内容
     */
    fun createPhpFileContent(data: MutableList<MutableMap<String, Any>>?): String {
        val json = GsonGet.getGson().toJson(data)
        return "<?php\n" +
                "\n" +
                "return " + json
            .replace(
                "{\"", "\n" +
                        "    '"
            )
            .replace("\"}", "'")
            .replace("\":\"", "'=>'")
            .replace("\":[", "'=>[")
            .replace(
                "]}", "]"
            ).replace("['", "[\n    '")
            .replace("'=>'", "' => '")
            .replace("','", "',\n    '")
            .replace("]", "\n    ]")
            .replace(",'", ",\n    '")
            .replace("'=>[", "' => [")
            .replace("{},", "")
            .replace("{}", "") + ";"
    }

    /**
     * 将php源码转换成简单单层级map
     */
    private fun parserPhpToSingleMap(
        cacheKey: String,
        item: LinkedTreeMap<*, *>?,
        items: MutableList<MutableMap<String, Any>>
    ) {
        val key = (item?.get("key") as? LinkedTreeMap<*, *>)?.get("value")?.toString() ?: ""
        val tempKey = (if (TextUtils.isEmpty(cacheKey)) key else "$cacheKey@$key")
        val valueKind = (item?.get("value") as? LinkedTreeMap<*, *>)?.get("kind")
        if (valueKind == "string") {
            val value = (item?.get("value") as? LinkedTreeMap<*, *>)?.get("value")?.toString() ?: ""
            items.add(
                mutableMapOf(tempKey to value)
            )
        } else {
            val valueItems =
                (item?.get("value") as? LinkedTreeMap<*, *>)?.get("items") as? ArrayList<LinkedTreeMap<*, *>>
            valueItems?.forEach {
                parserPhpToSingleMap((if (TextUtils.isEmpty(cacheKey)) key else "$cacheKey@$key"), it, items)
            }
        }
    }

    /**
     * 将php源码转换成复杂多层级map
     */
    private fun parserPhpToMultiMap(
        cacheKey: String,
        cacheMap: MutableMap<String, Any>?,
        item: LinkedTreeMap<*, *>?,
        items: MutableList<MutableMap<String, Any>>
    ) {
        val tempCacheMap = cacheMap ?: mutableMapOf()
        val key = (item?.get("key") as? LinkedTreeMap<*, *>)?.get("value")?.toString() ?: ""
        val tempKey = if (TextUtils.isEmpty(cacheKey)) key else "$cacheKey@$key"
        val valueKind = (item?.get("value") as? LinkedTreeMap<*, *>)?.get("kind")
        if (valueKind == "string") {
            val value = (item?.get("value") as? LinkedTreeMap<*, *>)?.get("value")?.toString() ?: ""
            tempCacheMap[tempKey] = value
            if (TextUtils.isEmpty(cacheKey)) {
                items.add(
                    tempCacheMap
                )
            }
        } else {
            val valueItems =
                (item?.get("value") as? LinkedTreeMap<*, *>)?.get("items") as? ArrayList<LinkedTreeMap<*, *>>
            val newList = mutableListOf<MutableMap<String, Any>>()
            valueItems?.forEach {
                val itemMap = mutableMapOf<String, Any>()
                parserPhpToMultiMap(tempKey, itemMap, it, items)
                newList.add(itemMap)
            }
            tempCacheMap[tempKey] = newList
            if (TextUtils.isEmpty(cacheKey))
                items.add(tempCacheMap)
        }
    }

}
