define(["view/map", 
		'view/drifter-list', 
		'view/lat-lon-adder',
		'view/date-time-picker',
		'view/method-radio', 
		'view/dataset-radio', 
		'view/submit-buttons',
		'view/days-range-slider', 
		'view/hours-range-slider', 
		'view/modal',
		'emitter/render', 
		'vue', 
		'moment',
		'constant',
		'action/trajectory',
		'action/drifter'], 
	function(Map, DrifterList, LatLonAdder, DateTimePicker, MethodRadio, DatasetRadio, Buttons, DaysRangeSlider, HoursRangeSlider, Modal, RenderEmitter, Vue, moment, Constant, TrajectoryAction, DrifterAction){
		RenderEmitter.register('drifter-list', function(drifters){
			_vForm.drifters = drifters;
		});
		RenderEmitter.register('all-trajectory', function(trajectories){
			_vForm.trajectories = trajectories;
		});

		var _vForm = new Vue({
			el: 'div.app',
			data: {
				drifters: [],
				trackId: Constant.TRACK_METHODS.FORWARD_EULER.ID,
				trackMethod: Constant.TRACK_METHODS.FORWARD_EULER.METHOD,
				trackWay: Constant.TRACK_METHODS.FORWARD_EULER.TRACK_WAY,
				dataset: Constant.DATASET_TYPE.ROMS,
				hoverDrifterId: '',
				monthModel: '',
				daysModel: '',
				hoursModel: '',
				showModal: false,
				trajectoryData: null,
				h: 1,
				r: 0.16
			},
			methods: {
				onCalendarChange: function(mDate){
					this.monthModel = mDate;
				},
				onDaysModelChange: function(daysModel){
			  		this.daysModel = daysModel;
				},
				onHoursModelChange: function(hoursModel){
					this.hoursModel = hoursModel;
				},
				onMethodChange: function(trackId){
					this.trackId = trackId;
					this.trackMethod = Constant.TRACK_METHODS[trackId.toUpperCase()].METHOD;
					this.trackWay = Constant.TRACK_METHODS[trackId.toUpperCase()].TRACK_WAY;
				},
				onDatasetChange: function(dataset){
					this.dataset = dataset;
				},
				onFormSubmit: function(action){
					if (action == 'cancel') {
						window.location.reload();
					}else if (action == 'submit') {
						if (this.drifters.length) {
							TrajectoryAction.clearTrajectories();
							TrajectoryAction.compute(this.drifters, this.hoursModel, this.daysModel, this.trackWay, this.trackMethod, this.dataset, this.h, this.r);
						}else{
							console.info('no drifters');
						}
					}else if (action == 'clear-trajectories') {
						TrajectoryAction.clearTrajectories();
					}else if (action == 'clear-drifters') {
						DrifterAction.clear();
					}else if (action == 'download-data') {
						if(this.trajectories.length) TrajectoryAction.downloadData(this.trajectories);
					}
				},
				onDrifterHover: function(drifterId){
					this.hoverDrifterId = drifterId;
				},
				onCloseModal: function(){
					this.showModal = false;
				}
			}
		});
		window._vForm = _vForm;
	}
);