angular
        .module('carfriend')
        .config(config)
        .run(run);

function run($ionicPlatform, $rootScope, $state, AuthService, Backand) {
    $ionicPlatform.ready(function () {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins.Keyboard) {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

            // Don't remove this line unless you know what you are doing. It stops the viewport
            // from snapping when text inputs are focused. Ionic handles this internally for
            // a much nicer keyboard experience.
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        var isMobile = !(ionic.Platform.platforms[0] == "browser");
        Backand.setIsMobile(isMobile);
        Backand.setRunSignupAfterErrorInSigninSocial(true);
    });

    function unauthorized() {
        console.log("user is unauthorized, sending to login");
        $state.go('login');
    }

    function signout() {
        AuthService.signout();
    }

    $rootScope.$on('unauthorized', function () {
        unauthorized();
    });

    $rootScope.$on('$stateChangeSuccess', function (event, toState) {
        if (toState.name === 'login') {
            signout();
        } else if (toState.name !== 'login' && Backand.getToken() === undefined) {
            unauthorized();
        }
    });

    Backand.on('new_notification', function (data) {
        console.log("Prislo nove upozornenie urcene len pre mna");
        $rootScope.$broadcast("newNotification", data);
        $rootScope.$broadcast('incBadger'); 
    });

    Backand.on('near_notification', function (data) {
        console.log("Prislo blizke upozornenie urcene pre viacerych");
        $rootScope.$broadcast("newNotification", data);
        $rootScope.$broadcast('incBadger'); 

    });
      Backand.on('new_message', function (data) {
        console.log("Prisla nova sprava", data);
       $rootScope.$broadcast("newMessage", data);
        $rootScope.$broadcast('incChatBadger'); 

    });


}


function config(BackandProvider, ionicDatePickerProvider) {
    //Backand set up
    BackandProvider.setAppName('carfriends');
    BackandProvider.setSignUpToken('4de64990-1159-4db3-aab2-77d918e810d2');
 //   BackandProvider.setAnonymousToken('8f432fe2-33c1-4bef-9f4b-9c5502c8f3b1');
    BackandProvider.runSocket(true); //enable the web sockets that makes the database realtime
    
    var datePickerObj = {
      inputDate: new Date(),
      setLabel: 'Nastaviť',
      todayLabel: 'Dnes',
      closeLabel: 'Zavrieť',
      mondayFirst: true,
      weeksList: ["N", "P", "U", "S", "Š", "P", "S"],
      monthsList: ["Január", "Február", "Marec", "Apríl", "Maj", "Jún", "Júl", "August", "September", "Október", "November", "December"],
      templateType: 'popup',
      from: new Date(2016, 8, 1),
      to: new Date(2025, 8, 1),
      dateFormat: 'dd MMMM yyyy',
      closeOnSelect: true,
      disableWeekdays: []
    };
    ionicDatePickerProvider.configDatePicker(datePickerObj);
}