'use strict';

app.controller('DeviceSelectionController', function ($scope, $state, toastr, Devices) {
  $scope.doNext = function() {
    Devices.getOne($scope.deviceMenu.roomId, $scope.deviceMenu.deviceId).then(function (data) {

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

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

});



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 = btn.macro ? JSON.parse(btn.macro)[0] : {};

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

      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;
      }


      console.log(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++;

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

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

          if (btn.display_text) {

            btn.id = buttonId;
            buttonId++;


            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);
      }
    });

    console.log(pages);

   
    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 });

    Devices.storeOne($scope.deviceMenu.roomId, $scope.deviceMenu.deviceId, jsonData).then(function(data) {
      $state.go('menu.advanced.options');
    }, function (error) {
      toastr.error(error, 'Error');
    });   

  };

});
