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

.run(function(
    $ionicPlatform,
    $state,
    configFactory
) {
    
     $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');
        }

        db = window.sqlitePlugin.openDatabase({ name: 'odoo.db', location: 'default' });
        db.executeSql("CREATE TABLE IF NOT EXISTS 'user' (\n\
                       'id' INTEGER NOT NULL  PRIMARY KEY AUTOINCREMENT,\n\
                       'remote_id' INTEGER DEFAULT 0,\n\
                       'host' VARCHAR(35) DEFAULT 0,\n\
                       'port' INTEGER DEFAULT 0,\n\
                       'database' VARCHAR(35) DEFAULT NULL,\n\
                       'username' VARCHAR(35) DEFAULT NULL,\n\
                       'password' VARCHAR(35) DEFAULT NULL)");
        db.executeSql("CREATE TABLE IF NOT EXISTS 'partner' (\n\
                       'id' INTEGER DEFAULT NULL PRIMARY KEY AUTOINCREMENT,\n\
                       'remote_id' INTEGER DEFAULT 0,\n\
                       'modified' INTEGER DEFAULT 0,\n\
                       'modifiedDate' TEXT DEFAULT CURRENT_TIMESTAMP,\n\
                       'name' VARCHAR(35) DEFAULT NULL,\n\
                       'city' VARCHAR(35) DEFAULT NULL,\n\
                       'mobile' VARCHAR(20) DEFAULT NULL,\n\
                       'phone' VARCHAR(20) DEFAULT NULL,\n\
                       'fax' VARCHAR(20) DEFAULT NULL,\n\
                       'email' VARCHAR(100) DEFAULT NULL,\n\
                       'street' VARCHAR(35) DEFAULT NULL,\n\
                       'street2' VARCHAR(35) DEFAULT NULL,\n\
                       'image_medium' BLOB DEFAULT NULL,\n\
                       'image_small' BLOB DEFAULT NULL,\n\
                       'comment' VARCHAR(160) DEFAULT NULL,\n\
                       'customer' INTEGER DEFAULT 0,\n\
                       'employee' INTEGER DEFAULT 0,\n\
                       'is_company' INTEGER DEFAULT 0,\n\
                       'debit' INTEGER DEFAULT 0,\n\
                       'debit_limit' INTEGER DEFAULT 0,\n\
                       'opportunity_count' INTEGER DEFAULT 0,\n\
                       'contracts_count' INTEGER DEFAULT 0,\n\
                       'journal_item_count' INTEGER DEFAULT 0,\n\
                       'meeting_count' INTEGER DEFAULT 0,\n\
                       'phonecall_count' INTEGER DEFAULT 0,\n\
                       'sale_order_count' INTEGER DEFAULT NULL,\n\
                       'total_invoiced' INTEGER DEFAULT NULL);");

        db.executeSql("CREATE TABLE IF NOT EXISTS 'lead' (\n\
                      'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n\
                      'remote_id' INTEGER DEFAULT 0,\n\
                      'modified' INTEGER DEFAULT 0,\n\
                      'modifiedDate' TEXT DEFAULT CURRENT_TIMESTAMP,\n\
                      'name' VARCHAR(35) DEFAULT NULL,\n\
                      'description' VARCHAR(100) DEFAULT NULL,\n\
                      'contact_name' VARCHAR(100) DEFAULT NULL,\n\
                      'phone' VARCHAR(20) DEFAULT NULL,\n\
                      'mobile' VARCHAR(20) DEFAULT NULL,\n\
                      'fax' VARCHAR(20) DEFAULT NULL,\n\
                      'street' VARCHAR(35) DEFAULT NULL,\n\
                      'street2' VARCHAR(35) DEFAULT NULL,\n\
                      'meeting_count' INTEGER DEFAULT 0,\n\
                      'message_bounce' INTEGER DEFAULT 0,\n\
                      'planned_cost' INTEGER DEFAULT 0,\n\
                      'planned_revenue' INTEGER DEFAULT 0,\n\
                      'priority' INTEGER DEFAULT 0,\n\
                      'probability' INTEGER DEFAULT 0,\n\
                      'type' VARCHAR(20) DEFAULT NULL,\n\
                      'user_id' INTEGER DEFAULT 0,\n\
                      'partner_id' INTEGER DEFAULT 0);");

        configFactory(function (configuration) {
            if (configuration) {
                $state.go('app.main');
            } else {
                $state.go('configuration');
            }
        }, function (err) {
            $state.go('configuration');
        });
    });
})

.config(function (
    $stateProvider,
    $urlRouterProvider,
    // $ionicConfigProvider,
    $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'
        });

    // $urlRouterProvider.otherwise('/configuration');

    // $ionicConfigProvider.tabs.position('bottom');
    // $ionicConfigProvider.backButton.icon('ion-chevron-left');

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