import SHA256 from 'crypto-js/sha256'

type StorageRecord = Record<string, unknown>;

function isStorageRecord(value: unknown): value is StorageRecord {
    return Object.prototype.toString.call(value) === '[object Object]';
}

export function $save<T>(key: string, value: T): void;
export function $save(key: StorageRecord): void;
export function $save<T>(key: string | StorageRecord, value?: T): void {
    if (isStorageRecord(key) && value === undefined) {
        for (const k in key) {
            if (Object.prototype.hasOwnProperty.call(key, k)) {
                localStorage.setItem(k, JSON.stringify(key[k]));
            }
        }
        return;
    }

    if (typeof key !== 'string') {
        return;
    }

    localStorage.setItem(key, JSON.stringify(value));
}

export function $read<T>(key: string, defaultValue: T): T;
export function $read<T>(key: string): T | null;
export function $read<T extends StorageRecord>(key: T): T;
export function $read<T>(key: string | StorageRecord, defaultValue?: T): T | StorageRecord | null {
    if (isStorageRecord(key) && defaultValue === undefined) {
        const result: StorageRecord = {};
        for (const k in key) {
            if (Object.prototype.hasOwnProperty.call(key, k)) {
                result[k] = $read(k, key[k]);
            }
        }
        return result;
    }

    if (typeof key !== 'string') {
        return defaultValue ?? null;
    }

    const value = localStorage.getItem(key);

    if (value === null) {
        return defaultValue ?? null;
    }

    try {
        return JSON.parse(value) as T;
    } catch {
        return defaultValue ?? null;
    }
}

export function $format_str(str: string, num: number) {
    if (str.length - 2 < num) {
        return str;
    }
    return str.substring(0, num) + '...';
}

let toastElement: HTMLDivElement | undefined;
let toastTimer: ReturnType<typeof window.setTimeout> | undefined;

export function $toast(message?: string, duration = 2000) {
    if (!message) {
        return;
    }

    if (!toastElement) {
        toastElement = document.createElement('div');
        toastElement.style.cssText = [
            'position: fixed',
            'left: 50%',
            'top: 45%',
            'z-index: 99999',
            'max-width: 560px',
            'padding: 18px 28px',
            'border-radius: 18px',
            'background: rgba(0, 0, 0, 0.72)',
            'color: #fff',
            'font-size: 22px',
            'line-height: 1.4',
            'text-align: center',
            'transform: translate(-50%, -40%)',
            'opacity: 0',
            'pointer-events: none',
            'transition: opacity 0.2s ease, transform 0.2s ease',
        ].join(';');
        document.body.appendChild(toastElement);
    }

    if (toastTimer) {
        window.clearTimeout(toastTimer);
        toastTimer = undefined;
    }

    toastElement.textContent = message;
    toastElement.style.opacity = '1';
    toastElement.style.transform = 'translate(-50%, -50%)';

    toastTimer = window.setTimeout(() => {
        if (!toastElement) {
            return;
        }

        toastElement.style.opacity = '0';
        toastElement.style.transform = 'translate(-50%, -40%)';
        toastTimer = undefined;
    }, duration);
}

type ConfirmOptions = {
    title?: string;
    message?: string;
    confirmText?: string;
    cancelText?: string;
}

let confirmMaskElement: HTMLDivElement | undefined;
let confirmBoxElement: HTMLDivElement | undefined;
let confirmTitleElement: HTMLDivElement | undefined;
let confirmMessageElement: HTMLDivElement | undefined;
let confirmCancelElement: HTMLButtonElement | undefined;
let confirmOkElement: HTMLButtonElement | undefined;
let confirmResolve: ((value: boolean) => void) | undefined;
let confirmHideTimer: ReturnType<typeof window.setTimeout> | undefined;

function closeConfirm(result: boolean) {
    if (!confirmMaskElement || !confirmBoxElement) {
        confirmResolve?.(result);
        confirmResolve = undefined;
        return;
    }

    if (confirmHideTimer) {
        window.clearTimeout(confirmHideTimer);
        confirmHideTimer = undefined;
    }

    confirmMaskElement.style.opacity = '0';
    confirmBoxElement.style.transform = 'scale(0.96)';
    confirmHideTimer = window.setTimeout(() => {
        if (confirmMaskElement) {
            confirmMaskElement.style.display = 'none';
        }
        confirmHideTimer = undefined;
    }, 200);

    confirmResolve?.(result);
    confirmResolve = undefined;
}

