import DataMgr from "../mgr/DataMgr";
import PlayerConst, { DATA_STORAGE_KEY } from "../data/PlayerConst";
import HttpMgr from "../mgr/HttpMgr";
import EffectMgr from "../mgr/EffectMgr";
import GameMgr, { UI_NAME } from "../mgr/GameMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RedPacketUI extends cc.Component {


    @property(cc.Node)
    node_rectBg: cc.Node = null;
    @property(cc.Node)
    node_unopen: cc.Node = null;
    @property(cc.Node)
    node_top: cc.Node = null;
    @property(cc.Node)
    node_bottom: cc.Node = null;

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

    @property(cc.Node)
    node_openBtn: cc.Node = null;
    @property(cc.Animation)
    ani_openBtn: cc.Animation = null;
    @property(cc.Node)
    node_closeBtn: cc.Node = null;

    @property(cc.Node)
    node_open: cc.Node = null;
    @property(cc.Node)
    node_cashInfo: cc.Node = null;
    @property(cc.Label)
    label_redPacketName_unopen: cc.Label = null;
    @property(cc.Label)
    label_redPacketTip: cc.Label = null;
    @property(cc.Label)
    label_cashNum: cc.Label = null;
    @property(cc.Node)
    node_cashOutBtn: cc.Node = null;
    @property(cc.Node)
    node_getCashBtn: cc.Node = null;
    @property(cc.Node)
    node_doubleCashBtn: cc.Node = null;

    public top_oirgin_y: number;
    public top_scale_size: number;
    public bottom_origin_y: number;
    public bottom_scale_size: number;

    public cashNum: number = 0.1;
    public getCallBacK: Function = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.top_oirgin_y = this.node_top.y;
        this.bottom_origin_y = this.node_bottom.y;
        this.top_scale_size = (cc.winSize.width + 20) / this.node_top.width;
        this.bottom_scale_size = cc.winSize.width / this.node_bottom.width;
    }

    start() {
        this.node_rectBg.setContentSize(cc.winSize.width, cc.winSize.height);
        this.initView();
        this.initEvent();
    }

    // update (dt) {}

    onEnable() {

        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "进入过关红包界面");

        //播放红包来了音效
        // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.redPacket_come);
        mk.audio.playEffect("ef_redPacket_come");
        
        mk.console.log("设置红包来了1!!!!!!!!!!!!!!!!!!!!!");
        this.node_closeBtn.active = false;
        let timeout = setTimeout(() => {
            this.node_closeBtn.active = true;
            clearTimeout(timeout);
        },800);
    }

    onDisable() {
        this.initView();
    }

    /**初始化
     * @param 红包名字
     * @param 红包现金数目
     * @param 回调
     * @param 提示
     */
    init(readPacketName: string, cashNum: number, getCallBack: Function = null, tip: string = null) {

        this.getCallBacK = getCallBack;
        this.cashNum = cashNum;

        this.label_redPacketName_open.string = readPacketName;
        this.label_redPacketName_unopen.string = readPacketName;
        this.label_cashNum.string = cashNum.toString();
        if (tip) {
            this.label_redPacketTip.string = tip.toString();
            this.label_redPacketTip.node.active = true;
        }
        else {
            this.label_redPacketTip.node.active = false;
        }
    }

    /**初始化视图 */
    initView() {
        this.node_open.active = false;
        this.node_unopen.active = true;
        this.node_top.y = this.top_oirgin_y;
        this.node_top.scaleX = this.node_top.scaleY = 1;
        this.node_bottom.y = this.bottom_origin_y;
        this.node_bottom.scaleX = this.node_bottom.scaleX = 1;
        this.node_openBtn.active = true;
        this.node_closeBtn.active = false;
        this.node_rectBg.opacity = 50;
    }

    /**初始化事件 */
    initEvent() {
        this.node_closeBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_openBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_getCashBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_doubleCashBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_cashOutBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
    }

    onClick(event: cc.Event.EventTouch) {
        switch (event.currentTarget) {
            case this.node_closeBtn:
                this.onClickClose();
                break;
            case this.node_openBtn:
                this.onClickOpenBtn();
                break;
            case this.node_getCashBtn:
                this.onClickGetCash();
                break;
            case this.node_doubleCashBtn:
                this.onClickDoubelCash();
                break;
            case this.node_cashOutBtn:
                this.onClickCashOutBtn();
                break;
        }
    }

    /**点击关闭 */
    onClickClose() {

        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击关闭按钮");

        // this.node.active = false;
        mk.ui.closePanel("RedPacketUI");

        if (this.getCallBacK) {
            this.getCallBacK();
        }
    }

    /**点击领取双倍 */
    onClickOpenBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击打开过关红包按钮");
        // AdMgr.Inst.watchAd(VIDEOTYPE.OpenRedPacket, (ifsuccess) => { this, this.watchCallBack(ifsuccess) });
    }

    /**观看回调 */
    watchCallBack(ifSuccess: boolean = false) {
        if (ifSuccess) {
            mk.console.log("观看成功")
            this.watchSuccess();
        }
        else {
            mk.console.log("观看失败")
            this.watchFailed();
        }
    }

    /**观看成功 */
    watchSuccess() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "视频打开过关红包成功");
        this.onClickOpen();
    }

    /**观看失败 */
    watchFailed() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "视频打开过关红包失败");
        // EffectMgr.Inst.addTip("观看视频失败,请稍后再试");
    }

    /**点击打开 */
    onClickOpen() {

        this.ani_openBtn.play("ani_openRedPacket");

        this.scheduleOnce(() => {

            this.ani_openBtn.play();

            this.node_openBtn.active = false;
            this.node_closeBtn.active = false;

            // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.redPacket_open);
            mk.audio.playEffect("ef_redPacket_open");

            cc.tween(this.node_rectBg).to(0.2, { opacity: 255 }).call(() => {
                this.node_open.active = true;
                this.node_cashInfo.scaleX = this.node_cashInfo.scaleY = 1.1;

                this.node_getCashBtn.active = false;
                let timeOut = setTimeout(() => {
                    clearTimeout(timeOut);
                    this.node_getCashBtn.active = true;
                }, 1500)

            }).start();

            let top_end_height = this.node_top.height * this.top_scale_size;
            let top_end_y = cc.winSize.height * 0.5 + top_end_height * 0.5 - 150;

            cc.tween(this.node_top).to(0.3, { scaleX: this.top_scale_size, scaleY: this.top_scale_size, y: top_end_y }).call(() => {
                cc.tween(this.node_cashInfo).to(0.2, { scaleX: 1, scaleY: 1 }).call(() => { }).start();
            }).start();

            let bottom_end_height = this.node_top.height * this.bottom_scale_size;
            let bottom_end_y = -(cc.winSize.height * 0.5 + bottom_end_height * 0.5);
            cc.tween(this.node_bottom).to(0.3, { scaleX: this.bottom_scale_size, scaleY: this.bottom_scale_size, y: bottom_end_y }).call(() => {

            }).start();
        }, 0.9)
    }

    onClickCashOutBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "去提现");
    
        mk.ui.openPanel("CashOutUI");
        
        // Main.Inst.node_game.active = true;
    }

    /**点击领取 */
    onClickGetCash() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击普通领取");
        this.getCash(1);
    }

    /**点击领取双倍 */
    onClickDoubelCash() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击双倍领取");
        // AdMgr.Inst.watchAd(VIDEOTYPE.PassRedPacket, (ifsuccess) => { this, this.watchDoubleCallBack(ifsuccess) });
    }

    /**观看回调 */
    watchDoubleCallBack(ifSuccess: boolean = false) {
        if (ifSuccess) {
            mk.console.log("观看成功")
            this.watchDoubleSuccess();
        }
        else {
            mk.console.log("观看失败")
            this.watchDoubleFailed();
        }
    }

    /**观看成功 */
    watchDoubleSuccess() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "双倍领取过关红包成功");
        this.getCash(2);
    }

    /**观看失败 */
    watchDoubleFailed() {
        GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "双倍领取过关红包失败");
        // EffectMgr.Inst.addTip("观看视频失败,请稍后再试");
    }

    /**是否获取红包 */
    getCash(multiple: number = 1) {
        
        // this.node.active = false;
        mk.ui.closePanel("RedPacketUI");

        PlayerConst.todayPassRedPacketNum++;
        DataMgr.Inst.updateTodayPassRedPacketNum(PlayerConst.todayPassRedPacketNum);

        DataMgr.Inst.updateMoneyNum(this.cashNum * multiple);
        let get_type = multiple === 1 ? 0 : 1;
        HttpMgr.Inst.passRedPacket(get_type, PlayerConst.levelNum, this.cashNum).then((data) => {
            mk.console.log("passRedPacket data", data);
            EffectMgr.Inst.addTip("观看视频成功,成功领取双倍红包");
            DataMgr.Inst.updateMoneyNum(this.cashNum);
        }).catch((err) => {

        });

        if (this.getCallBacK) {
            this.getCallBacK();
        }
    }

}

export enum REDPACKETTYPE {
    /**新手 */
    NewPlayer = 0,
    /**过关红包 */
    Pass = 1,
    /** */
}