import GameMgr from "../FrameWork/Mgr/GameMgr";
import AppPlatform from "../FrameWork/Util/AppPlatform";
import Common from "../FrameWork/Util/Common";
import Common5 from "../Platform/th/Common5";
import TTAPI from "../Platform/tt/TTAPI";
import PhysicalPowerManger from "../ttFrame/manager/PhysicalPowerManger";



/*
 * @Author: YeeChan
 * @Date: 2021-12-22 18:54:23
 * @Description: 无限体力弹窗
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class WxtlUI extends cc.Component {

    protected showNode: cc.Node = null;
    protected btnOk: cc.Node = null;
    protected btnClose: cc.Node = null;
    protected btnBack: cc.Node = null;
    private label: cc.Label = null;

    @property({ displayName: '进度条', type: cc.Sprite })
    protected loadingSpr: cc.Sprite = null;

    @property({ displayName: '星星1', type: cc.Node })
    protected starNode1: cc.Node = null;

    @property({ displayName: '星星2', type: cc.Node })
    protected starNode2: cc.Node = null;

    @property({ displayName: '星星3', type: cc.Node })
    protected starNode3: cc.Node = null;

    private needVideoNum = 3;
    protected start(): void {
        this.needVideoNum = Common5.videoWxtlPowerNum;

        this.showNode = this.node.getChildByName("show");
        this.btnOk = this.showNode.getChildByName("btnOk");
        this.btnClose = this.showNode.getChildByName("btnClose");
        this.btnBack = this.showNode.getChildByName("btnBack");

        this.label = this.btnOk.getChildByName("times_lab").getComponent(cc.Label);

        this.btnOk.on("click", () => {
            let tab = {
                onClose: (finish)=>{
                    if (finish) {
                        if (AppPlatform.is_TT_GAME_custom()) {
                            window["tt"].reportAnalytics('watchAd', {
                                type: '看视频无限体力',
                            });
                        }
                        let num = PhysicalPowerManger.getPhyVideoAllNum();
                        num = num + 1;
                        if (PhysicalPowerManger.getPhyVideoAllTime() == 0) {
                            if (num == 1) {
                                PhysicalPowerManger.setPhysicalNum(PhysicalPowerManger.getPhysicalNum() + Common5.videoFirstGetPowerNum);
                                GameMgr.getInstance_custom().saveGameData_custom();
                            } else if (num == 2) {
                                PhysicalPowerManger.setPhysicalNum(PhysicalPowerManger.getPhysicalNum() + Common5.videoSecondGetPowerNum);
                                GameMgr.getInstance_custom().saveGameData_custom();
                            }
                        }
    
                        if (num >= this.needVideoNum) {
                            Common5.isNoPower = false;
                            num = 0;
                            this.updateStarShow();
                            PhysicalPowerManger.setPhyVideoAllNum(num);
                            PhysicalPowerManger.addNewPhysicalAll();//增加一个24小时无限体力
                            this.starNode1.getChildByName("onTag").active = true;
                            this.starNode2.getChildByName("onTag").active = true;
                            this.starNode3.getChildByName("onTag").active = true;
                            this.loadingSpr.fillRange = 1;
                            GameMgr.getInstance_custom().saveGameData_custom();
                            this.scheduleOnce(() => {
                                this.btnCloseFunc();
                            }, 0.3)
                        } else {
                            PhysicalPowerManger.setPhyVideoAllNum(num);
                            this.updateVideoNum();
                            this.updateStarShow();
                            GameMgr.getInstance_custom().saveGameData_custom();
                        }
                    }
                },onFailed:()=>{
    
                }
            }
            AppPlatform.playVideo_custom(tab)
        }, this);


        this.btnClose.on("click", () => {
            this.btnCloseFunc();
        }, this);

        this.btnBack.on("click", () => {
            this.btnCloseFunc();
        }, this);

        this.updateVideoNum();

        this.starNode1.getChildByName("val_lab").getComponent(cc.Label).string = "+" + Common5.videoFirstGetPowerNum;
        this.starNode2.getChildByName("val_lab").getComponent(cc.Label).string = "+" + Common5.videoSecondGetPowerNum;
        this.updateStarShow();
    }


    /**
     * 更新无限体力看视频的次数
     */
    public updateVideoNum() {
        this.label.string = PhysicalPowerManger.getPhyVideoAllNum() + "/" + this.needVideoNum;
    }

    btnCloseFunc() {
        // console.log("ly+++++++++++++++++  关闭");
        if (Common5.isNoPower) {
            Common.getPrefabFromBundle("ZoomGame", "Prefabs/UI/AddPower", null, (NewPlayerGuider: cc.Node) => {

            })
        }
        this.node.removeFromParent();
    }


    updateStarShow() {
        let num = PhysicalPowerManger.getPhyVideoAllNum();
        if (PhysicalPowerManger.getPhyVideoAllTime() > 0) {
            this.starNode1.getChildByName("val_lab").active = false;
            this.starNode2.getChildByName("val_lab").active = false;
        }
        if (num == 0) {
            this.starNode1.getChildByName("onTag").active = false;
            this.starNode2.getChildByName("onTag").active = false;
            this.starNode3.getChildByName("onTag").active = false;
            this.loadingSpr.fillRange = 0;
        } else if (num == 1) {
            this.starNode1.getChildByName("onTag").active = true;
            this.starNode2.getChildByName("onTag").active = false;
            this.starNode3.getChildByName("onTag").active = false;
            this.loadingSpr.fillRange = 0;
        } else if (num == 2) {
            this.starNode1.getChildByName("onTag").active = true;
            this.starNode2.getChildByName("onTag").active = true;
            this.starNode3.getChildByName("onTag").active = false;
            this.loadingSpr.fillRange = 0.5;
        } else if (num == 3) {
            this.starNode1.getChildByName("onTag").active = true;
            this.starNode2.getChildByName("onTag").active = true;
            this.starNode3.getChildByName("onTag").active = true;
            this.loadingSpr.fillRange = 1;
        }
    }
}