function ensureConfirmElement() {
    if (confirmMaskElement) {
        return;
    }

    confirmMaskElement = document.createElement('div');
    confirmMaskElement.style.cssText = [
        'position: fixed',
        'inset: 0',
        'z-index: 99998',
        'display: none',
        'align-items: center',
        'justify-content: center',
        'background: rgba(0, 0, 0, 0.45)',
        'opacity: 0',
        'transition: opacity 0.2s ease',
    ].join(';');

    confirmBoxElement = document.createElement('div');
    confirmBoxElement.style.cssText = [
        'width: min(560px, calc(100vw - 80px))',
        'box-sizing: border-box',
        'padding: 34px 32px 28px',
        'border-radius: 22px',
        'background: #fff',
        'color: #333',
        'font-size: 24px',
        'line-height: 1.45',
        'text-align: center',
        'box-shadow: 0 18px 48px rgba(0, 0, 0, 0.22)',
        'transform: scale(0.96)',
        'transition: transform 0.2s ease',
    ].join(';');

    confirmTitleElement = document.createElement('div');
    confirmTitleElement.style.cssText = [
        'margin-bottom: 16px',
        'font-size: 30px',
        'font-weight: 700',
        'color: #222',
    ].join(';');

    confirmMessageElement = document.createElement('div');
    confirmMessageElement.style.cssText = [
        'min-height: 34px',
        'word-break: break-word',
    ].join(';');

    const buttonWrap = document.createElement('div');
    buttonWrap.style.cssText = [
        'display: flex',
        'gap: 18px',
        'margin-top: 30px',
    ].join(';');

    confirmCancelElement = document.createElement('button');
    confirmCancelElement.type = 'button';
    confirmCancelElement.style.cssText = [
        'flex: 1',
        'height: 58px',
        'border: 1px solid #ddd',
        'border-radius: 29px',
        'background: #fff',
        'color: #666',
        'font-size: 24px',
    ].join(';');
    confirmCancelElement.onclick = () => closeConfirm(false);

    confirmOkElement = document.createElement('button');
    confirmOkElement.type = 'button';
    confirmOkElement.style.cssText = [
        'flex: 1',
        'height: 58px',
        'border: 0',
        'border-radius: 29px',
        'background: #AA0000',
        'color: #fff',
        'font-size: 24px',
    ].join(';');
    confirmOkElement.onclick = () => closeConfirm(true);

    confirmMaskElement.onclick = (event) => {
        if (event.target === confirmMaskElement) {
            closeConfirm(false);
        }
    };

    buttonWrap.append(confirmCancelElement, confirmOkElement);
    confirmBoxElement.append(confirmTitleElement, confirmMessageElement, buttonWrap);
    confirmMaskElement.appendChild(confirmBoxElement);
    document.body.appendChild(confirmMaskElement);
}

export function $confirm(message?: string, title?: string): Promise<boolean>;
export function $confirm(options?: ConfirmOptions): Promise<boolean>;
export function $confirm(messageOrOptions?: string | ConfirmOptions, title?: string): Promise<boolean> {
    const options: ConfirmOptions = typeof messageOrOptions === 'object'
        ? messageOrOptions
        : { message: messageOrOptions, title };

    if (!options.message) {
        return Promise.resolve(false);
    }

    ensureConfirmElement();

    if (confirmResolve) {
        confirmResolve(false);
        confirmResolve = undefined;
    }

    if (confirmHideTimer) {
        window.clearTimeout(confirmHideTimer);
        confirmHideTimer = undefined;
    }

    confirmTitleElement!.textContent = options.title ?? '提示';
    confirmMessageElement!.textContent = options.message;
    confirmCancelElement!.textContent = options.cancelText ?? '取消';
    confirmOkElement!.textContent = options.confirmText ?? '确定';
    confirmMaskElement!.style.display = 'flex';
    window.requestAnimationFrame(() => {
        if (!confirmMaskElement || !confirmBoxElement) {
            return;
        }

        confirmMaskElement.style.opacity = '1';
        confirmBoxElement.style.transform = 'scale(1)';
    });

    return new Promise<boolean>((resolve) => {
        confirmResolve = resolve;
    });
}

export function $is_run_local(): boolean {
    if (/https?:\/\/localhost|192.168/gim.test(location.href)) {
        return true;
    }
    return false;
}

function $query<T extends string | string[]>(q: T): T {
    const query = location.href.match(/\?([^#]*)/)?.[1]?.replace(/\/$/, '');
    const params = new URLSearchParams(query);
    if (Array.isArray(q)) {
        return q.map((key) => params.get(key)).filter((v): v is string => !!v) as T;
    }
    return params.get(q) as T;
}

export function $getWechat(): any | null {
    const q = $query(['token', 'nickname', 'avatar']);
    if (q && q.length == 3) {
        return {
            token: SHA256(q[0]).toString(), //原token很长,这里压缩下,压缩后不能还原,最后提交成绩时要提交原始token
            token_origin: q[0],
            nickname: q[1],
            avatar: q[2],
        };
    }

    // 调试模式：无真实微信参数时使用模拟数据
    if (__DEBUG__) {
        const mockToken = 'debug_mock_token_' + Date.now()
        return {
            token: SHA256(mockToken).toString(),
            token_origin: mockToken,
            nickname: '测试用户',
            avatar: 'https://picsum.photos/200',
        }
    }

    return null;
}

export async function postJson(token: string, index: number) {
    const url = "https://api.guocai365.org.cn/adminapi/offline_clearance/score/list";

    let arr = ['', 'JGQF', 'JBJF', 'MSYF', 'CMYF', 'QCNF', 'FYDT'];

    try {
        const resp = await fetch(url, {
            method: "POST",
            headers: {
                "Authorization": `Bearer ${token}`,
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                activity_id: 1,
                game_code: arr[index],
                page: 1,
                pageSize: 20,
            }),
        });

        const text = await resp.text();

        if (!resp.ok) {
            console.error("请求失败:", resp.status, text);
            return;
        }

        // 如果返回的是 JSON
        const data = JSON.parse(text);
        console.log("响应:", data);
    } catch (err) {
        console.error("请求异常:", err);
    }
}