package com.tencent.qian.webview.model;

import androidx.annotation.NonNull;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
 * {@code qianapp://} 协议解析结果。
 *
 * <p>协议形态：{@code qianapp://<custom-path>?flowId=...&action=...&result=...&from=tencent_ess&<extras>}
 */
public final class SignResult {

    /** 全空 fallback，用于非法 URL 解析失败场景。 */
    @NonNull
    public static final SignResult EMPTY = new SignResult("", "", "", "", "", "", Collections.emptyMap());

    @NonNull private final String rawUrl;
    @NonNull private final String customPath;
    @NonNull private final String flowId;
    @NonNull private final String action;
    @NonNull private final String result;
    @NonNull private final String from;
    @NonNull private final Map<String, String> extras;

    public SignResult(
            @NonNull String rawUrl,
            @NonNull String customPath,
            @NonNull String flowId,
            @NonNull String action,
            @NonNull String result,
            @NonNull String from,
            @NonNull Map<String, String> extras
    ) {
        this.rawUrl = rawUrl;
        this.customPath = customPath;
        this.flowId = flowId;
        this.action = action;
        this.result = result;
        this.from = from;
        this.extras = Collections.unmodifiableMap(extras);
    }

    @NonNull public String getRawUrl() { return rawUrl; }
    @NonNull public String getCustomPath() { return customPath; }
    @NonNull public String getFlowId() { return flowId; }
    @NonNull public String getAction() { return action; }
    @NonNull public String getResult() { return result; }
    @NonNull public String getFrom() { return from; }
    @NonNull public Map<String, String> getExtras() { return extras; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SignResult)) return false;
        SignResult that = (SignResult) o;
        return Objects.equals(rawUrl, that.rawUrl)
                && Objects.equals(customPath, that.customPath)
                && Objects.equals(flowId, that.flowId)
                && Objects.equals(action, that.action)
                && Objects.equals(result, that.result)
                && Objects.equals(from, that.from)
                && Objects.equals(extras, that.extras);
    }

    @Override
    public int hashCode() {
        return Objects.hash(rawUrl, customPath, flowId, action, result, from, extras);
    }

    @Override
    @NonNull
    public String toString() {
        return "SignResult{" +
                "rawUrl='" + rawUrl + '\'' +
                ", customPath='" + customPath + '\'' +
                ", flowId='" + flowId + '\'' +
                ", action='" + action + '\'' +
                ", result='" + result + '\'' +
                ", from='" + from + '\'' +
                ", extras=" + extras +
                '}';
    }
}
