import CommonUtils from "../utils/CommonUtils";
import TornStyleBlock from "../utils/TornStyleBlock";
import TornStyleSwitch from "../utils/TornStyleSwitch";
import ResponseInject from "../../interface/ResponseInject";
import globVars from "../../globVars";
import IUserProfileData from "../../interface/IUserProfileData";
import ClassName from "../../container/ClassName";
import { Injectable } from "../../container/Injectable";
import LocalConfigWrapper from "../LocalConfigWrapper";
import Logger from "../Logger";
import IFeature from "../../man/IFeature";

@ClassName('ProfileHelper')
@Injectable()
export default class ProfileHelper implements ResponseInject, IFeature {
    private block: TornStyleBlock;

    constructor(
        private readonly localConfigWrapper: LocalConfigWrapper,
        private readonly commonUtils: CommonUtils,
        private readonly logger: Logger,
    ) {
    }

    description(): string {
        return "个人资料页面辅助";
    }

    iStart(): void | Promise<void> {
        this.start()
    }

    urlExcludes(): RegExp[] {
        return [];
    }

    // 曾用名已检测过标记
    private task = true;

    urlIncludes(): RegExp[] {
        return [/profiles\.php\?XID=/];
    }

    start() {

        CommonUtils.addStyle('body.wh-hide_profile_img .profile-image a.profile-image-wrapper .img-wrap img{display:none;}');
        // let id = document.querySelector('link[rel="canonical"]').getAttribute('href').split('=')[1];
        let id = (new URL(window.location.href)).searchParams.get('XID');
        // id获取格式判断
        if (!this.commonUtils.isValidUid(id)) {
            this.logger.error('[ProfileHelper] id格式错误');
        }
        if (this.localConfigWrapper.config.HideProfileImg) {
            this.logger.info('[ProfileHelper] 隐藏头像');
            document.body.classList.toggle('wh-hide_profile_img');
        }
        this.block = new TornStyleBlock('芜湖助手').insert2Dom();
        // 隐藏头像
        let hideImgSwitch = new TornStyleSwitch('隐藏头像', this.localConfigWrapper.config.HideProfileImg);
        this.block.append(hideImgSwitch.getBase());
        hideImgSwitch.getInput().addEventListener('change', () => {
            document.body.classList.toggle('wh-hide_profile_img');
            this.localConfigWrapper.config.HideProfileImg = hideImgSwitch.getInput().checked;
        });
        if (this.localConfigWrapper.config.ShowNameHistory) {
            globVars.responseHandlers.push((...args: any[]) => this.responseHandler.apply(this, args));
        }
    }

    responseHandler(url: string, body: { json: unknown; text: string; isModified: boolean }) {
        if (url.includes('profiles.php?step=getProfileData') && this.task) {
            // 曾用名
            let nameHistoryNode;
            nameHistoryNode = document.createElement('p');
            nameHistoryNode.innerHTML = '曾用名：';
            this.block.append(nameHistoryNode);
            let resp = body.json as IUserProfileData;
            if (resp.userInformation.previousAliases.length > 0) {
                resp.userInformation.previousAliases.forEach(item => nameHistoryNode.innerHTML += item + ' ');
            } else {
                nameHistoryNode.innerHTML += '暂无';
            }
            this.task = false;
        }
    }
}
