angular
        .module('carfriend')
        .controller('SearchCtrl', SearchCtrl);

function SearchCtrl($scope, $filter, CarService, ColorService, ionicMaterialInk, ionicMaterialMotion, NotificationsService, UsersService, AuthService) {
    var vm = this;

    vm.selected = false;
    vm.searchLoading = false;
    vm.dirty = false;
    vm.dirtyChat = false;
    
    
    vm.colorsOffer = ColorService.getColors();

    vm.doClear = doClear;
    vm.doSearch = doSearch;
    vm.selectCar = selectCar;
    
    vm.doClearChat = doClearChat;
    vm.chatSearching = chatSearching;
        
    initSearchField();
    initChatSearchField();
    ionicMaterialInk.displayEffect();
    ionicMaterialMotion.ripple();

    $scope.$watch('vmm.evidenceNumber', function (val) {
        vm.evidenceNumber = $filter('uppercase')(val);
        if (val === '')
            vm.dirty = false;
    }, true);

    $scope.$watch('vmm.carColor', function (val) {
        if (val === '')
            vm.dirty = false;
        else
            vm.dirty = true;
    }, true);

    $scope.$watch('vmm.chatObject', function (val) {
        if (val === '')
            vm.dirtyChat = false;
        else
            vm.dirtyChat = true;
    }, true);
    
    $scope.$on('clearSearchField', function(){
        doClear(); 
    });
    $scope.$on('clearChatsField', function(){
        doClearChat(); 
    });

    ///////////////

    function doSearch() {
        vm.result = {};
        vm.searchLoading = true;
        CarService.search(vm.evidenceNumber, vm.carColor).then(function (find) {
            vm.searchLoading = false;
            if (find === "no match") {
                vm.result.notFound = true;
                vm.result.error = "Táto farba nepatrí hľadanému vozidlu.";
            } else {
                if (find === "no registered") {
                    vm.result.notFound = true;
                    vm.result.error = "Zatiaľ nie je možné poslať upozornenie nezaregistrovanému vozidlu. Pracujeme aj na tejto možnosti.";
                } else {
                    isItMe(find.evidenceNumber) ? (vm.result.notFound = true, vm.result.error = "Nemôžete poslať upozornenie svojmu vozidlu.") : vm.result = find;
                }
            }
        }, function (error) {
        });
    }
    ;

    function doClear() {
        initSearchField();
        vm.dirty = false;
        vm.selected = false;
        NotificationsService.setVictimID('');
        vm.searchLoading = false;
    }
    ;

    function selectCar(id) {
        vm.selected = !vm.selected;
        if (vm.selected)
            NotificationsService.setVictimID(id);
        else
            NotificationsService.setVictimID('');
    }

    function initSearchField() {
        vm.evidenceNumber = '';
        vm.carColor = '';
        vm.chatObject = '';
        vm.result = {};
    }
    ;
    
    /*chaty*/
    
    function doClearChat() {
        initChatSearchField();
        vm.dirtyChat = false;
        vm.searchChatLoading = false;
    }
    ;
    
    function initChatSearchField() {
        vm.chatObject = '';
        vm.chatSearchResults = {};
    }
    ;

    function chatSearching(user) {
        vm.searchChatLoading = true;
        vm.chatSearchResults = {};
        CarService.findCarByEvc(user).then(function (success) {
            if (success.data && success.data.data && success.data.totalRows === 1) {
                vm.searchChatLoading = false;
                isItMe(success.data.data[0].evidenceNumber) ? vm.chatSearchResults.isItMe = true : vm.chatSearchResults = success.data.data[0];
            } else
                UsersService.fetchByUsername(user).then(function (success) {
                    vm.searchChatLoading = false;
                    if (success.data && success.data.data && success.data.totalRows === 1) {
                         isItMe(success.data.data[0].username) ? vm.chatSearchResults.isItMe = true : vm.chatSearchResults = success.data.data[0];
                    } else
                        vm.chatSearchResults.notFound = true;
                }, function (err) {
                    vm.searchChatLoading = false;
                    vm.chatSearchResults.notFound = true;
                });
        }, function (err) {
            vm.searchChatLoading = false;
            vm.chatSearchResults.notFound = true;
        });
    };
    
    function isItMe(object) {
        var currentUser = AuthService.getUser();
        var car;
        if (currentUser.details.userType === "driver")
            car = CarService.getCar(currentUser.details.id);
        if(car)
            return (car.evidenceNumber === object || currentUser.details.username === object) ? true : false;
        else
            return currentUser.details.username === object ? true : false;    
    };
};