import { ELAPSESTIME, REMAININGTIME } from "../../Data/Constans";
import LocalStorage from "../../Data/LocalStorage";
import RewardPopup from "../Popup/PopupReward";
import { PopupName } from "../Popup/PopupUI";
import TimerSpin from "../TimerSpin";
import UIManager from "../UIManager";
import ScreenUI, { ScreenName } from "./ScreenUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LuckySpinScreen extends ScreenUI {

    @property(cc.Node)
    wheel: cc.Node = null;

    @property(cc.Node)
    point: cc.Node = null;

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

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

    @property(cc.Button)
    btnFree: cc.Button = null;

    @property(cc.Button)
    btnSpin: cc.Button = null;

    private isSpinning: boolean = false;
    isRotary: boolean = true;
    isSpin: boolean = true;
    currentRotation = 0;

    minRotation = -10;
    maxRotation = 10;
    rotationSpeed = 150;

    private prizes: { type: string; count: number }[] = [
        { type: "UNDO", count: 1 },
        { type: "GEM", count: 30 },
        { type: "HINT", count: 2 },
        { type: "GEM", count: 20 },
        { type: "UNDO", count: 2 },
        { type: "GEM", count: 10 },
        { type: "HINT", count: 1 },
        { type: "GEM", count: 40 },
    ];

    override initialize(uiManager: UIManager): void {
        super.initialize(uiManager);
        this.screenName = ScreenName.LUCKYSPIN;
    }

    async onSpinButtonClicked() {
        if (this.isSpinning) {
            return;
        }
        this.isRotary = false;
        this.wheel.rotation = 0;
        this.isSpinning = true;
        this.isSpin = true;
        const randomIndex = Math.floor(Math.random() * this.prizes.length);
        const angle = (360 / this.prizes.length) * randomIndex + 3620;
        this.scheduleOnce(()=>{
            this.isSpin = false;
        }, 3.5)
        await new Promise(() => {
            cc.tween(this.wheel)
                .to(4, { angle: angle }, { easing: "sineInOut" })
                .call(() => {
                    const prize = this.prizes[randomIndex];
                    this.isSpinning = false;
                    TimerSpin.Instance.startTimerSpin();
                    this.updateButton();
                    this.scheduleOnce(() => {
                        this.point.angle = 0;
                        let rewardPopup = this.uiManager.showPopup(PopupName.REWARD);
                        rewardPopup.getComponent(RewardPopup).setUp(prize, this);
                    }, 0.7);
                })
                .start();
        });
    }

    update(dt: number) {
        if (this.isRotary) {
            this.wheel.angle += 10 * dt;
        }

        if (this.isSpin) {
            this.currentRotation += this.rotationSpeed * dt;

            if (this.currentRotation > this.maxRotation) {
                this.currentRotation = this.maxRotation;
                this.rotationSpeed = -this.rotationSpeed;
            } else if (this.currentRotation < this.minRotation) {
                this.currentRotation = this.minRotation;
                this.rotationSpeed = -this.rotationSpeed;
            }
            this.point.angle = this.currentRotation;
        }
    }

    updateButton() {
        if (LocalStorage.getIsFreeSpin()) {
            this.activeButtonSpin();
        } else {
            this.activeButtonFree();
        }
    }

    activeButtonSpin() {
        this.btnSpin.node.active = true;
        this.btnFree.node.active = false;
        this.txtTitleTime.node.active = false;
        this.timerLabel.node.active = false;
    }

    activeButtonFree() {
        this.btnFree.node.active = true;
        this.txtTitleTime.node.active = true;
        this.timerLabel.node.active = true;
        this.btnSpin.node.active = false;
    }

    override active(): void {
        super.active();
        this.isRotary = true;
        this.isSpin = false;
        this.updateButton();
    }

    override deactive(): void {
        super.deactive();
    }

}



