package com.tencent.qian.webview.prewarm;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * {@code warmup-manifest.json} 数据模型（三端结构一致）。由前端构建产出，SDK 只读消费。
 */
public final class WarmupManifest {

    /** 单个业务类型的预热资源集合。 */
    public static final class BizType {
        public final String warmupUrl;
        public final List<String> assets;

        public BizType(String warmupUrl, List<String> assets) {
            this.warmupUrl = warmupUrl;
            this.assets = assets;
        }
    }

    public final String version;
    public final List<String> hosts;
    public final Map<String, BizType> bizTypes;
    @Nullable
    public final List<String> menuBizTypes;

    public WarmupManifest(String version, List<String> hosts, Map<String, BizType> bizTypes, @Nullable List<String> menuBizTypes) {
        this.version = version;
        this.hosts = hosts;
        this.bizTypes = bizTypes;
        this.menuBizTypes = menuBizTypes;
    }

    /** 从 JSON 文本解析；失败返回 {@code null}（best-effort）。 */
    @Nullable
    public static WarmupManifest parse(@NonNull String json) {
        try {
            JSONObject root = new JSONObject(json);
            String version = root.optString("version", "");
            JSONArray hostsArr = root.optJSONArray("hosts");
            if (hostsArr == null) {
                return null;
            }
            List<String> hosts = new ArrayList<>(hostsArr.length());
            for (int i = 0; i < hostsArr.length(); i++) {
                hosts.add(hostsArr.optString(i));
            }
            Map<String, BizType> bizTypes = new HashMap<>();
            JSONObject bizObj = root.optJSONObject("bizTypes");
            if (bizObj != null) {
                Iterator<String> keys = bizObj.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    JSONObject biz = bizObj.optJSONObject(key);
                    if (biz == null) {
                        continue;
                    }
                    String warmupUrl = biz.optString("warmupUrl", "");
                    JSONArray assetsArr = biz.optJSONArray("assets");
                    List<String> assets = new ArrayList<>(assetsArr == null ? 0 : assetsArr.length());
                    if (assetsArr != null) {
                        for (int i = 0; i < assetsArr.length(); i++) {
                            assets.add(assetsArr.optString(i));
                        }
                    }
                    bizTypes.put(key, new BizType(warmupUrl, assets));
                }
            }
            List<String> menuBizTypes = null;
            JSONArray menuArr = root.optJSONArray("menuBizTypes");
            if (menuArr != null) {
                menuBizTypes = new ArrayList<>(menuArr.length());
                for (int i = 0; i < menuArr.length(); i++) {
                    menuBizTypes.add(menuArr.optString(i));
                }
            }
            return new WarmupManifest(version, hosts, bizTypes, menuBizTypes);
        } catch (Throwable t) {
            return null;
        }
    }
}
