/// <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) =>{  
      console.log($scope.labels);  
      $scope.labelClick = (entity, key:string, value:string) => {
      // log.debug("handleClick, entity: ", entity, " key: ", labelType, " value: ", value);
      console.log("cacacacac");
      $scope.$emit('dataLabelFilterUpdate', value)
    }
    $scope.labelClass = containerLabelClass;
  }]);

  // controller that deals with the labels per pod
  export var Labels = controller("Labels", ["$scope", "$location", ($scope, $location) => {
    console.log($scope.entity);
    $scope.labels = [];
    var labelKeyWeights = {
      "region": 1,
      "system": 2,
      "type": 3,
      "batch": 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.deleteRow = (entity) =>{
      $scope.$emit('deleteRow', entity);
    }
  }]);
}
