'use strict';

angular.module('service-testing-tool').controller('EndpointsController', ['$scope', 'Endpoints', '$stateParams', '$state', 'uiGridConstants',
  function($scope, Endpoints, $stateParams, $state, uiGridConstants) {
    $scope.schema = {
      type: "object",
      properties: {
        id: { type: "integer" },
        name: { type: "string" },
        description: { type: "string" },
        host: { type: "string" },
        port: { type: "integer" },
        protocol: { type: "string" },
        ctxroot: { type: "string" }
      },
      "required": ["name", "description", "host", "port", "protocol"]
    };

    $scope.form = [
      {
        key: "name",
        title: "Name"
      },
      {
        key: "description",
        title: "Description",
        type: "textarea"
      },
      {
        key: "protocol",
        title: "Protocol",
        type: "select",
        titleMap: [
          { value: "http", name: "http" },
          { value: "https", name: "https" }
        ]
      },
      {
        key: "host",
        title: "Host"
      },
      {
        key: "port",
        title: "Port"
      },
      {
        key: "ctxroot",
        title: "Context Root"
      }
    ];

    $scope.endpoint = {};

    $scope.create_update = function(form) {
      $scope.$broadcast('schemaFormValidate');

      if (form.$valid) {
        if (this.endpoint.id) {
          var endpoint = this.endpoint;
          endpoint.$update(function() {
            $state.go('endpoint_edit', {endpointId: endpoint.id});
          });
        } else {
          var endpoint = new Endpoints(this.endpoint);
          endpoint.$save(function(response) {
            $state.go('endpoint_edit', {endpointId: response.id});
          });
        }
      }
    };

    $scope.stateGo = function(state) {
      $state.go(state);
    };

    $scope.remove = function(endpoint) {
      endpoint.$remove(function(response) {
          $state.go('endpoint_grid');
      });
    };

    $scope.find = function() {
      $scope.columnDefs = [
        {
          name: 'name', width: 200, minWidth: 100,
          sort: {
            direction: uiGridConstants.ASC,
            priority: 1
          },
          cellTemplate:'gridCellTemplate.html'
        },
        {
          name: 'address', width: 600, minWidth: 300
        }
      ];

      Endpoints.query(function(endpoints) {
        $scope.endpoints = endpoints;
      });
    };

    $scope.findOne = function() {
      if ($stateParams.endpointId) {
        Endpoints.get({
          endpointId: $stateParams.endpointId
        }, function(endpoint) {
          $scope.endpoint = endpoint;
        });
      }
    };
  }
]);

@@ -340,32 +340,47 @@
 %7B type: %22string%22
+, maxLength: 50
  %7D,%0A        desc
@@ -396,32 +396,48 @@
 %7B type: %22string%22
+, maxLength: 500
  %7D,%0A        host
@@ -435,24 +435,34 @@
      host: %7B
+%0A         
  type: %22stri
@@ -456,32 +456,195 @@
   type: %22string%22
+,%0A          maxLength: 50,%0A          pattern: %22%5E((%5Ba-zA-Z0-9%5D%7C%5Ba-zA-Z0-9%5D%5Ba-zA-Z0-9%5C-%5D*%5Ba-zA-Z0-9%5D)%5C.)*(%5BA-Za-z0-9%5D%7C%5BA-Za-z0-9%5D%5BA-Za-z0-9%5C-%5D*%5BA-Za-z0-9%5D)$%22%0A       
  %7D,%0A        port
@@ -654,32 +654,60 @@
  type: %22integer%22
+, minimum: 0, maximum: 65535
  %7D,%0A        prot
@@ -728,16 +728,31 @@
 %22string%22
+, maxLength: 20
  %7D,%0A    
@@ -761,24 +761,34 @@
   ctxroot: %7B
+%0A         
  type: %22stri
@@ -790,16 +790,96 @@
 %22string%22
+,%0A          maxLength: 50,%0A          pattern: %22%5E%5C/((%5Ba-z0-9_%5C.-%5D)+%5C/)*$%22%0A       
  %7D%0A     
@@ -1030,16 +1030,105 @@
 : %22Name%22
+,%0A        validationMessage: %22The Name is required and should be less than 50 characters%22
 %0A      %7D
@@ -1219,16 +1219,113 @@
 extarea%22
+,%0A        validationMessage: %22The Description is required and should be less than 500 characters%22
 %0A      %7D
@@ -1590,138 +1590,412 @@
 ost%22
-%0A      %7D,%0A      %7B%0A        key: %22port%22,%0A        title: %22Port%22%0A      %7D,%0A      %7B%0A        key: %22ctxroot%22,%0A        title: %22Context Root
+,%0A        validationMessage: %22The Host is required and and should be a valid Host name or IP address%22%0A      %7D,%0A      %7B%0A        key: %22port%22,%0A        title: %22Port%22,%0A        validationMessage: %22The Port is required and should between 0 and 65536%22%0A      %7D,%0A      %7B%0A        key: %22ctxroot%22,%0A        title: %22Context Root%22,%0A        validationMessage: %22The Context root is required and should start and end with /
 %22%0A  

format validation