package com.tencent.qian.webview.model;

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

/**
 * 预热全局配置（三端字段对齐）。builder 构造，全部字段带默认值。
 */
public final class PrewarmConfig {

    private final PrewarmEnvironment environment;
    @Nullable
    private final String manifestUrl;
    private final int poolSize;
    private final boolean enablePool;
    @Nullable
    private final String preferredLanguage;
    private final boolean injectUserAgent;
    @NonNull
    private final String userAgentExtra;

    private PrewarmConfig(Builder b) {
        this.environment = b.environment;
        this.manifestUrl = b.manifestUrl;
        this.poolSize = b.poolSize;
        this.enablePool = b.enablePool;
        this.preferredLanguage = b.preferredLanguage;
        this.injectUserAgent = b.injectUserAgent;
        this.userAgentExtra = b.userAgentExtra;
    }

    /** 默认配置（全部默认值）。 */
    @NonNull
    public static final PrewarmConfig DEFAULT = new Builder().build();

    @NonNull
    public PrewarmEnvironment getEnvironment() {
        return environment;
    }

    /** 显式 manifest URL 覆盖（逃生舱）；{@code null} 时按 environment 走内置映射。 */
    @Nullable
    public String getManifestUrl() {
        return manifestUrl;
    }

    /** 池容量。v1 有效值恒为 1；> 1 会被 clamp。 */
    public int getPoolSize() {
        return poolSize;
    }

    public boolean isEnablePool() {
        return enablePool;
    }

    /**
     * 预热语言偏好（如 {@code "en-US"} / {@code "zh-CN"}）。客户显式指定要预热的语言资源；
     * {@code null} 时交由 {@code warmup.html} 自动判定。Android 走 L4：非空时以 {@code &lang=}
     * 传入 {@code warmupUrl}（在 warmup.html 内优先级最高）。
     */
    @Nullable
    public String getPreferredLanguage() {
        return preferredLanguage;
    }

    /**
     * 是否注入 SDK UA 标识（{@code qianwv/<version>}，与 {@code TencentQianWebViewConfig} 对齐），默认 true。
     * 池实例按本字段创建/热身 {@code warmupUrl}，保证与 {@code open()} 的 config UA 一致才能命中缓存并复用。
     */
    public boolean isInjectUserAgent() {
        return injectUserAgent;
    }

    /**
     * 客户附加 UA 段，拼接在 {@code qianwv/x.x.x} 之后（与 {@code TencentQianWebViewConfig} 对齐），默认空。
     * 与 {@code open()} 的 config UA 一致时池实例才会被复用。
     */
    @NonNull
    public String getUserAgentExtra() {
        return userAgentExtra;
    }

    @NonNull
    public static Builder builder() {
        return new Builder();
    }

    /** {@link PrewarmConfig} 构建器。 */
    public static final class Builder {
        private PrewarmEnvironment environment = PrewarmEnvironment.PROD;
        @Nullable
        private String manifestUrl = null;
        private int poolSize = 1;
        private boolean enablePool = true;
        @Nullable
        private String preferredLanguage = null;
        private boolean injectUserAgent = true;
        @NonNull
        private String userAgentExtra = "";

        @NonNull
        public Builder environment(@NonNull PrewarmEnvironment environment) {
            this.environment = environment;
            return this;
        }

        @NonNull
        public Builder manifestUrl(@Nullable String manifestUrl) {
            this.manifestUrl = manifestUrl;
            return this;
        }

        @NonNull
        public Builder poolSize(int poolSize) {
            this.poolSize = poolSize;
            return this;
        }

        @NonNull
        public Builder enablePool(boolean enablePool) {
            this.enablePool = enablePool;
            return this;
        }

        @NonNull
        public Builder preferredLanguage(@Nullable String preferredLanguage) {
            this.preferredLanguage = preferredLanguage;
            return this;
        }

        @NonNull
        public Builder injectUserAgent(boolean injectUserAgent) {
            this.injectUserAgent = injectUserAgent;
            return this;
        }

        @NonNull
        public Builder userAgentExtra(@NonNull String userAgentExtra) {
            this.userAgentExtra = userAgentExtra;
            return this;
        }

        @NonNull
        public PrewarmConfig build() {
            return new PrewarmConfig(this);
        }
    }
}
