(function () {
    angular.module('vip-projects')
        .directive('skills', function (SkillsService, jaro_winklerService) {
            return {
                templateUrl: 'features/reusable-services/skills/SkillsTemplate.html',
                restrict: 'E',
                scope: { skills: "=" },
                controllerAs: 'skillsController',
                controller: function ($attrs, $scope, $timeout) {
                    var vm = this;
                    vm.label = $attrs.label;
                    $scope.skills = $scope.skills ? $scope.skills : "";

                    //User Story: FA18 | VIP-176, mvale080 (changed allowedSkills to return a small set of matching strings rather than a long list of all skills)
                    vm.allowedSkills = function (skill) {
                        return (jaro_winklerService.distance(skill, (!$scope.skills ? "" : $scope.skills.split(',').pop().trim().toLowerCase())) > 0.85)
                            && !(vm.currentSkills.map(function (skill) {
                                return skill.trim().toLowerCase();
                            }).includes(skill.trim().toLowerCase()));
                    };
                    vm.hideDropdown = function () {
                        $timeout(function () {
                            vm.showDropdown = false;
                        }, 1000);
                    };
                    vm.addSkill = function (skill) {
                        if (vm.currentSkills.length > 0 &&
                            skill.toLowerCase().startsWith(vm.currentSkills[vm.currentSkills.length - 1].trim().toLowerCase())) {
                            vm.currentSkills.pop();
                        }
                        vm.currentSkills.push(skill.trim());
                        $scope.skills = vm.currentSkills.join(', ') + ", ";
                    };
                    vm.populateSkills = function () {
                        if (!$scope.skills) {
                            vm.currentSkills = [];
                        } else {
                            var skills = $scope.skills.split(',');
                            var last = skills.pop();
                            skills = skills.map(function (skill) { return skill.trim(); })
                                .filter(function (skill) { return skill !== "" });
                            vm.currentSkills = skills;
                            $scope.skills = vm.currentSkills.join(', ');
                            vm.currentSkills.push(last);
                            if (vm.currentSkills.length === 1) {
                                $scope.skills = last;
                            } else {
                                $scope.skills += "," + last;
                            }
                        }
                        //console.log("The current skills list is " + vm.currentSkills);
                    };
                    //User Story: FA18 | VIP-153, mvale080 A user can choose to auto complete a skill if it matches one that already exists in a list of skills.
                    /*
                    The function vm.nextskill reads every keydown press event. if it matches the key for Tab, which is keycode 9, it prevents the default action of Tab,
                    which is to move to the next selectable object on the current webpage. In place of that, it instead calls the first match based on the jaro_winkler
                    comparison. This should match with the first box shown to the user on the far left. It will fill in the rest of the typed word as well, ensuring no
                    duplicate letters added.
                    */
                    vm.nextSkill = function ($event) {
                        if ($event.keyCode == 9) {
                            $event.preventDefault();
                            console.log("Tab was pressed");
                            if (!$scope.skills) {
                                console.log("Skills is empty");
                                return;
                            } else {
                                var skills = $scope.skills.split(',');
                                var last = skills.pop();
                                console.log("Attempting to add skill")
                                var SkilltoAdd = vm.skillsList.find(function (skill) {
                                    if (jaro_winklerService.distance(skill, last) > 0.85 && !(vm.currentSkills.map(function (skill) {
                                        return skill.trim().toLowerCase();
                                    }).includes(skill.trim().toLowerCase()))) {
                                        console.log("found skill " + skill);
                                        return skill;
                                    }
                                });
                                if(SkilltoAdd){
                                    vm.addSkill(SkilltoAdd);
                                }
                                return;

                            }
                        }
                    };
                    vm.currentSkills = [];

                    //User Story: FA18 | VIP-174, mvale080 (adjusted function below to handle the change in input)
                    SkillsService.retrieveSkills().then(function (data) {
                        vm.SkillsFromDB = data;
                        vm.skillsList = [];
                        vm.SkillsFromDB.forEach(function(skill, index){
                            if(!vm.skillsList){
                                vm.skillsList = concat(skill.skill);
                            }
                            else{
                                vm.skillsList = vm.skillsList.concat(skill.skill);
                            }
                        }
                        );
                    });
                }
            }
        });
})();