import { Widget, _decorator, Node, Sprite, SpriteFrame, Label, Tween, tween, v3 } from 'cc';
import { DtoReward } from '../dto/DtoReward';
import { ItemType } from '../enum/ItemType';
import { CoinMgr } from '../manager/CoinMgr';
import { ItemMgr } from '../manager/ItemMgr';
import { ResMgr } from '../manager/ResMgr';
import { InputBlock } from '../misc/InputBlock';
import { UIBase } from '../scriptBase/UIBase';
import { CoinItem } from '../uiItem/CoinItem';
import { CfgItem } from '../config/CfgItem';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UI/UIGetReward')
@requireComponent(Widget)
export class UIGetReward extends UIBase {

    private icon: Sprite = null
    private lbCnt: Label = null
    private coinItem: CoinItem = null

    private rewardArr: DtoReward[] = []

    private tw: Tween<Node>

    private curReward: DtoReward = null
    public set CurReward(reward: DtoReward) {
        this.curReward = reward
        const { itemId, cnt } = reward
        const { bundle, icon, type } = CfgItem[itemId]
        this.lbCnt.string = `x${cnt}`
        const spf: SpriteFrame = ResMgr.getSpriteFrame(bundle, icon)
        this.icon.spriteFrame = spf

        if (this.tw) this.tw.stop()
        this.tw = tween(this.icon.node)
        this.tw.set({ scale: v3() })
        this.tw.to(0.5, { scale: v3(1, 1, 1) }, { easing: 'backOut' })
        this.tw.start()
    }

    protected onLoad(): void {
        this.icon = this.findComp('Icon', Sprite)
        this.lbCnt = this.findComp('LbCnt', Label)
        this.coinItem = this.findComp('CoinItem', CoinItem)
    }

    public onOpen(data?: any): void {
        this.coinItem.init(CoinMgr.CurCoin)
        this.rewardArr = data
        this.CurReward = this.rewardArr.shift()
    }

    public onClose(data?: any): void {

    }

    protected async onBtnTripleGetClick() {
        const len: number = this.rewardArr.length
        if (len <= 0) {
            await this.getItem(3)
            this.close()
        } else {
            await this.getItem(3)
            this.CurReward = this.rewardArr.shift()
        }

    }

    protected async onBtnNormalGetClick() {
        const len: number = this.rewardArr.length
        if (len <= 0) {
            await this.getItem()
            this.close()
        } else {
            await this.getItem()
            this.CurReward = this.rewardArr.shift()
        }
    }

    private getItem(time: number = 1) {
        return new Promise<void>((resolve, reject) => {
            const { itemId, cnt } = this.curReward
            const itemType: ItemType = ItemMgr.getType(itemId)

            this.lbCnt.string = `x${time * cnt}`

            ItemMgr.updateItem(itemId, time * cnt)

            switch (itemType) {
                case ItemType.Coin:
                    this.coinItem.updateCoin(this.coinItem.Coin + time * cnt)
                    InputBlock.setActive(2)
                    this.scheduleOnce(() => {
                        resolve()
                    }, 2)
                    break;
                case ItemType.Skill:
                    resolve()
                    break
                case ItemType.Collection:
                    resolve()
                default:
                    break;
            }
        })
    }

}


