/**
 * @description 福袋item
 * @author kaka
 */

const { ccclass, property } = cc._decorator;

@ccclass
export default class BlessingBagItem extends cc.Component{

    @property(cc.Label)
    txtNum: cc.Label = null;
    @property(cc.RichText)
    txtTip: cc.RichText = null;
    @property(cc.Label)
    txtSlider: cc.Label = null;

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

    @property(cc.Node)
    btnCashed: cc.Node = null;
    @property(cc.Node)
    btnCanCash: cc.Node = null;
    @property(cc.Node)
    btnNoFinish: cc.Node = null;

    data: any = null;
    lastNum: number = 0;
    totalTime: number = 0;
    deltaTime: number = 0;
    delta: number = 0;
    curValue: number = 0;
    curTime: number = 0;

    isPlayAni: boolean = false;

    /**
     * list_data
     * @param data item数据
     */
    public async setItemData(bData) {
        this.Init(bData.data, bData.isPlayAniUpdate, bData.isPlayInitAni);
    }

    Init(data: any, isPlayAni: boolean = false, isPlayInitAni: boolean = false) {
        this.data = data;
        this.txtNum.string = data.redMoney + "元";
        this.txtTip.string = "<outline color=#8C1010 width=4><color=#FFE00C>" + data.redMoney + "元</c>提现进度</outline>";
        this.btnCashed.active = data.status == 2;
        this.btnCanCash.active = data.status == 1;
        this.btnNoFinish.active = data.status == 0;

        if (isPlayAni) {
            this.scheduleOnce(() => {
                this.PlayNumAni(0.3);
                cc.tween(this.txtSlider.node).to(0.15, { scale: 1.2 })
                    .to(0.15, { scale: 1 })
                    .start();
            }, 0.3);
        }
        else if (isPlayInitAni) {
            this.scheduleOnce(() => {
                this.PlayInitNumAni(0.2);
                cc.tween(this.txtSlider.node).to(0.1, { scale: 1.2 })
                    .to(0.1, { scale: 1 })
                    .start();
            }, 0.3);
        } else {
            this.txtSlider.string = data.progressRate + "%";
            this.slider.fillRange = data.progressRate * 0.01;
            this.lastNum = data.progressRate;
        }
    }

    update(dt) {
        this.UpdateAniFrame(dt);
    }

    PlayNumAni(time: number) {
        this.totalTime = time;
        this.delta = (this.data.progressRate - this.lastNum) / time;
        this.deltaTime = time / (this.data.progressRate - this.lastNum);
        this.curValue = this.lastNum;
        this.curTime = 0;
        this.isPlayAni = true;
    }

    PlayInitNumAni(time: number) {
        this.totalTime = time;
        this.delta = (this.data.progressRate - 0) / time;
        this.deltaTime = time / (this.data.progressRate - 0);
        this.curValue = 0;
        this.curTime = 0;

        this.isPlayAni = true;
    }

    UpdateAni() {
        if (this.curTime < this.totalTime) {
            this.curTime += this.deltaTime;
            this.curValue += this.delta * this.deltaTime;
            if (this.curValue >= this.data.progressRate) {
                this.curValue = this.data.progressRate;
            }
        } else {
            this.curTime = this.totalTime;
            this.unschedule(this.UpdateAni);
            this.curValue = this.data.progressRate;
            this.lastNum = this.data.progressRate;
        }
        this.txtSlider.string = this.curValue.toFixed(2) + "%";
        this.slider.fillRange = this.curValue * 0.01;
    }

    UpdateAniFrame(dt) {
        if (this.isPlayAni) {
            if (this.curTime < this.totalTime) {
                this.curTime += dt;
                this.curValue += this.delta * dt;
                if (this.curValue >= this.data.progressRate) {
                    this.isPlayAni = false;
                    this.curValue = this.data.progressRate;
                    this.lastNum = this.data.progressRate;
                }
                //if (this.data.redMoneyId == 1) {
                //console.log("-->CurValue:" + this.curValue);
                //}
            } else {
                this.isPlayAni = false;
                this.curTime = this.totalTime;
                //this.unschedule(this.UpdateAni);
                this.curValue = this.data.progressRate;
                this.lastNum = this.data.progressRate;
            }
            this.txtSlider.string = this.curValue.toFixed(2) + "%";
            this.slider.fillRange = this.curValue * 0.01;
        }
    }

    OnExit() {
        this.txtSlider.string = "0.00%";
        this.slider.fillRange = 0;
    }


    Click_CashedBtn() {
        mk.audio.playEffect("button");
        console.log("clicked cashed");
        mk.tip.pop('该红包已提现');
    }

    Click_CanCashBtn() {
        mk.audio.playEffect("button");
        console.log("clicked canCash");
        gData.blessingBag.OpenTaskRbPanel(this.data.redMoney, this.data.redMoneyId, 2, this.data.redMoney);
    }

    Click_NoFinishBtn() {
        mk.audio.playEffect("button");
        console.log("clicked noFinish");
        mk.tip.pop('提现进度不足');
    }
}
