import LocalStorage from "../../Data/LocalStorage";
import LuckySpinScreen from "../Screen/LuckySpinScreen";
import UIManager from "../UIManager";
import PopupUI, { PopupName } from "./PopupUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RewardPopup extends PopupUI {

    @property(cc.Sprite)
    iconSprite: cc.Sprite = null;

    @property(cc.Label)
    txtCount: cc.Label = null;

    @property(cc.SpriteFrame)
    spriteHint: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    spriteUndo: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    spriteGem10: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    spriteGem20: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    spriteGem30: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    spriteGem40: cc.SpriteFrame = null;

    data = null;
    luckySpin: LuckySpinScreen = null;

    override initialize(manager: UIManager): void {
        super.initialize(manager);
        this.popupName = PopupName.REWARD;
    }

    hide(): void {
        super.hide();
    }

    show(): void {
        super.show();
    }

    setUp(data, luckySpin) {
        this.luckySpin = luckySpin
        this.data = data;
        console.log(data);
        switch (data.type) {
            case 'UNDO':
                this.iconSprite.spriteFrame = this.spriteUndo;
                this.txtCount.string = data.count;
                break;
            case 'HINT':
                this.iconSprite.spriteFrame = this.spriteHint;
                this.txtCount.string = data.count;
                break;
            case 'GEM':
                console.log(data.count);
                if (data.count == 10) {
                    console.log(data.count);
                    this.iconSprite.spriteFrame = this.spriteGem10;
                    this.txtCount.string = "+" + data.count;
                } else if (data.count == 20) {
                    this.iconSprite.spriteFrame = this.spriteGem20;
                    this.txtCount.string = "+" + data.count;
                } else if (data.count == 30) {
                    this.iconSprite.spriteFrame = this.spriteGem30;
                    this.txtCount.string = "+" + data.count;
                } else if (data.count == 40) {
                    this.iconSprite.spriteFrame = this.spriteGem40;
                    this.txtCount.string = "+" + data.count;
                }
                break;
        }
    }

    onClickClaim() {
        this.claim(false);
    }

    onClickClaimX2() {
        this.claim(true);
    }

    claim(isX2: boolean) {
        this.hide();
        this.luckySpin.isRotary = true;
        let count = this.data.count;
        if (isX2) {
            count = this.data * 2;
        }
        switch (this.data.type) {
            case 'UNDO':
                let undo = LocalStorage.getUndo();
                LocalStorage.setUndo(undo + count);
                break;
            case 'HINT':
                let hint = LocalStorage.getHint();
                LocalStorage.setHint(hint + count);
                break;
            case 'GEM':
                let gem = LocalStorage.getGem();
                LocalStorage.setGem(gem + count);
        }
    }
}
