'use strict';
//dependent upon on Bootstrap

app.directive('deviceInterface', function () {
	return {
		restrict: 'E',
		scope: {
			ngModel: '=',
			popup: '=',
			exists: '=',
			hasPageBtn: '=',
			currentState: '='
		},
		templateUrl: 'views/device-interface.html',
		controller: function($scope, $modal, Devices) {
			console.log($scope.ngModel);

			// 2016.06.24 HOMESET-422, MXHOMEPRO-122 안드로이드 버그로 인해 클릭 이벤트 분기 처리
			var isAndroid = /(android)/i.test(navigator.userAgent);
			var dragIndex;

			$scope.ngModel.currentMenuPage = 1;
			$scope.ngModel.currentHardPage = 1;
			$scope.ngModel.selectedBtn = null;
			$scope.ngModel.repeatSetting = 'default';
			$scope.ngModel.activeTab = 'menu';
			//$scope.ngModel.newBtn = {};
			$scope.ngModel.menu = {
					pages: []
			};
			$scope.ngModel.hard = {
				pages: Devices.getHardBtnPages()
			};

			if ($scope.exists) { 
				$scope.ngModel.menu.pages = Devices.getMenuBtnPages();
			} else {
				if ($scope.popup) {
					// if popup copy its current state into the model  
					$scope.ngModel.menu.pages = $scope.currentState.menu.pages; 
				} else {
					// else initialize the device interface push a blank menu page
					$scope.ngModel.menu.pages.push(Devices.makeBlankMenuPage({room_id: $scope.ngModel.room.id, repeat: { min: 3, macro: 3}}));
				}
			}


			$scope.isEmpty = function(val) { return _.isEmpty(val); };
			$scope.sortableOptions = {
				stop: function( event, ui ) {
					var buttonId = _(buttonsArray).pluck('id').min().value();
					console.log(buttonId);
					// 2016.06.24 HOMESET-422, MXHOMEPRO-122 안드로이드 버그로 인해 클릭 이벤트 분기 처리
					if(isAndroid){
						var _item = $(ui.item);
						if(dragIndex == $( 'li.lcd-grid-item' ).index( _item )){
							_item.find('>label').trigger('click');
						}
					}
					for (var i = 0; i < buttonsArray.length; i++) {
						buttonsArray[i].page_btn_idx = i;
						//buttonsArray[i].id = buttonId;
						//buttonId++;
					}
					$scope.$apply();
				},
				start : function(event, ui){
					dragIndex = $( 'li.lcd-grid-item' ).index( $(ui.item) );
				}
			};
			$scope.ngModel.deviceData = Devices.getCurrentData();

			$scope.ngModel.filteredMenuPage = $scope.ngModel.menu.pages[0];
			$scope.ngModel.filteredHardPage = $scope.ngModel.hard.pages[0];
			var buttonsArray = $scope.ngModel.menu.pages[0].btns;

			var menuPages = $scope.ngModel.menu.pages;
			var pageId = _(menuPages).pluck('id').max().value(),
					btnId = _(menuPages[menuPages.length - 1].btns).pluck('id').max().value()+1;

			console.log(pageId);
			console.log(btnId);



			$scope.addPage = function() {
				var btns = [];
				for (var i=0; i < 6; i++) {
					btns.push(Devices.makeBlankMenuButton(btnId, i, $scope.ngModel.deviceData));
					btnId++;
				}
				pageId++;
				console.log('pushing page with id of ' + pageId);
				$scope.ngModel.menu.pages.push({id: pageId, btns: btns});

			};

			$scope.hidePages = function() {
				var modal = $modal({
					scope: $scope,
					title: 'Hide pages',
					template: 'views/modal-tpls/hide-pages.html',
					show: false
				});

				modal.$promise.then(modal.show);
			};

			$scope.hidePagesSave = function() {

			};

			$scope.menuPageChanged = function(page) {
				$scope.ngModel.filteredMenuPage = $scope.ngModel.menu.pages[page - 1];
				buttonsArray = $scope.ngModel.menu.pages[page - 1].btns;
				$scope.ngModel.selectedBtn = null;
				console.log(buttonsArray);
			};

			$scope.hardPageChanged = function(page) {
				$scope.ngModel.filteredHardPage = $scope.ngModel.hard.pages[page - 1];
				buttonsArray = $scope.ngModel.hard.pages[page - 1].btns;
				$scope.ngModel.selectedBtn = null;
				console.log(buttonsArray);
			};

		}
	};
});
