define(["base/BaseCtrl","./PlayerCtrl","events/EventManager"],function(a,b,c){var d=Marionette.Controller.extend({initialize:function(d){a.applyMixin.call(this),this.ctrls={},this.views={},this.eventManager=new c(this),this.rememberRepeatCounter=0,this.timerLesson=null,this.t=0,this.resumePosition=0,this.ctrls.player=new b,this.ctrls.player.isSupport()?(this.ctrls.player.makeSupport(),this.isAudioSupport=!0):this.isAudioSupport=!1,this.lessonsModel=d.lessonsModel,this.settingsModel=d.settingsModel,this._clearTimmerLesson(),_.bindAll(this,"onLessonsModelIndexChanged","automaticStart","onSettingsModelAudioChanged","onModelStop","onModelPlayPause"),_.bindAll(this,"_goPlay","_goRepeat","_goDelay","_autoLoop","_goNextLesson","setTimeoutFunction","_correctProgress"),this._bindEvents(),_.delay(this.automaticStart,100)},_bindEvents:function(){this.eventManager.bind(CtrlEvents.LessonsModel.indexChanged,this.onLessonsModelIndexChanged,this),this.eventManager.bind(CtrlEvents.Settings.audioChanged,this.onSettingsModelAudioChanged,this),this.eventManager.bind(CtrlEvents.Settings.audioStopChanged,this.onModelStop,this),this.eventManager.bind(CtrlEvents.Settings.audioPlayPauseChanged,this.onModelPlayPause,this)},_unbindEvents:function(){this.eventManager&&this.eventManager.unbindAll(),this.eventManager&&this.eventManager.unbindKeyboardEvents()},onLessonsModelIndexChanged:function(){this._resetNow(),this.isClosed||this.settingsModel.isAudioStoped()||this.settingsModel.isAudioPlaying()&&this.goPlayManually()},automaticStart:function(){this._clearTimmerLesson(),this.isClosed||(this._resetNow(),this.goPlayManually())},onSettingsModelAudioChanged:function(){},goPlayManually:function(a){this.settingsModel.isAudioStoped()||this.settingsModel.isAudioPlaying()&&this._goPlay(a)},onModelPlayPause:function(){if(this._clearTimmerLesson(),!this.isClosed)if(this.settingsModel.isAudioStoped())this._resetNow(),this.goPlayManually();else if(this.settingsModel.isAudioPlaying())if(this.ctrls.player.isPaused()&&this.lessonsModel.isCurrentLessonAudio())this.ctrls.player&&this.ctrls.player.play();else{var a=!0;this.goPlayManually(a)}else this.ctrls.player&&this.ctrls.player.pause()},_resetNow:function(){this.resumePosition=0,this._clearTimmerLesson(),this.rememberRepeatCounter=0,this.ctrls.player&&this.ctrls.player.stop()},onModelStop:function(){this.settingsModel.isAudioStoped()&&this._resetNow()},_goPlay:function(a){this._clearTimmerLesson();var b=this.lessonsModel.getCurrentAudioSrc();b&&this.isAudioSupport&&"off"===this.settingsModel.getMuteLessonSelected().id?this.ctrls.player&&this.ctrls.player.play({src:b,onFinish:this._goRepeat}):(this.ctrls.player.stop(),this._goRepeat(a))},_goRepeat:function(a){if(this.settingsModel.isModeAudioplay()){var b=this.settingsModel.getRepeatAudioSelected();if(this.lessonsModel.isCurrentLessonNextAudio())return this.resumePosition=0,void this._goDelay(1e3,this.resumePosition);if(b&&b.loop>this.rememberRepeatCounter)return this.rememberRepeatCounter++,void this._goDelay()}this.rememberRepeatCounter=0,this._autoLoop(a)},_goDelay:function(a,b){this._clearTimmerLesson();var c=this.settingsModel.getTimerLessonSelected();a=a?a:c&&_.isNumber(c.ms)?c.ms:200,this.setTimeoutFunction(this._goPlay,a,b)},_clearTimmerLesson:function(){clearInterval(this.timerLesson),this.timerLesson=null},_autoLoop:function(a){if(this._clearTimmerLesson(),this.rememberRepeatCounter=0,this.settingsModel.isModeAudioplay()){var b=this.settingsModel.getTimerLessonSelected();b&&_.isNumber(b.ms)&&(a||(this.resumePosition=0),this.setTimeoutFunction(this._goNextLesson,b.ms,this.resumePosition))}else this.settingsModel.setModeAudioplayDefault()},_goNextLesson:function(){this.lessonsModel.nextLesson()},setTimeoutFunction:function(a,b,c){this._clearTimmerLesson(),null==this.timerLesson&&(this.t=0,_.isNumber(c)&&(this.t+=c,this.resumePosition=this.t),this.timerLesson=setInterval(_.partial(this._correctProgress,a,b),1e3),this._correctProgress(a,b,c))},_correctProgress:function(a,b){if(!this.isClosed){var c={currentWaitPosition:_.isNumber(this.t)?this.t/1e3:0,currentWaitLength:_.isNumber(b)?b/1e3:0};this.eventManager.trigger(CtrlEvents.Player.dalayPosition,c,this),this.t>=b&&(this._clearTimmerLesson(),a()),this.t+=1e3,this.resumePosition=this.t}},onDestroy:function(){this._clearTimmerLesson(),this._resetNow()}});return _.extend(d.prototype,a),d});