/*
 *  Overview
 *
 */

type Overview (appdoo) extends ViewController (Core.UI) {


	as BasicObject (Core) {

		public init() {
			this.animationDuration = 0.25;
			super.init();
			setTimeout(function () { this.overviewSegmentedControl.selectedIndex = kSegmentIndexScenes; }.bind(this), 1);
		}
		
		public destroy() {
			var node = this.view.DOMNode;
			if (node.parentNode) {
				node.parentNode.removeChild(node);
			}
			this.removeWindowEvents();
			super.destroy();
		}
	
	}


	property (outlet) headerView expects appdoo.Overview.HeaderTitleView {
	}

	property (outlet) contentView expects Core.UI.View {
	}

	property (outlet) scenesView expects appdoo.Overview.ScenesView {
	}

	property (outlet) overviewSegmentedControl {
	}
	
	property (outlet) firstSettingsControl {
	}


	public focus() {
	
	}
	
	/** Responding to events of the view */
	
	as ViewDelegate (Core.UI) {
	
		public viewDidLoad(view) {
			if (view !== this.view) return;
			super.viewDidLoad(view);
			this.view.viewDidResize = this.resizeViewHandler.bind(this);
			this.createButtons();
		}
	
	var x;
	
		public viewDidAppear() {
		
			if (x) return; x = true;

			this.context.prepareForUndoManagerWithID('overview');
			this.addWindowEvents();
			
			if (this.overviewSegmentedControl.selectedIndex === kSegmentIndexScenes) {
			
				this.scenesView.overviewDidAppear();
				this.scenesView.focus();
				
			}

			if (!this.context.isOffscreen)
				this.context.nativeProxy.sendMessage('contentScaleDidChange',0);

		}
	
		public viewDidDisappear() {
			if (!x) return; x = false;
			this.removeWindowEvents();
		}
		

	}
	
	protected resizeViewHandler() {
		this.view.__proto__.viewDidResize.call(this.view);
	}


	/** Loading and setting up */
	
	as MainViewController (appdoo.appdooContext) {
		
		public performAfterLoading(callback) {
			if (this.view === undefined) {
				this.loadView();
				this.view.hidden = true;
				document.body.appendChild(this.view.DOMNode);
			}
	
			callback();
		}
	
		public validateMenuItem(tag) {
			return false;
		}
	}


	/** Handling events */

	protected addWindowEvents() {

		this.keydownEventHandlerBound =
			this.keydownEventHandler.bind(this);

		window.addEventListener('keydown',
								this.keydownEventHandlerBound);
		

		this._swipeEventHandlerBound = this.swipeEventHandler.bind(this);
		window.addEventListener('swipe', this._swipeEventHandlerBound);

	}

	protected removeWindowEvents() {
		window.removeEventListener('keydown',
								this.keydownEventHandlerBound);

		window.removeEventListener('swipe',
								this._swipeEventHandlerBound);
	}

	protected keydownEventHandler(e) {
	
		if (this.overviewSegmentedControl.selectedIndex === kSegmentIndexAppInfo) {
			if (this.context.isCancelEvent(e)) { 
				this.overviewSegmentedControl.selectedIndex = kSegmentIndexScenes;
				e.preventDefault();
				return;
			}
		}
	

		if (e.metaKey === true && e.altKey !== true && e.shiftKey !== true) {
			var nSegments = 2;
				keyCode1 = 49;

			// cmd 1, cmd 2, ...
			if (e.keyCode >= keyCode1 && e.keyCode <= keyCode1+nSegments-1) { 
				this.overviewSegmentedControl.selectedIndex = e.keyCode-keyCode1;
				e.preventDefault();
				return;
			}
		}

		// zoom in
		if (e.keyCode === 187 && e.metaKey && e.altKey) {
			if (this.scenesView.zoomIn())
				e.preventDefault();
			return;
		}

		// zoom out
		if (e.keyCode === 189 && e.metaKey && e.altKey) {
			if (this.scenesView.zoomOut())
				e.preventDefault();
			return;
		}
		
		if (e.target !== document.body) return;

	}
	
	protected swipeEventHandler(e) {

		if (e.detail === 1) {
			this.overviewSegmentedControl.selectedIndex = 0;
		}
		else if (e.detail === 2) {
			this.overviewSegmentedControl.selectedIndex = 1;
		}

	}
	
	protected createButtons() {
		
		if(this.context.disableInAppPreferences) {
			return;
		}
		var width = LocalizedNumber('PROJECT_SETTINGS_BUTTON_WIDTH')||143;
		width += 4;

		var b = this.context.makeForwardButton(LocalizedString('Project Settings'), width, 0,
			function () {
				this.overviewSegmentedControl.selectedIndex = kSegmentIndexAppInfo;
			}.bind(this));
		b.style({zIndex:2001});
		b.autoresizingMask = 1;
		b.position = { left: this.view.size.width-width-15, top: 30 };
		this.view.addSubview(b);
		this.goToSettingsButton = b;

		var b2 = this.context.makeBackButton(LocalizedString('Screens'), LocalizedNumber('BACK_TO_SCENES_BUTTON_WIDTH')+2, 0, function () {
				this.overviewSegmentedControl.selectedIndex = kSegmentIndexScenes;
			}.bind(this));
		b2.style({zIndex:2001});
		b2.position = { left: 15, top: 30 };
		b2.hidden = true;
		this.view.addSubview(b2);

		this.goToScenesButton = b2;
		
	}
	
	public segmentedViewControllerDidLoadSegmentAtIndex(svc,i) {

		if (this.goToSettingsButton)
			this.goToSettingsButton.hidden = (i === kSegmentIndexAppInfo);

		if (this.goToScenesButton)
			this.goToScenesButton.hidden = (i === kSegmentIndexScenes);

	}
	
	
	public getOverviewAppearance() {
		return {
			contentScale: this.scenesView.contentScale
		};
	}

	public setOverviewAppearance(obj) {
		if (!obj) return;
	}

	public createScene() {
		return this.scenesView.sceneDispenserView.createSceneWithoutDrag();
	}
	

	public showPrototypeDevicesBar(bar) {
		var containerView = this.scenesView;
		containerView.addSubview(bar);
		containerView.size.height = window.innerHeight - bar.size.height - 45;
	}
	
	public fileReferencesDidUpdate(refs) {
		this.headerView.fileReferencesDidUpdate(refs);
		var infoEditor = getPrototype('appdoo.ProjectInfoEditor').sharedInstance;
		if (infoEditor) infoEditor.fileReferencesDidUpdate(refs); 
	}
	
	public prototypeDevicesBarDidChangeHeight(bar) {
		if (!bar.superview) return;
		
		var siblingView = bar.superview.subviews[0];
		var newHeigt = window.innerHeight - bar.size.height;
		var difference = siblingView.size.height - newHeigt;
		siblingView.size.height = newHeigt;
		siblingView.style({ webkitTransform:'translateY('+difference/2+'px)' });
		bar.style({ webkitTransform:'translateY('+(difference)+'px)' });
		
		var animationTime = bar.size.height > 0 ? .75 : 0.5;
		setTimeout(function () {
			siblingView.beginAnimations();
			siblingView.animationDuration = animationTime;
			siblingView.style({ webkitTransform:'' });
			siblingView.commitAnimations();
			bar.beginAnimations();
			bar.animationDuration = animationTime;
			bar.style({ webkitTransform:'' });
			bar.commitAnimations();
		}, 1);
		
	}

}
