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

const { ccclass, property } = cc._decorator;
/**
 * 转盘
 * @author 邹勇 kaka
 */
@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;

    @property(cc.ParticleSystem)
    starAni: cc.ParticleSystem = null;

    @property(sp.Skeleton)
    baozhaAni: sp.Skeleton = null;

    private audioId: number;

    private endAngle = 0
    private startDraw = false
    private slowDis = 720
    private slowAngle = 0
    private perAdd = 10
    private perMinus = 0

    private rewardWeights: number[];

    onLoad() {
        this.baozhaAni.node.active = false;
        this.initData();
    }

    /**
     * 初始化奖励和抽奖次数
     */
    private initData() {
        this.rewardWeights = [0, 0, 0];
        if (gData.turnable.config) {
            for (let i = 0; i < gData.turnable.config.length; i++) {
                let sc = this.node_items[i].getComponent(TurnableItem);
                let type = parseInt(gData.turnable.config[i].type);
                this.rewardWeights[type - 1]++;
                sc.initData(type);
            }
        }
        gData.turnable.requestData();
        this.updateLeftTimes();
    }

    /** 点击抽奖 */
    clickDraw() {
        if (gData.turnable.leftTimes > 0) {
            mk.ad.watchAd((success: boolean) => {
                mk.console.log("watchAD:" + success);
                if (success) {
                    gData.adData.watchVideo(AdFun.turntable);
                }
            });
        }
    }

    private async play(type) {
        this.audioId = await mk.audio.playEffect("turnableplay", true);
        this.btn_close.getComponent(BtnClosePanel).block_click = true;

        this.perAdd = 10;
        this.node_turn.angle += 360 * 5;
        let index = 0;
        let i = 0;
        while (i < this.rewardWeights.length - 1) {
            if (i < type - 1) {
                index += this.rewardWeights[i];
                i++;
            }
            else {
                index += Math.floor(Math.random() * this.rewardWeights[i]);
                break;
            }
        }
        this.endAngle = index * -60;
        this.slowAngle = this.endAngle + this.slowDis;
        this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5));
        this.startDraw = true;

        this.starAni.resetSystem()
        this.starAni.node.active = true

        // this.node_turn.angle = 1800;
        // let tar = index * 60;
        // cc.tween(this.node_turn)
        //     .to(3, { angle: tar })
        //     .call(this.playOver, this).start();
    }

    private async playOver() {
        mk.audio.stopEffect(this.audioId);
        this.btn_close.getComponent(BtnClosePanel).block_click = false;
        mk.audio.playEffect("turnplateDrawEnd");
        this.updateLeftTimes();
        await mk.time.WaitForSeconds(0.7);
        mk.ui.openPanel("module/reward/reward");
    }

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

    update() {
        if (gData.turnable.requestFinish) {
            this.updateLeftTimes();
            gData.turnable.requestFinish = false;
        }
        if (gData.turnable.adData) {
            if (gData.turnable.adData && gData.turnable.adData.videoRedMoney) {
                let type = gData.turnable.adData.videoRedMoney.redMoneyType;
                this.play(type);
                gData.reward.data = gData.turnable.adData.videoRedMoney.videoRewardList;
            }
            gData.turnable.adData = null;
        }

        if (this.startDraw) {
            this.node_turn.angle -= this.perAdd;
            //开始减速
            if (this.node_turn.angle <= this.slowAngle) {
                if (this.perAdd <= this.perMinus * 4) {
                    this.perAdd -= this.perMinus * 0.01;
                }
                else {
                    this.perAdd -= this.perMinus;
                }
            }

            if (this.node_turn.angle <= this.endAngle) {
                this.startDraw = false;
                this.starAni.node.active = false
                this.baozhaAni.node.active = true
                this.baozhaAni.setAnimation(0, 'animation', false)
                this.playOver();
            }
        }
    }

    onDestroy() {

    }

    onClickClose() {
        mk.ad.checkShowInterByChance();
    }
}