package com.tencent.qian.webview.model;

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

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * Tencent Qian H5 容器配置。
 *
 * <p>全部字段带默认值，客户最少 0 配置即可跑通签署流程。
 * 与 iOS {@code TencentQianWebViewConfig} struct 字段 1:1 对齐。
 *
 * <p>典型用法：
 * <pre>{@code
 * TencentQianWebViewConfig config = TencentQianWebViewConfig.builder()
 *     .topBarTitle("电子签署")
 *     .externalSchemes(Arrays.asList("weixin", "alipays", "mqqapi"))
 *     .userAgentExtra("myapp/3.2.1")
 *     .build();
 * TencentQianWebView.open(activity, signUrl, config, callback);
 * }</pre>
 */
public final class TencentQianWebViewConfig {

    // ────────── 嵌套枚举 ──────────

    public enum StatusBarStyle { LIGHT_CONTENT, DARK_CONTENT }

    public enum CameraCaptureBehavior {
        PREFER_FRONT,
        PREFER_BACK,
        SYSTEM_DEFAULT,
        DISABLED
    }

    public enum MixedContentPolicy {
        NEVER,
        ALWAYS,
        COMPATIBILITY
    }

    public enum JsDialogStyle { SYSTEM, MATERIAL }

    // ────────── 静态常量 ──────────

    public static final String DEFAULT_COMPATIBILITY_TEST_URL =
            "https://quick.beta.qian.tencent.cn/compatibilityTest";

    public static final List<String> DEFAULT_LEGACY_FACE_CAPTURE_MIME_TYPES =
            Collections.unmodifiableList(Arrays.asList("video/kyc", "video/faceid"));

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

    // ────────── 私有字段 ──────────

    private final boolean showTopBar;
    @NonNull private final String topBarTitle;
    @Nullable @ColorInt private final Integer topBarBackgroundColor;
    @Nullable @ColorInt private final Integer topBarTextColor;
    private final boolean showCloseButton;
    @NonNull private final StatusBarStyle statusBarStyle;
    private final boolean injectUserAgent;
    @NonNull private final String userAgentExtra;
    @NonNull private final MixedContentPolicy allowMixedContent;
    private final boolean confirmBeforeClose;
    @NonNull private final String confirmCloseTitle;
    @NonNull private final String confirmCloseMessage;
    @NonNull private final List<String> externalSchemes;
    @Nullable private final String compatibilityTestUrl;
    @NonNull private final JsDialogStyle jsDialogStyle;
    private final boolean downloadWithCookies;
    @NonNull private final List<String> legacyFaceCaptureMimeTypes;
    @NonNull private final CameraCaptureBehavior cameraCaptureBehavior;

    private TencentQianWebViewConfig(@NonNull Builder builder) {
        this.showTopBar = builder.showTopBar;
        this.topBarTitle = builder.topBarTitle;
        this.topBarBackgroundColor = builder.topBarBackgroundColor;
        this.topBarTextColor = builder.topBarTextColor;
        this.showCloseButton = builder.showCloseButton;
        this.statusBarStyle = builder.statusBarStyle;
        this.injectUserAgent = builder.injectUserAgent;
        this.userAgentExtra = builder.userAgentExtra;
        this.allowMixedContent = builder.allowMixedContent;
        this.confirmBeforeClose = builder.confirmBeforeClose;
        this.confirmCloseTitle = builder.confirmCloseTitle;
        this.confirmCloseMessage = builder.confirmCloseMessage;
        this.externalSchemes = Collections.unmodifiableList(builder.externalSchemes);
        this.compatibilityTestUrl = builder.compatibilityTestUrl;
        this.jsDialogStyle = builder.jsDialogStyle;
        this.downloadWithCookies = builder.downloadWithCookies;
        this.legacyFaceCaptureMimeTypes = Collections.unmodifiableList(builder.legacyFaceCaptureMimeTypes);
        this.cameraCaptureBehavior = builder.cameraCaptureBehavior;
    }

    // ────────── Getters ──────────

    public boolean isShowTopBar() { return showTopBar; }
    @NonNull public String getTopBarTitle() { return topBarTitle; }
    @Nullable @ColorInt public Integer getTopBarBackgroundColor() { return topBarBackgroundColor; }
    @Nullable @ColorInt public Integer getTopBarTextColor() { return topBarTextColor; }
    public boolean isShowCloseButton() { return showCloseButton; }
    @NonNull public StatusBarStyle getStatusBarStyle() { return statusBarStyle; }
    public boolean isInjectUserAgent() { return injectUserAgent; }
    @NonNull public String getUserAgentExtra() { return userAgentExtra; }
    @NonNull public MixedContentPolicy getAllowMixedContent() { return allowMixedContent; }
    public boolean isConfirmBeforeClose() { return confirmBeforeClose; }
    @NonNull public String getConfirmCloseTitle() { return confirmCloseTitle; }
    @NonNull public String getConfirmCloseMessage() { return confirmCloseMessage; }
    @NonNull public List<String> getExternalSchemes() { return externalSchemes; }
    @Nullable public String getCompatibilityTestUrl() { return compatibilityTestUrl; }
    @NonNull public JsDialogStyle getJsDialogStyle() { return jsDialogStyle; }
    public boolean isDownloadWithCookies() { return downloadWithCookies; }
    @NonNull public List<String> getLegacyFaceCaptureMimeTypes() { return legacyFaceCaptureMimeTypes; }
    @NonNull public CameraCaptureBehavior getCameraCaptureBehavior() { return cameraCaptureBehavior; }

    // ────────── Builder ──────────

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

