import { PREVIEW } from "cc/env";
import { CfgItem } from "../config/CfgItem";
import { DtoItem } from "../dto/DtoItem";
import { EventType } from "../enum/EventType";
import { SkillName, SkillType } from "../enum/SkillType";
import { EventMgr } from "./EventMgr";
import { ItemMgr } from "./ItemMgr";

export class SkillMgr {

    public static useSkill(skill: SkillType): void {
        const skillId: number = this.getSkillId(skill)
        const cnt: number = ItemMgr.updateItem(skillId, -1)
        EventMgr.emit(EventType.UpdateSkillCnt, skill, cnt)
        switch (skill) {
            case SkillType.FreezeTime:
                EventMgr.emit(EventType.UseSkillFreezeTime)
                break
            case SkillType.EraseGroup:
                EventMgr.emit(EventType.UseSkillEraseGroup)
                break
            case SkillType.RefreshPosition:
                EventMgr.emit(EventType.UseSkillRefreshPosition)
                break
            case SkillType.DoubleCoin:
                EventMgr.emit(EventType.UseSkillDoubleCoin)
                break
        }
    }

    public static addSkill(skill: SkillType, cnt: number = 1): void {
        const skillId: number = this.getSkillId(skill)
        const curCnt: number = ItemMgr.updateItem(skillId, cnt)
        EventMgr.emit(EventType.UpdateSkillCnt, skill, curCnt)
        EventMgr.emit(EventType.AddSkill, skill)
        switch (skill) {
            case SkillType.FreezeTime:
                EventMgr.emit(EventType.AddSkillFreezeTime)
                break
            case SkillType.EraseGroup:
                EventMgr.emit(EventType.AddSkillEraseOneGroup)
                break
            case SkillType.RefreshPosition:
                EventMgr.emit(EventType.AddSkillRefreshPosition) //刷新元素
                break
            case SkillType.DoubleCoin:
                EventMgr.emit(EventType.AddSkillDoubleCoin)
                break
        }
    }

    public static hasSkill(skill: SkillType): boolean {
        const skillId: number = this.getSkillId(skill)
        const cnt: number = ItemMgr.getCnt(skillId)
        return cnt > 0
    }

    public static skillCnt(skill: SkillType): number {
        const skillId: number = this.getSkillId(skill)
        const cnt: number = ItemMgr.getCnt(skillId)
        return cnt
    }

    public static getSkillId(skill: SkillType): number {
        for (let i = 0; i < CfgItem.length; i++) {
            const itemData: DtoItem = CfgItem[i];
            if (itemData?.skillType === skill) return itemData.id
        }
        return -1
    }

}

if (PREVIEW) {
    globalThis.SkillMgr = SkillMgr
}
