angular.module(
    'odoo',
    [
        'ionic',
        'ionic-toast',
        'ngCordova',
        'ngStorage',
        'xml-rpc',
        'jett.ionic.filter.bar',
        'pascalprecht.translate'
    ]
)

    .run(function (
        $ionicPlatform,
        $state,
        $localStorage
    ) {

        $ionicPlatform.ready(function () {
            if (window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                cordova.plugins.Keyboard.disableScroll(true);
            }

            if (window.StatusBar) {
                StatusBar.overlaysWebView(false);
                StatusBar.backgroundColorByHexString('#387ef5');
            }

            if (window.sqlitePlugin) {
                db = sqlitePlugin.openDatabase({ name: 'odoo.db', location: 'default' });

                if ($localStorage.id) {
                     $state.go('app.main');
                } else {
                    $state.go('configuration');
                }
            }
        });
    })

    .config(function (
        $stateProvider,
        $urlRouterProvider,
        $ionicConfigProvider,
        $translateProvider,
        $ionicFilterBarConfigProvider
    ) {

        $stateProvider
            .state('app', {
                url: '/app',
                abstract: true,
                templateUrl: 'templates/menu.html'
            })

            .state('app.main', {
                url: '/main',
                views: {
                    'content': {
                        templateUrl: 'templates/main.html',
                        controller: 'MainController'
                    }
                }
            })

            .state('app.sales', {
                url: '/sales',
                views: {
                    'content': {
                        templateUrl: 'templates/sales/sales.html',
                        controller: 'SaleController'
                    }
                }
            })

            .state('app.customers', {
                url: '/customers',
                views: {
                    'content': {
                        templateUrl: 'templates/sales/customers.html',
                        controller: 'CustomersController'
                    }
                }
            })

            .state('app.leads', {
                url: '/leads',
                views: {
                    'content': {
                        templateUrl: 'templates/sales/leads.html',
                        controller: 'LeadsController'
                    }
                }
            })

            .state('app.opportunities', {
                url: '/opportunities',
                views: {
                    'content': {
                        templateUrl: 'templates/sales/opportunities.html',
                        controller: 'OpportunitiesController'
                    }
                }
            })

            .state('app.preferences', {
                url: '/preferences',
                views: {
                    'content': {
                        templateUrl: 'templates/preferences.html',
                        controller: 'PreferencesController'
                    }
                }
            })

            .state('configuration', {
                url: '/configuration',
                templateUrl: 'templates/configuration.html',
                controller: 'ConfigurationController'
            });

        $translateProvider.translations('es', {
            Opportunities: 'Oportunidades',
            New: 'Nuevo',
            Dead: 'Muerto',
            Qualification: 'Calificación',
            Proposition: 'Propuesta',
            Negotiation: 'Negociación',
            Won: 'Ganado',
            Lost: 'Perdido'
        });

        $ionicConfigProvider.spinner.icon('ios');

        $ionicFilterBarConfigProvider.theme('positive');
        $ionicFilterBarConfigProvider.placeholder('Buscar');

        $translateProvider.useSanitizeValueStrategy('escape');
        $translateProvider.preferredLanguage('es');
        $translateProvider.fallbackLanguage('es');
    });
