import { ref, reactive, watch } from 'vue';

// 模拟接口和类
interface IPage {
    OwnerContainer: any;
}

interface PageClosedEventArgs {
    ExportArgs: Record<string, any>;
}

class PageContainer {
    CurrentPage: IPage | null = null;
    IsBusy = false;
    AllPagesClosed: ((sender: any, e: PageClosedEventArgs) => void) | null = null;

    PushPage(page: IPage) {
        this.CurrentPage = page;
    }
}

class PageMessageBox {
    Show(page: IPage, message: string, title: string, button: any, image: any, defaultResult: any) {
        // 模拟用户选择
        return confirm(message) ? 'Yes' : 'No';
    }
}

class FocusManager {
    CurrentFocusedElement: any = null;
    IsFocusContinue = false;

    SetFocus(element: any) {
        // 模拟设置焦点
    }

    static GetManagerInstance(element: any) {
        return new FocusManager();
    }
}

class PageDialogBase {
    IsModal = false;
    InternalOpen(ownerPage: IPage, pageContainer: PageContainer) {
        // 模拟打开操作
    }
}

// 定义 PageDialog 类
class PageDialog extends PageDialogBase {
    static flagEsc = ref(false);
    static isNormal = ref(false);

    private _pageContainer = new PageContainer();
    private _result = reactive<Record<string, any>>({});
    private foucusElement: any = null;
    private focusManager: FocusManager | null = null;

    constructor() {
        this._pageContainer.AllPagesClosed = this.PageContainerAllPagesClosed.bind(this);
    }

    PageDialog_MouseLeftButtonDown() {
        // 模拟拖动操作
    }

    private _closeCommand: (() => void) | null = null;

    get CloseCommand() {
        return this._closeCommand || (this._closeCommand = () => this.ShowCloseMessage());
    }

    private ShowCloseMessage() {
        if (!this._pageContainer || this._pageContainer.IsBusy) return;
        const pageMessageBox = new PageMessageBox();
        if (this._pageContainer.CurrentPage && pageMessageBox.Show(this._pageContainer.CurrentPage, "将关闭当前操作窗口,是否确认关闭操作？", "提示信息", null, null, null) === 'Yes') {
            if (!PageDialog.isNormal.value) {
                PageDialog.flagEsc.value = true;
            } else {
                PageDialog.flagEsc.value = false;
            }
            this.Close();
        }
    }

    PageDialog_PreviewKeyDown(key: string) {
        if (key === 'Escape') {
            this.ShowCloseMessage();
        } else {
            PageDialog.flagEsc.value = false;
        }
    }

    OnClosed() {
        if (PageDialog.flagEsc.value) {
            if (this.foucusElement) {
                this.focusManager?.SetFocus(this.foucusElement);
                this.focusManager!.IsFocusContinue = false;
            }
        } else {
            if (this.focusManager) {
                this.focusManager.IsFocusContinue = true;
            }
        }
        PageDialog.isNormal.value = false;
    }

    PageContainerAllPagesClosed(sender: any, e: PageClosedEventArgs) {
        this._pageContainer.AllPagesClosed = null;
        Object.assign(this._result, e.ExportArgs);
        PageDialog.flagEsc.value = false;
        this.Close();
    }

    Open(ownerPage: IPage, page: IPage, titleName: string) {
        this._pageContainer.PushPage(page);
        // 模拟设置标题
        this.focusManager = FocusManager.GetManagerInstance(ownerPage);
        if (this.focusManager) {
            this.foucusElement = this.focusManager.CurrentFocusedElement;
        }
        // 模拟设置位置
        // 模拟模态处理
        this.InternalOpen(ownerPage, this._pageContainer);
        return this._result;
    }

    Close() {
        this.OnClosed();
    }
}

export default PageDialog;