app.controller('myDevicesNavi', ['$rootScope', '$scope', '$http', '$interval', 
    function($rootScope, $scope, $http, $interval) {

        var clearFormValues = function() {
            $scope.myDeviceName = '';
            $scope.myDeviceType = '';
            $scope.myDeviceIpAddr = '';

            $scope.deviceNameErr = '';
            $scope.deviceTypeErr = '';
            $scope.ipAddrErr = '';
            $scope.createErr = '';
            $scope.userDefinedDeviceType = '';
        };
        clearFormValues();

        $scope.deviceCreatedMsg = '';
        $scope.createMore = function() {
            $scope.deviceCreatedMsg = '';
        };

        $scope.validationShown = false;

        //load ips from database
        $http( { method: "GET", 
                    url: '/backend/lease.php', 
                params: { verb: 'get_ips'} })
        .then(function loginSuccess(response) {
            if (response.data.exitCode === 0) {
                $scope.ips = response.data.rows;
                //mixpanel.track("pricing-settings", {});
            } else {
                $scope.err = response.data.message;
                //mixpanel.track("pricing-settings", {"error": $scope.details.err});
            }
            $scope.isInProgress = false;
        }, function loginError(response) {
            $scope.err = response.statusText;
            $scope.isInProgress = false;
            //mixpanel.track("pricing-settings", {"error": $scope.details.err});
        });

        var createDeviceRecord = function() {
            let actualDeviceType = ($scope.userDefinedDeviceType === '') ? $scope.myDeviceType : $scope.userDefinedDeviceType;
            $http( { method: "GET", url: '/backend/device.php', 
                     params: { verb: "create", 
                               session: $rootScope.sessionToken, 
                               name: $scope.myDeviceName, 
                               type: actualDeviceType, 
                               addr: $scope.myDeviceIpAddr } })
            .then(function responseSuccess(response) {
                if (response.data.exitCode === 0) {
                    $scope.deviceCreatedMsg = 'Device was successfully registered';
                    $scope.createdDeviceName = $scope.myDeviceName;
                    $scope.createdDeviceType = actualDeviceType;
                    $scope.createdDeviceAddr = $scope.myDeviceIpAddr;
                    $scope.$emit('event_device_created', {});
                    clearFormValues();
                } else {
                    $scope.createErr = response.data.message;
                }
                $scope.isInProgress = false;
            }, function loginError(response) {
                $scope.isInProgress = false;
                $scope.createErr = 'connection error';
            });
        };

        $scope.validateDeviceName = function() {
            $scope.deviceNameErr = '';
            var patt1 = /^[a-z]+/i;
            var patt2 = /[a-z0-9]$/i;
            var patt3 = /^(\w|\d|\.|\_|\-|\s)+$/i;

            if ($scope.myDeviceName.length === 0) {
                $scope.deviceNameErr = 'please enter device name';
            } else
            if (!patt1.test($scope.myDeviceName)) {
                $scope.deviceNameErr = 'first symbol should be \'A-Z\'';
            } else
            if ($scope.myDeviceName.length <= 2) {
                $scope.deviceNameErr = 'Device name is too short';
            } else
            if (!patt2.test($scope.myDeviceName)) {
                $scope.deviceNameErr = 'last symbol should be \'A-Z\' or \'0-9\'';
            } else
            if ($scope.myDeviceName.length >= 16) {
                $scope.deviceNameErr = 'Device name is too long';
            } else {
                var res3 = patt3.test($scope.myDeviceName);
                if (!res3) {
                    $scope.deviceNameErr = 'accepted only A-Z, 0-9, `.`, `_`, `-`, ` ` ';
                }
            }
            return $scope.deviceNameErr === '';
        };

        $scope.validateIpAddr = function() {
            $scope.ipAddrErr = '';
            if ($scope.myDeviceIpAddr === '---Please select---' || $scope.myDeviceIpAddr === '') {
                $scope.ipAddrErr = 'select the IP address for your device'; 
            }
        };

        $scope.validateDeviceType = function() {
            $scope.deviceTypeErr = '';
            if ($scope.myDeviceType === '---Please select---' || $scope.myDeviceType === '') {
                $scope.deviceTypeErr = 'select type of your device'; 
            }
        };

        $scope.createPortMapping = function() {
            $scope.validateDeviceName();
            $scope.validateIpAddr();
            $scope.validateDeviceType();

            if ($scope.deviceNameErr === '' && $scope.deviceTypeErr === '' && $scope.ipAddrErr === '') {
                $scope.isInProgress = true;
                createDeviceRecord();
            } else {
                $scope.validationShown = true;
            }
        };
}]);
