import { AdFun } from "../../../data/AdData";
import TurnableItem from "./TurnableItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Turnable extends cc.Component {
    @property(cc.Node)
    node_turn: cc.Node = null;
    @property(cc.Node)
    node_items: cc.Node[] = [];
    @property(cc.Node)
    btn_draw: cc.Node = null;
    @property(cc.Sprite)
    btn_close: cc.Sprite = null;
    @property(cc.RichText)
    lbl_left: cc.RichText = null;

    private isPlaying: boolean = false;

    onLoad() {
        this.initData();
    }

    /**
     * 初始化奖励和抽奖次数
     */
    private initData() {
        for (let i = 0; i < this.node_items.length; i++) {
            let sc = this.node_items[i].getComponent(TurnableItem);
            sc.initData(i + "");
        }
        this.updateLeftTimes();
    }

    /** 点击抽奖 */
    clickDraw() {
        if (gData.turnable.leftTimes > 0 && !this.isPlaying) {
            this.isPlaying = true;
            g.adData.watchVideo(AdFun.turntable);
        }
    }

    private draw() {
        gData.turnable.leftTimes--;
        this.node_turn.rotation = 1800;
        let index = 2;
        let tar = index * 60;
        cc.tween(this.node_turn)
            .to(3, { rotation: tar }, { easing: 'expoInOut' })
            .call(() => {
                this.updateLeftTimes();
            }, this).start();
    }

    updateLeftTimes(){
        this.lbl_left.string = `剩余：<color=#FFED99>${gData.turnable.leftTimes}</color>次`;
    }

    update() {
        if(g.turnable.)
        if (!gData.turnable.dataFinish) {
            gData.turnable.init();
        }
        else{
            this.initData();
        }
    }
}