import JsbSystem from "../../mk/system/JsbSystem";
import { GameProp } from "../data/GameData";
import FunBtns from "./FunBtns";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Game extends cc.Component {

    @property({ type: cc.Node, displayName: "游戏核心玩法" })
    node_gameplay: cc.Node = null;
    @property({ type: cc.Node, displayName: "功能按钮" })
    node_btn: cc.Node = null;
    @property({ type: cc.Sprite, displayName: "头像" })
    img_head: cc.Sprite = null;
    @property({ type: cc.Label, displayName: "红包币" })
    lbl_redMoney: cc.Label = null;
    @property({ type: cc.Label, displayName: "金猪币" })
    lbl_rmb: cc.Label = null;
    @property({ type: cc.Sprite, displayName: "红包提现" })
    btn_cashOutNormal: cc.Sprite = null;
    @property({ type: cc.Sprite, displayName: "金猪提现" })
    btn_cashOutGold: cc.Sprite = null;
    @property({ type: cc.Node, displayName: "气泡红包1" })
    btn_getRed1: cc.Node = null;
    @property({ type: cc.Node, displayName: "气泡红包2" })
    btn_getRed2: cc.Node = null;
    @property({ type: cc.Node, displayName: "气泡红包3" })
    btn_getRed3: cc.Node = null;

    @property({ type: cc.Button, displayName: "开始游戏" })
    btn_start: cc.Button = null;
    @property({ type: cc.Node, displayName: "红包币图标" })
    icon_hb: cc.Node = null;
    @property({ type: cc.Node, displayName: "金猪币图标" })
    icon_zb: cc.Node = null;

    @property({ type: cc.Node, displayName: "顶部ui组" })
    node_top_ui: cc.Node = null;


    onLoad() {
        gData.gameData.gameStyle = this;
        mk.ui.closePanel("login");
    }

    start() {
        this.initMusic();
        this.initBtns();
        this.initQiPaos();
        this.initInfo();

        // if(gData.gameData.isNewPlayer()){
        //     this.initGuide();
        //     gData.gameData.setProp(GameProp.newPlayer,0);
        // }
    }

    private initGuide() {
        mk.guide.open(1);
    }

    update() {
        if (gData.gameData.init_coin) {
            this.changeInfo();
        }

        if (gData.gameData.init_head) {
            this.initHead();
        }
    }
    lateUpdate() {
        gData.gameData.init_head = false;
    }

    private initMusic() {
        mk.audio.playMusic("bgm");
    }

    /**
     * 根据配置，添加开启的功能按钮
     */
    private initBtns() {
        let sc = this.node_btn.getComponent(FunBtns) as FunBtns;
        sc.init();
    }

    private initQiPaos() {
        mk.tween.scale(this.btn_getRed1, 0.6, 0, 1, null, 'backOut');
        mk.tween.scale(this.btn_getRed2, 0.6, 0, 1, null, 'backOut');
        mk.tween.scale(this.btn_getRed3, 0.6, 0, 1, null, 'backOut');

        let easing_type = '';//sineInOut
        cc.tween(this.btn_getRed1).to(1.2, { y: 100 }, { easing: easing_type }).to(1.2, { y: 50 }, { easing: easing_type }).union().repeatForever().start();
        cc.tween(this.btn_getRed2).delay(0.2).to(1.5, { y: 125 }, { easing: easing_type }).to(1.5, { y: 50 }, { easing: easing_type }).union().repeatForever().start();
        cc.tween(this.btn_getRed3).delay(0.4).to(1, { y: 10 }, { easing: easing_type }).to(1, { y: -50 }, { easing: easing_type }).union().repeatForever().start();
    }

    private initInfo() {
        this.lbl_redMoney.string = gData.gameData.gameData.redMoney + "";
        // this.lbl_rmb.string = gData.gameData.gameData.piggyBank + "";
        const new_count = gData.gameData.gameData.piggyBank / 100;
        this.lbl_rmb.string = new_count.toFixed(2);
        gData.gameData.init_coin = false;
    }

    private changeInfo() {
        this.lbl_redMoney.node.getComponent('NumberAnim').setValue(gData.gameData.gameData.redMoney);
        // this.lbl_rmb.node.getComponent('NumberAnim').setValue(gData.gameData.gameData.piggyBank);
        const new_count = gData.gameData.gameData.piggyBank / 100;
        this.lbl_rmb.node.getComponent('NumberAnim').setValue(new_count.toFixed(2), 2);
        gData.gameData.init_coin = false;
    }

    private async initHead() {
        let result = await mk.loader.loadRemote(gData.wechatData.avatar + "?aaa=aa.jpg", null);
        this.img_head.spriteFrame = new cc.SpriteFrame(result);
    }

    //测试道具获取效果
    private testFly() {
        mk.fly.PlayCoinAnim(1, 20, cc.v2(0, -300), cc.v2(0, 0));
        // mk.fly.PlayCoinAnim(2, 20, cc.v2(0, -300), 'Canvas/界面层/game/功能按钮/右侧按钮/btn2');
    }

    /** */
    public onClickStart() {
        this.node_gameplay.active = true;
    }

    /**点击分享 */
    public onClickShare() {
        if (!gData.loginData.isAuth) {
            mk.tip.pop("请先点击头像,在设置界面授权");
            return;
        }
        JsbSystem.sharePic();
    }
}

