'use strict';
/**
 * @ngdoc controller
 * @name DeviceSelectionController
 * @description
 *  room, device 선택 화면
 */
app.controller('DeviceSelectionController', function ($scope, $state, toastr, Devices) {
	$scope.doNext = function() {
		// device 정보 얻기
		Devices.getOne($scope.deviceMenu.roomId, $scope.deviceMenu.deviceId).then(function (data) {

			Devices.setCurrentData(data);
			$state.go('menu.advanced.device.edit-layout');

		}).catch(toastr.error);
	};

});

/**
 * @ngdoc controller
 * @name DeviceMenuLayoutController
 * @description
 *  device button layout 편집 화면
 */
app.controller('DeviceMenuLayoutController', function ($scope, $state, $modal, toastr, Systems, Devices) {

	$scope.finish = function() {

		var pages = [],
				hardPages = angular.copy($scope.deviceMenu.hard.pages),
				hardBtns = _.flatten(hardPages, 'btns'),
				menuPages = angular.copy($scope.deviceMenu.menu.pages),
				pageId = 0,
				buttonId = 8192;

		function rewriteMacro(btn) {
			var newMacro = {};
			if(btn.macro) {
				///Devices Service의 getOne function을 통해 Device정보를 얻어 올때 각 button의 macro data를 object로 변환하였으나
				///혹시 몰라서 macro가 string type인 경우에 대해서도 처리.
				newMacro = angular.isObject(btn.macro) ? btn.macro[0] : JSON.parse(btn.macro)[0];
			}

			if(btn.func) {
				if (_.isEmpty(newMacro)) {
					newMacro.type = 1;
					newMacro.device_id = parseInt($scope.deviceMenu.deviceData.id, 10);
				}

				console.log('rewriteMacro-btn=', btn);
			
				newMacro.func = btn.func.id;

				if (btn.repeatSetting === 'custom') {
					newMacro.repeat_min = btn.repeat_min;
					newMacro.repeat_macro = btn.repeat_macro;
				} else {
					delete newMacro.repeat_min;
					delete newMacro.repeat_macro;
				}

				if (!btn.press_hold) {
					newMacro.press_hold = 0;
				} else {
					newMacro.press_hold = 1;
				}
			}

			//console.log('rewriteMacro- newMacro=', newMacro);
			return JSON.stringify([newMacro]);
		}

		_.remove(hardBtns, function(button) {
			return !button.display_text;
		});

		_.forEach(hardBtns, function(hardBtn) {
			hardBtn.macro = rewriteMacro(hardBtn);
			hardBtn.page_btn_idx = -1;
			delete hardBtn.func;
			delete hardBtn.repeatSetting;
			delete hardBtn.repeat_min;
			delete hardBtn.repeat_macro;
			delete hardBtn.press_hold;
			delete hardBtn.name;
		});


		pages.push({ id: -1, btns: hardBtns });

		_.forEach(menuPages, function(page) {
			if (_.filter(page.btns, 'display_text').length) {
				page.id = pageId;
				pageId++;

				if (page.page_hidden == 1) {
					page.hidden = 1;
				} else {
					page.hidden = 0;
				}

				_.remove(page.btns, function(button) {
					return !button.display_text;
				});

				_.forEach(page.btns, function(btn) {

					if (btn.display_text) {

						btn.id = buttonId;
						buttonId++;

						btn.hidden = (true == btn.hidden ? 1:0);

						btn.macro = rewriteMacro(btn);

						delete btn.func;
						delete btn.repeatSetting;
						delete btn.repeat_min;
						delete btn.repeat_macro;
						delete btn.press_hold;
					}

				});

				pages.push(page);
			}
		});

	 
		var returnDevice = angular.copy($scope.deviceMenu.deviceData); 
		_.assign(returnDevice, {pages: pages});

		//if these properties are seleceted as `None` on the client their properties will be null
		//in that case, they to be changed to -1 
		
		returnDevice.meta_pwr_on =  returnDevice.meta_pwr_on || -1;
		returnDevice.meta_pwr_off = returnDevice.meta_pwr_off || -1;
		returnDevice.meta_pwr_toggle = returnDevice.meta_pwr_toggle || -1;
		returnDevice.meta_in_toggle = returnDevice.meta_in_toggle || -1;
		

		var jsonData = JSON.stringify({ data: returnDevice });

		// 해당 device 저장
		Devices.storeOne($scope.deviceMenu.roomId, $scope.deviceMenu.deviceId, jsonData).then(function(data) {
			// system update
			Systems.updateSystem($scope.system.id);

			$state.go('menu.advanced.options');
		}, function (error) {
			toastr.error(error, 'Error');
		});

	};

});
