// 定义相关类型
interface TradeModel {}
interface PageContainer {}
interface IPage {
    Dispose(): void;
    OwnerContainer: PageContainer;
    SetOwnerContainer(pageContainer: PageContainer): void;
    InstanceId: string;
    SetInstanceId(guid: string): void;
    ViewStateData: { [key: string]: any };
    SetViewStateData(viewStateData: { [key: string]: any }): void;
}

interface EventHandler {
    (sender: any, e: any): void;
}

interface PropertyChangedEventHandler {
    (sender: any, e: { propertyName: string }): void;
}

// 模拟辅助类
class PageHelper {
    static GetTradeModel(sender: any): TradeModel {
        return {} as TradeModel;
    }
    static SetTradeModel(sender: any, value: TradeModel) {}
    static SetOwnerPage(sender: any, page: any) {}
    static GetViewStateData(sender: any): { [key: string]: any } {
        return {};
    }
    static SetViewStateData(sender: any, viewStateData: { [key: string]: any }) {}
}

class PageContainerHelper {
    static GetOwnerContainer(sender: any): PageContainer {
        return {} as PageContainer;
    }
    static SetOwnerContainer(sender: any, pageContainer: PageContainer) {}
    static GetInstanceId(sender: any): string {
        return "";
    }
    static SetInstanceId(sender: any, guid: string) {}
}

class TradePage implements IPage {
    private _ready: EventHandler | null = null;
    private _propertyChanged: PropertyChangedEventHandler | null = null;

    constructor() {
        // 模拟加载事件
        this.triggerLoad();
    }

    get TradeModel(): TradeModel {
        return PageHelper.GetTradeModel(this);
    }

    set TradeModel(value: TradeModel) {
        PageHelper.SetTradeModel(this, value);
    }

    get Ready() {
        return {
            add: (handler: EventHandler) => {
                this._ready = handler;
            },
            remove: (handler: EventHandler) => {
                if (this._ready === handler) {
                    this._ready = null;
                }
            },
        };
    }

    private triggerLoad() {
        PageHelper.SetOwnerPage(this, this);
        if (this._ready) {
            this._ready(this, {});
        }
        this.onTradePageLoaded();
    }

    protected onTradePageLoaded() {}

    Dispose() {}

    get OwnerContainer(): PageContainer {
        return PageContainerHelper.GetOwnerContainer(this);
    }

    SetOwnerContainer(pageContainer: PageContainer) {
        PageContainerHelper.SetOwnerContainer(this, pageContainer);
    }

    get InstanceId(): string {
        return PageContainerHelper.GetInstanceId(this);
    }

    SetInstanceId(guid: string) {
        PageContainerHelper.SetInstanceId(this, guid);
    }

    get ViewStateData(): { [key: string]: any } {
        return PageHelper.GetViewStateData(this);
    }

    SetViewStateData(viewStateData: { [key: string]: any }) {
        if (!viewStateData) {
            viewStateData = {};
        }
        PageHelper.SetViewStateData(this, viewStateData);
    }

    get PropertyChanged() {
        return {
            add: (handler: PropertyChangedEventHandler) => {
                this._propertyChanged = handler;
            },
            remove: (handler: PropertyChangedEventHandler) => {
                if (this._propertyChanged === handler) {
                    this._propertyChanged = null;
                }
            },
        };
    }

    protected raisePropertyChanged(propertyName: string) {
        if (this._propertyChanged) {
            this._propertyChanged(this, { propertyName });
        }
    }
}

export default TradePage;