    public static final class Builder {
        private boolean showTopBar = true;
        @NonNull private String topBarTitle = "";
        @Nullable private Integer topBarBackgroundColor = null;
        @Nullable private Integer topBarTextColor = null;
        private boolean showCloseButton = true;
        @NonNull private StatusBarStyle statusBarStyle = StatusBarStyle.DARK_CONTENT;
        private boolean injectUserAgent = true;
        @NonNull private String userAgentExtra = "";
        @NonNull private MixedContentPolicy allowMixedContent = MixedContentPolicy.NEVER;
        private boolean confirmBeforeClose = false;
        @NonNull private String confirmCloseTitle = "";
        @NonNull private String confirmCloseMessage = "";
        @NonNull private List<String> externalSchemes = Arrays.asList("weixin", "alipays");
        @Nullable private String compatibilityTestUrl = null;
        @NonNull private JsDialogStyle jsDialogStyle = JsDialogStyle.MATERIAL;
        private boolean downloadWithCookies = false;
        @NonNull private List<String> legacyFaceCaptureMimeTypes =
                Arrays.asList("video/kyc", "video/faceid");
        @NonNull private CameraCaptureBehavior cameraCaptureBehavior = CameraCaptureBehavior.PREFER_FRONT;

        public Builder() {}

        @NonNull public Builder showTopBar(boolean v) { this.showTopBar = v; return this; }
        @NonNull public Builder topBarTitle(@NonNull String v) { this.topBarTitle = v; return this; }
        @NonNull public Builder topBarBackgroundColor(@Nullable @ColorInt Integer v) { this.topBarBackgroundColor = v; return this; }
        @NonNull public Builder topBarTextColor(@Nullable @ColorInt Integer v) { this.topBarTextColor = v; return this; }
        @NonNull public Builder showCloseButton(boolean v) { this.showCloseButton = v; return this; }
        @NonNull public Builder statusBarStyle(@NonNull StatusBarStyle v) { this.statusBarStyle = v; return this; }
        @NonNull public Builder injectUserAgent(boolean v) { this.injectUserAgent = v; return this; }
        @NonNull public Builder userAgentExtra(@NonNull String v) { this.userAgentExtra = v; return this; }
        @NonNull public Builder allowMixedContent(@NonNull MixedContentPolicy v) { this.allowMixedContent = v; return this; }
        @NonNull public Builder confirmBeforeClose(boolean v) { this.confirmBeforeClose = v; return this; }
        @NonNull public Builder confirmCloseTitle(@NonNull String v) { this.confirmCloseTitle = v; return this; }
        @NonNull public Builder confirmCloseMessage(@NonNull String v) { this.confirmCloseMessage = v; return this; }
        @NonNull public Builder externalSchemes(@NonNull List<String> v) { this.externalSchemes = v; return this; }
        @NonNull public Builder compatibilityTestUrl(@Nullable String v) { this.compatibilityTestUrl = v; return this; }
        @NonNull public Builder jsDialogStyle(@NonNull JsDialogStyle v) { this.jsDialogStyle = v; return this; }
        @NonNull public Builder downloadWithCookies(boolean v) { this.downloadWithCookies = v; return this; }
        @NonNull public Builder legacyFaceCaptureMimeTypes(@NonNull List<String> v) { this.legacyFaceCaptureMimeTypes = v; return this; }
        @NonNull public Builder cameraCaptureBehavior(@NonNull CameraCaptureBehavior v) { this.cameraCaptureBehavior = v; return this; }

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

    // ────────── equals / hashCode / toString ──────────

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof TencentQianWebViewConfig)) return false;
        TencentQianWebViewConfig that = (TencentQianWebViewConfig) o;
        return showTopBar == that.showTopBar
                && showCloseButton == that.showCloseButton
                && injectUserAgent == that.injectUserAgent
                && confirmBeforeClose == that.confirmBeforeClose
                && downloadWithCookies == that.downloadWithCookies
                && Objects.equals(topBarTitle, that.topBarTitle)
                && Objects.equals(topBarBackgroundColor, that.topBarBackgroundColor)
                && Objects.equals(topBarTextColor, that.topBarTextColor)
                && statusBarStyle == that.statusBarStyle
                && Objects.equals(userAgentExtra, that.userAgentExtra)
                && allowMixedContent == that.allowMixedContent
                && Objects.equals(confirmCloseTitle, that.confirmCloseTitle)
                && Objects.equals(confirmCloseMessage, that.confirmCloseMessage)
                && Objects.equals(externalSchemes, that.externalSchemes)
                && Objects.equals(compatibilityTestUrl, that.compatibilityTestUrl)
                && jsDialogStyle == that.jsDialogStyle
                && Objects.equals(legacyFaceCaptureMimeTypes, that.legacyFaceCaptureMimeTypes)
                && cameraCaptureBehavior == that.cameraCaptureBehavior;
    }

    @Override
    public int hashCode() {
        return Objects.hash(showTopBar, topBarTitle, topBarBackgroundColor, topBarTextColor,
                showCloseButton, statusBarStyle, injectUserAgent, userAgentExtra,
                allowMixedContent, confirmBeforeClose, confirmCloseTitle, confirmCloseMessage,
                externalSchemes, compatibilityTestUrl, jsDialogStyle, downloadWithCookies,
                legacyFaceCaptureMimeTypes, cameraCaptureBehavior);
    }

    @Override
    @NonNull
    public String toString() {
        return "TencentQianWebViewConfig{" +
                "showTopBar=" + showTopBar +
                ", topBarTitle='" + topBarTitle + '\'' +
                ", statusBarStyle=" + statusBarStyle +
                ", confirmBeforeClose=" + confirmBeforeClose +
                ", jsDialogStyle=" + jsDialogStyle +
                '}';
    }
}
