// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        confirmButton: {
            default: null,
            type: cc.Button,
            serializable: true,
        },
        cancelButton: {
            default: null,
            type: cc.Button,
            serializable: true,
        },
        closeButton: {
            default: null,
            type: cc.Button,
            serializable: true,
        },

        showNum: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {



        let clickEventHandler_Sound = new cc.Component.EventHandler();
        clickEventHandler_Sound.target = cc.find('GameNode/ManageSound'); //这个 node 节点是你的事件处理代码组件所属的节点，这里就是Button1
        clickEventHandler_Sound.component = "PlaySound";//这个是脚本文件名
        clickEventHandler_Sound.handler = "PlayPressBtn"; //回调函名称

        //确定按钮
        let clickEventHandler1 = new cc.Component.EventHandler();
        clickEventHandler1.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点，这里就是Button1
        clickEventHandler1.component = "TipView";//这个是脚本文件名
        clickEventHandler1.handler = "btnConfirmClick"; //回调函名称
        // clickEventHandler1.customEventData = "click1 user data"; //用户数据

        //this._confirmButton = this.node.getChildByName('Container').getChildByName('ConfirmButton').getComponent(cc.Button); //获取cc.Button组件
        this.confirmButton.clickEvents.push(clickEventHandler1); //增加处理
        this.confirmButton.clickEvents.push(clickEventHandler_Sound);   //添加声音


        //取消按钮
        let clickEventHandler2 = new cc.Component.EventHandler();
        clickEventHandler2.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点，这里就是Button2
        clickEventHandler2.component = "TipView";//这个是脚本文件名
        clickEventHandler2.handler = "btnCancelClick"; //回调函名称
        // clickEventHandler2.customEventData = "click2 user data"; //用户数据

        //this._cancelButton = this.node.getChildByName('Container').getChildByName('CancelButton').getComponent(cc.Button); //获取cc.Button组件
        this.cancelButton.clickEvents.push(clickEventHandler2); //增加处理
        this.cancelButton.clickEvents.push(clickEventHandler_Sound);   //添加声音


        //关闭按钮
        let clickEventHandler3 = new cc.Component.EventHandler();
        clickEventHandler3.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点，这里就是Button2
        clickEventHandler3.component = "TipView";//这个是脚本文件名
        clickEventHandler3.handler = "btnCancelClick"; //回调函名称
        this.closeButton.clickEvents.push(clickEventHandler3); //增加处理
        this.closeButton.clickEvents.push(clickEventHandler_Sound);   //添加声音
    },

    start() {

        if (GlobalD.GameData.GetGolden() < this.Data.ContentButton.DiamondPrice) {
            //禁用按钮
            this.confirmButton.enableAutoGrayEffect = true;
            this.confirmButton.interactable = false;
            return;
        }
        //设置按钮
        this.confirmButton.enableAutoGrayEffect = false;
        this.confirmButton.interactable = true;
    },

    onShowTip(data) {

        // cc.log('传送过来的对象：', data.ContentButton);
        this.Data = data;

        if (GlobalD.GameData.GetGolden() < this.Data.ContentButton.DiamondPrice) {
            this.showNum.string = this.Data.ContentButton.DiamondPrice ;
            //禁用按钮
            if (this.confirmButton) {
                this.confirmButton.enableAutoGrayEffect = true;
                this.confirmButton.interactable = false;
            }

            return;
        }
        //设置按钮

        if (this.confirmButton) {
            this.confirmButton.enableAutoGrayEffect = false;
            this.confirmButton.interactable = true;
        }
        this.showNum.string = this.Data.ContentButton.DiamondPrice;

    },


    //确定
    btnConfirmClick: function (event, customEventData) {
        // this.Data.TipViewNode.active = false;
        //扣去钻石
        // GlobalD.GameData.PlusDiamond(-this.Data.ContentButton.DiamondPrice);
        GlobalD.GameData.PlusGolden(-this.Data.ContentButton.DiamondPrice);
        //后面需要判断是否扣去钻石成功，才能进行添加。 todo...
        //购买后添加
        GlobalD.game.AddBuildingNum(this.Data.ContentButton.node.name);

        this.node.destroy();
    },
    //取消
    btnCancelClick: function (event, customEventData) {
        // this.Data.TipViewNode.active = false;
        this.node.destroy();
    },
    btnCloseClick: function (event, customEventData) {

        this.node.destroy();
    }
});
