export class SlotRankingDataManager {
    static _instance: SlotRankingDataManager = null;

    static get instance(): SlotRankingDataManager {
        if (!this._instance) {
            this._instance = new SlotRankingDataManager();
        }
        return this._instance;
    }

    _rankList: any = null;

    set rankList(list: any) { this._rankList = list; }
    get rankList(): any { return this._rankList; }

    getRankMaxCloseTimeIsBiggerThanCurTime(): boolean {
        if (!this._rankList || !this._rankList.List || this._rankList.List.length === 0) {
            // 代表当前没有活动开启，则说明关闭入口按钮
            return false;
        }

        let maxCloseTime = 0;
        for (let item of this._rankList.List) {
            if (item.CloseTime > maxCloseTime) {
                maxCloseTime = item.CloseTime;
            }
        }

        return Date.now() <= (maxCloseTime * 1000);
    }

    getRankListStatusByType(type: string): number {
        if (!this._rankList || !this._rankList.List || this._rankList.List.length === 0) {
            return 2;
        }

        let item = this._rankList.List.find((item: any) => item.Type === type);
        return item ? item.Status : 2;
    }


    getRankListStatus(): number {
        if (!this._rankList || !this._rankList.List || this._rankList.List.length === 0) {
            return 2;
        }

        let hasEnabled = false;  // 是否有启用的
        let hasMaintenance = false;  // 是否有维护的

        for (let item of this._rankList.List) {
            if (item.Status === 0) {
                hasEnabled = true;
                break;
            } else if (item.Status === 1) {
                hasMaintenance = true;
            }
        }

        if (hasEnabled) {
            return 0;
        }

        if (hasMaintenance) {
            return 1;
        }

        return 2;
    }

    getRankListServerId(): string | null {
        if (!this._rankList || !this._rankList.List || this._rankList.List.length === 0) {
            return null;
        }

        let id = this._rankList.List[0].Id;
        if (!id) {
            return null;
        }

        let parts = id.split('_');
        if (parts.length >= 3) {
            return parts[2];
        }

        return null;
    }

    getRankEndTimeByType(type: string): number | null {
        if (!this._rankList || !this._rankList.List || this._rankList.List.length === 0) {
            return null;
        }

        let item = this._rankList.List.find((item: any) => item.Type === type);
        return item ? item.EndTime : null;
    }

    getRankInfoByType(type: string): any | null {
        if (!this._rankList || !this._rankList.List || this._rankList.List.length === 0) {
            return null;
        }

        let item = this._rankList.List.find((item: any) => item.Type === type);
        return item || null;
    }
}
