angular.module('odoo')

    /**
     *
     */
    .controller('ConfigurationController', function (
        $scope,
        $state,
        $ionicLoading,
        $ionicPopup,
        $localStorage,
        odooFactory,
        databaseFactory
    ) {
        $scope.loading = false;
        $scope.configuration = $localStorage.$default({
            host: '192.168.2.105',
            port: 8069,
            database: 'odoo',
            username: 'admin',
            password: 'admin',
            id: null,
            user_name: null,
            company_id: null
        });

        /**
         *
         */
        $scope.auth = function () {
            $scope.loading = true;

            odooFactory.auth(function (id) {
                $scope.configuration.id = id;

                odooFactory.read('res.users', [['id', '=', id]], function (user) {
                    var data = user[0];

                    $scope.configuration.user_name = data.name;
                    $scope.configuration.company_id = data.company_id[0];

                    databaseFactory.initTables(function (result) {
                        $scope.loading = false;
                        $state.go('app.main');
                    }, function (err) {
                        $scope.loading = false;
                        console.log(err);
                    });

                }, function (readErr) {
                    $scope.loading = false;
                    console.log(readErr);
                });

            }, function (err) {
                $scope.loading = false;
                console.log(err);
            });
        }
    });
