
import LogicUnitManager from './LogicUnitManager'
import DataModelManager from 'DataModelManager'
import GameProcessManager   from '../net/GameProcessManager'
import PlayerInfoModel from '../model/PlayerInfoModel';

cc.Class({
    extends: cc.Component,

    properties: {

    },

    ctor: function () {
      // behavior
      this._behaviorEventIDMap = {};  
      this.lum = LogicUnitManager;
      this._behaviorEvent_index = 0;
      
      // data model
      this._dataEventIDMap = {};  
      this.dmm = DataModelManager;
      this._dataEvent_index = 0;

        


    },

    onBehaviorEvent(behaviorName,eventName,callbackFun)
    {
        let behavior = this.lum.getBehavior(behaviorName);
        if(behavior)
        {
            let eventId = behavior.on(eventName,callbackFun,this);
            this._behaviorEventIDMap[this._behaviorEvent_index++] = {'behavior':behavior,'id':eventId}; 
        }
    },

    onDataModelChange(modelName,attributename,callbackFun)
    {
        let model = this.dmm.getModel(modelName);
        if(model && typeof model.on !== 'undefined')
        {
            let eventId = model.on(attributename,callbackFun,this);
            this._dataEventIDMap[this._dataEvent_index++] = {'m':model,'id':eventId}; 
        }
    },

    onLoad () {

    },

    start () {

    },
   
    onDestroy()
    {
       // behavior destroy
       for(let val in this._behaviorEventIDMap)
       {
           let eid = this._behaviorEventIDMap[val]['id'];
           let behavior = this._behaviorEventIDMap[val]['behavior'];
           if(behavior)
           {
             behavior.off(eid);
           }
       }

       // data model destroy
       for(let val in this._dataEventIDMap)
       {
           let m = this._dataEventIDMap[val]['m'],eid = this._dataEventIDMap[val]['id'];
           if(m)
           {
               m.off(eid);
           }
           
       }
    },

    playShow(callbackFun)
    {
        this.node.opacity=0;
        this.node.runAction(cc.fadeIn(1.0));
    },

    playEnd(nextSence)
    {
        var callbackFun = null
        if (nextSence) {
            callbackFun = function(){
                cc.director.loadScene(nextSence)
                };
        }
        this.node.runAction(cc.sequence(cc.fadeOut(1.0),cc.callFunc(callbackFun)));
    }
    
});
