let APJS=require("../../../amazingpro"),BaseNode=require("../Utils/BaseNode").BaseNode,CentralDataReset=require("../Utils/CentralDataReset").CentralDataReset,kRunningWrapModeKey="runningWrapModes",kClipPlaySpeedKey="speed";class CGAnimatorController extends BaseNode{constructor(){super(),this.component=null,this.animationList=[],this.animationSize=0,this.currentClip=null,this.loops=0,this.currentLoop=0,this.infinity=!1,this.errorConfig=!1,this.stayLastFrame=!1,this.finish=!1,this.sys=null,this.state="",this.chosenIndex=-1,this.haveRegisteredListener=!1}_updateEventListener(){this.haveRegisteredListener&&this.chosenIndex==this.inputs[3]()||this._registerEventListener(this.sys)}_registerEventListener(t){var e;this.haveRegisteredListener&&this._removeEventListener(t),this.component=this.inputs[2](),this.errorConfig=!1,this.loops=this.inputs[4](),null!=this.component&&!1!==this.component.isInstanceOf("Animator")&&0!==this.loops&&(-1===this.loops&&(this.infinity=!0),this.stayLastFrame=this.inputs[5](),this.animationList=this.component.animations,this.animationSize=this.animationList.length,!(this.animationSize<1))&&(this.currentClip,this.chosenIndex=this.inputs[3](),!(this.chosenIndex>=this.animationSize||this.chosenIndex<0))&&(e=this.animationList[this.chosenIndex])&&(this.currentClip=e.getDefaultClip(this.component),this.currentClip)?(t.eventListener.registerListener(t.APJScript,APJS.AnimationEventType.AnimationEnd,this.currentClip,t.APJScript),this.haveRegisteredListener=!0):this.errorConfig=!0}_removeEventListener(t){t.eventListener.removeListener(this.sys.APJScript,APJS.AnimationEventType.AnimationEnd,this.currentClip,this.sys.APJScript)}getRunningAnimazNameFromAnimator(t){return!t||!t.getNative()||0===(t=t.getNative().runningWrapModes.getVectorKeys()).size()?null:t.get(0)}resetOnRecord(t){var e,i,s;this.component=this.inputs[2](),null!=this.component&&(e=CentralDataReset.getInstance().getPropertyValueFromCentralData(this.component,kRunningWrapModeKey),i=CentralDataReset.getInstance().getPropertyValueFromCentralData(this.component,kClipPlaySpeedKey),e.status)&&i.status&&(e=e.value,i=i.value,s=this.getRunningAnimazNameFromAnimator(this.component),s=this.component.getClipByName(s),this.component.stopAll(),s&&(this.component.getNative().schedule(s.getNative(),e,i),s.play(),s.getNative().seek(0)),s=this.component.getDefaultLayer().currentState)&&(s.blendWeight=1)}execute(t){if(this.component=this.inputs[2](),this.stayLastFrame=this.inputs[5](),this.loops=this.inputs[4](),this.component){var e;this.component.guid.toString();if(CentralDataReset.getInstance().registerPropertyResetCallbacksIfNotExistAutoMode(this.component,[kRunningWrapModeKey],{objectSetter:(t,e,i)=>{},objectGetter:(t,e)=>{var i,t=this.getRunningAnimazNameFromAnimator(t);return t?(i=this.component.getNative().runningWrapModes,APJS.transferToAPJSObj(i.get(t))):null}}),CentralDataReset.getInstance().registerPropertyResetCallbacksIfNotExistAutoMode(this.component,[kClipPlaySpeedKey],{objectSetter:(t,e,i)=>{},objectGetter:(t,e)=>{var t=this.getRunningAnimazNameFromAnimator(t);return(t=t&&this.component.getClipByName(t))?t.speed:0}}),null!=this.component&&this._updateEventListener(),this.errorConfig)return void(this.nexts[0]&&this.nexts[0]());0===t?(this.component.stopAll(),this.sys.animSeqNodes.forEach(t=>{this.component&&t.component&&t.component.equals(this.component)&&(t.state="stop")}),this.finish=!1,this.currentLoop=0,e=this.animationList[this.chosenIndex].speed,this.chosenIndex!=this.inputs[3]()&&this.stayLastFrame&&this.component.getNative().unschedule(this.currentClip.getNative()),this.currentClip&&(this.component.getNative().schedule(this.currentClip.getNative(),1,e),this.currentClip.play(),this.currentClip.getNative().seek(0)),(e=this.component.getDefaultLayer().currentState)&&(e.blendWeight=1),this.state="play",this.nexts[1]&&this.nexts[1]()):1===t&&(this.component.stopAll(),this.state="stop",this.nexts[2])&&this.nexts[2]()}this.nexts[0]&&this.nexts[0]()}beforeStart(t){this.sys=t,this.sys.animSeqNodes.push(this)}onUpdate(t){null!=this.inputs[2]()&&this._updateEventListener()}onCallBack(t,e,i){i===APJS.AnimationEventType.AnimationEnd&&e.equals(this.currentClip)&&"stop"!==this.state&&""!==this.state&&(this.currentLoop=this.currentLoop+1,this.currentLoop>=this.loops&&!1===this.infinity?(this.component.getNative().unschedule(this.currentClip.getNative()),this.finish=!0,this.stayLastFrame&&this.component.stopAll()):(i=this.currentClip.speed,this.component.getNative().schedule(this.currentClip.getNative(),1,i)))}onLateUpdate(t){this.finish&&(this.nexts[3]&&this.nexts[3](),this.state="",this.finish=!1)}onDestroy(){this._removeEventListener(this.sys)}}exports.CGAnimatorController=CGAnimatorController;