'use strict';

angular.module('upgrade', []).controller('DashBoardCtrl', [
  '$scope',
  'configServer',
  'Util',
  'session',
  'moment',
  '$uibModal',
  'exchangeRate',
  'currencyUnit',
  '$state',
  '$sce',
  function (
    $scope,
    configServer,
    Util,
    session,
    moment,
    $uibModal,
    exchangeRate,
    currencyUnit,
    $state,
    $sce
  ) {
    //get currency of team
    $scope.currentUser = session.getUser();

    $scope.transmissionPercent = 0;
    $scope.isLoadData = false;
    $scope.isLoadData2 = true;
    $scope.items = [];

    $scope.language = session.getCurrentLanguage();
    if ($scope.language === 'ja') {
      $scope.showVideoJa = true;
    } else {
      $scope.showVideoJa = false;
    }
    $scope.loadProject = function () {
      $scope.isLoadData = true;
      Util.createRequest(
        configServer.API_URL + '/projects/secure-mail',
        'GET',
        {},
        function (response) {
          var status = response.status;
          $scope.resultProject = response.data;
          if (status === 200) {
            if (response.data.length === 0) {
              $scope.filePath = '';
              $scope.isLoadData = false;
              $scope.isLoadData2 = false;
            } else {
              $scope.myFileProjects = response.data[0];
              $scope.projectId = $scope.myFileProjects.project_id;
              $scope.filePath =
                '<a href="#!/my-email">' + Util.translate('COMMON.MY_EMAIL') + '</a>';
              $scope.browseCurrentShareFile();
            }
            $scope.getStatisticDashBoard();
          } else {
            Util.createToast({ className: 'danger', content: 'SERVER_ERROR.' + status });
          }
          $scope.isLoadData = false;
        },
        function (error) {
          $scope.isLoadData = false;
          $scope.isLoadData2 = false;
          Util.createToast({
            className: 'danger',
            content: 'SERVER_ERROR.' + error.status,
          });
        }
      );
    };
    $scope.loadProject();

    //get current 4 files
    $scope.browseCurrentShareFile = function () {
      $scope.isLoadData = false;
      var browseParam = {
        project_id: $scope.projectId,
        active: true,
        asc_sort: false,
        length: 4,
        search_global: false,
        search_key: '',
        start: 1,
        folder_only: true,
        sort_field: 'modifiedDate',
      };
      Util.createRequest(
        configServer.API_URL + '/folders/0/items',
        'GET',
        browseParam,
        function (response) {
          var status = response.status;
          if (status === 200) {
            $scope.items = response.data.file_info;
          } else {
            Util.createToast({ className: 'danger', content: 'SERVER_ERROR.' + status });
          }
          $scope.isLoadData2 = false;
        },
        function (error) {
          $scope.isLoadData2 = false;
          Util.createToast({
            className: 'danger',
            content: 'SERVER_ERROR.' + error.status,
          });
        }
      );
    };

    function convertFileSizeToMegaByte(bytes) {
      return (bytes / (1024 * 1024)).toFixed(2);
    }

    //get Statistic
    $scope.getStatisticDashBoard = function () {
      Util.createRequest(
        configServer.API_URL + '/dashboard/secure_mail/teams',
        'GET',
        {},
        function (response) {
          var status = response.status;
          if (status === 200) {
            console.log('response', response);

            $scope.transmissionPercent =
              (response.data.used_transmission / response.data.total_transmissions) * 100;
            $scope.transmissionTotal = response.data.total_transmissions;
            $scope.transmissionUsed = response.data.used_transmission;
            $scope.totalStorage = response.data.total_storage;
            $scope.usedStorage = response.data.used_storage;
            $scope.usedStoragePercent = ($scope.usedStorage / $scope.totalStorage) * 100;
            $scope.totalStorageConvert = Util.convertFileSize($scope.totalStorage);
            $scope.usedStorageConvert = Util.convertFileSize($scope.usedStorage);
            $scope.data = [
              convertFileSizeToMegaByte($scope.usedStorage),
              convertFileSizeToMegaByte($scope.availableStorage),
            ];
            $scope.colorOptions = ['#e74a3b', '#1cc88a'];
          } else {
            Util.createToast({ className: 'danger', content: 'SERVER_ERROR.' + status });
          }
        },
        function (error) {
          Util.createToast({
            className: 'danger',
            content: 'SERVER_ERROR.' + error.status,
          });
        }
      );
    };

    $scope.formatDateTime = function (date) {
      return Util.formatDate(moment(date), $scope.currentUser.time_zone, 'MM/DD/YYYY HH:mm:ss');
    };

    $scope.openFolder = function (item, projectId) {
      if (item.status !== 1) {
        return;
      }
      if (item.folder === true) {
        $state.go('myEmail', { folder_id: item.id });
      }
    };

    $scope.openNewTab = function (url) {
      window.open(url, '_blank');
    };
  },
]);
