angular.module('odoo')

    /**
     * -----------------------------------------------------------------------------
     *  Description:    Local storage manager for customers data
     * -----------------------------------------------------------------------------
     */
    .factory('customersStorageFactory', function () {

        /**
         *
         */
        var save = function (data, success, error) {
            var query = null;

            if (data.id) {
                query = squel.update()
                    .table('partner')
                    .set('remote_id', data.remote_id)
                    .set('modified', 1)
                    .set('modifiedDate', 'CURRENT_TIMESTAMP', { dontQuote: true })
                    .set('name', data.name)
                    .set('city', data.city)
                    .set('mobile', data.mobile)
                    .set('phone', data.phone)
                    .set('fax', data.fax)
                    .set('email', data.email)
                    .set('street', data.street)
                    .set('street2', data.street2)
                    .set('image_medium', data.image_medium)
                    .set('image_small', data.image_small)
                    .set('comment', data.comment)
                    .set('customer', data.customer)
                    .set('employee', data.employee)
                    .set('is_company', data.is_company)
                    .set('debit', data.debit)
                    .set('debit_limit', data.debit_limit)
                    .set('opportunity_count', data.opportunity_count)
                    .set('contracts_count', data.contracts_count)
                    .set('journal_item_count', data.journal_item_count)
                    .set('meeting_count', data.meeting_count)
                    .set('phonecall_count', data.phonecall_count)
                    .set('sale_order_count', data.sale_order_count)
                    .set('total_invoiced', data.total_invoiced)
                    .where('id', data.id);
            } else {
                 query = squel.insert()
                    .into('partner')
                    .set('remote_id', data.remote_id)
                    .set('modified', 0)
                    .set('modifiedDate', 'CURRENT_TIMESTAMP', { dontQuote: true })
                    .set('name', data.name)
                    .set('city', data.city)
                    .set('mobile', data.mobile)
                    .set('phone', data.phone)
                    .set('fax', data.fax)
                    .set('email', data.email)
                    .set('street', data.street)
                    .set('street2', data.street2)
                    .set('image_medium', data.image_medium)
                    .set('image_small', data.image_small)
                    .set('comment', data.comment)
                    .set('customer', data.customer)
                    .set('employee', data.employee)
                    .set('is_company', data.is_company)
                    .set('debit', data.debit)
                    .set('debit_limit', data.debit_limit)
                    .set('opportunity_count', data.opportunity_count)
                    .set('contracts_count', data.contracts_count)
                    .set('journal_item_count', data.journal_item_count)
                    .set('meeting_count', data.meeting_count)
                    .set('phonecall_count', data.phonecall_count)
                    .set('sale_order_count', data.sale_order_count)
                    .set('total_invoiced', data.total_invoiced);
            }

            query = query.toParam();

            db.executeSql(query.text, query.values, function (result) {
                success(query.text.startsWith('INSERT') ? result.insertId : customer.id);
            }, function (err) {
                error(err);
            });
        };

        /**
         *
         */
        var remove = function (data, success, error) {
            if (!data.id) {
                error('Customer cannot delete without provide an id');
            }

            var query = null;

            if (data.remote_id) {
                query = squel.update()
                    .table('partner')
                    .set('modified', 2)
                    .where('id', data.id)
                    .toParam();
            } else {
                query = squel.delete()
                    .from('partner')
                    .where('id', data.id)
                    .toParam();
            }

            query = query.toParam();

            db.executeSql(query.text, query.values, function (result) {
                success(result.rowsAffected);
            }, function (err) {
                error(err);
            });
        };

        /**
         *
         */
        var removeAll = function (success, error) {
            var query = squel.delete()
                .from('partner')
                .where('customer', 1)
                .toParam();

            db.executeSql(query.text, query.values, function (result) {
                success(result.rowsAffected);
            }, function (err) {
                error(err);
            });
        };

        return {
            save: save,
            remove: remove,
            removeAll: removeAll
        }
    });
