/// <reference path="../../includes.ts"/>
/// <reference path="kubernetesHelpers.ts"/>
/// <reference path="kubernetesPlugin.ts"/>

module Kubernetes {

  // controller for the status icon cell
  export var PodStatus = controller("PodStatus", ["$scope", ($scope) => {
    $scope.statusMapping = (text) => {
      return statusTextToCssClass(text);
    }
  }]);

  _module.controller("Kubernetes.TermController", ($scope, TerminalService) => {
    $scope.canConnectTo = (container) => {
      if (container.securityContext && container.securityContext.privileged) {
        return false;
      }
      return true;
    }
    $scope.openTerminal = (selfLink, containerName) => {
      var id = TerminalService.newTerminal(selfLink, containerName);
      log.debug("Created terminal, id: ", id);
    }
  });

  export var DataLabels = controller("DataLabels",['$scope','$location', ($scope, $location) =>{
    
      $scope.labelClick = (entity, key:string, value:string) => {              
        $scope.$emit('dataLabelFilterUpdate', value, key)
    }
    $scope.labelClass = containerLabelClass;
  }]);

  // controller that deals with the labels per pod
  export var Labels = controller("Labels", ["$scope", "$location", ($scope, $location) => {   
    $scope.labels = [];
    var labelKeyWeights = {
      "cityName": 1,
      "districtName": 2,
      "systemName": 3,
      "year": 4,
      "version": 5
    };
    $scope.$watch('entity', (newValue, oldValue) => {
      if (newValue) {
        // log.debug("labels: ", newValue);
        // massage the labels a bit
        $scope.labels = [];
        angular.forEach(Core.pathGet($scope.entity, ["metadata", "labels"]), (value, key) => {
          if (key === 'fabric8' || key === 'style' || key === 'status' || (key === 'isTarget' && value === 'false') || key === 'isExtract' || key === 'name') {
            // TODO not sure what this is for, the container type?
            return;
          }

          $scope.labels.push({
            key: key,
            title: value
          });
        });

        //  lets sort by key but lets make sure that we weight certain labels so they are first
        $scope.labels = $scope.labels.sort((a, b) => {
          function getWeight(key) {
            return labelKeyWeights[key] || 0;
          }
          var n1 = a["key"];
          var n2 = b["key"];
          var w1 = getWeight(n1);
          var w2 = getWeight(n2);
          var diff = w1 - w2;
          if (diff < 0) {
            return -1;
          } else if (diff > 0) {
            return 1;
          }
          if (n1 && n2) {
            if (n1 > n2) {
              return 1;
            } else if (n1 < n2) {
              return -1;
            } else {
              return 0;
            }
          } else {
            if (n1 === n2) {
              return 0;
            } else if (n1) {
              return 1;
            } else {
              return -1;
            }
          }
        });
      }
    });

    $scope.handleClick = (entity, labelType:string, value) => {
      // log.debug("handleClick, entity: ", entity, " key: ", labelType, " value: ", value);
      $scope.$emit('labelFilterUpdate', value.title)
    }

    $scope.labelClass = containerLabelClass;
  }]);

  //服务状态过滤
  export var Status = controller('Status', ["$scope", "$http", "$interval", "$location", "KubernetesApiURL", ($scope, $http, $interval, $location, KubernetesApiURL) => {
      /*$scope.$watch('entity', (newValue, oldValue) => {
          if(newValue)
             console.log(newValue);
      },true);*/
  }]);

  export var TaskEdit = controller('TaskEdit', ['$scope', ($scope) => {

    $scope.showDeleteOne = {
       show: false,
       item: null,
       open: (entity) => {
         if(entity.status == 1)
          Configs.customAlert("提示", "操作失败: 正在迁移的任务,不能迁移!", '',null, 0, "error");
         else{
           var showDeleteOne = $scope.showDeleteOne;
           showDeleteOne.show = true;
           showDeleteOne.item = entity;
         }
       },
       onOk: () => {
         var showDeleteOne = $scope.showDeleteOne;        
         $scope.$emit('deleteRow', showDeleteOne.item);
       },
       onCancel: ()=>{
         var showDeleteOne = $scope.showDeleteOne;
         showDeleteOne.show = false;
         showDeleteOne.item = null;
       }
    };
    /*$scope.deleteRow = (entity) =>{
      $scope.$emit('deleteRow', entity);
    }*/
  }]);

  export var LoadMask = controller('LoadMask', ['$scope', ($scope) => {    
  }]);
}