angular.module('odoo')

    /**
     *
     */
    .factory('databaseFactory', function (
        odooFactory,
        asyncLoopFactory
    ) {

        var mappings = [
            {
                model: 'res.partner',
                table: 'partner'
            },
            {
                model: 'crm.lead',
                table: 'crm_lead'
            },
            {
                model: 'crm.case.stage',
                table: 'stages'
            }
        ];

        /**
         *
         */
        var buildCreateStatement = function (table, model) {
            var statement = 'CREATE TABLE IF NOT EXISTS ' + table + ' (\n';

            for (var key in model) {
                if (model.hasOwnProperty(key) && typeof model[key] == 'object') {
                    if (model[key].type == 'float') {
                        statement += key + ' REAL DEFAULT 0,\n';
                    }

                    if (model[key].type == 'char' || model[key].type == 'text' || model[key].type == 'datetime') {
                        statement += key + ' TEXT DEFAULT NULL,\n';
                    }

                    if (model[key].type == 'integer' || model[key].type == 'selection' || model[key].type == 'boolean' || model[key].type == 'many2one' || model[key].type == 'many2many') {
                        statement += key + ' INTEGER DEFAULT 0,\n';
                    }

                    if (model[key].type == 'one2many') {
                        continue;
                    }
                }
            }

            statement += 'remote_id INTEGER DEFAULT 0,\n' +
                'modified INTEGER DEFAULT 0,\n' +
                'modified_date TEXT DEFAULT CURRENT_TIMESTAMP\n';
            
            console.log(statement);

            return statement + ');'
        }

        /**
         *
         */
        var fetchMaps = function (success, error) {
            var maps = [];

            asyncLoopFactory(mappings.length, function (loop) {
                var map = mappings[loop.iteration()];

                odooFactory.fields(map.model, function (response) {
                    maps.push({ table: map.table, model: response });

                    loop.next();
                }, function (odooErr) {
                    console.log(odooErr);

                    loop.next();
                });

            }, function () {
                success(maps);
            });
        }

        /**
         *
         */
        var initTables = function (success, error) {
            fetchMaps(function (maps) {

                asyncLoopFactory(maps.length, function (loop) {
                    var map = maps[loop.iteration()];
                    var sql = buildCreateStatement(map.table, map.model);

                    db.executeSql(sql, [], function (result) {
                        console.log(result);
                        loop.next();
                    }, function (err) {
                        console.log(err);
                        loop.next();
                    });

                }, function () {
                    success(maps);
                });

            }, function (err) {
                error(err);
            });
        }

        return {
            fetchMaps: fetchMaps,
            initTables: initTables
        }
    });