angular.module('odoo')

    .factory('leadsStorageFactory', function (
        $localStorage
    ) {

        var configuration = $localStorage;

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

            if (data.id) {
                query = squel.update()
                    .table('crm_lead')
                    .set('remote_id', data.remote_id ? data.remote_id : 0)
                    .set('modified', 1)
                    .set('name', data.name)
                    .set('description', data.description ? data.description : null)
                    .set('contact_name', data.contact_name ? data.contact_name : null)
                    .set('phone', data.phone ? data.phone : null)
                    .set('mobile', data.mobile ? data.mobile : null)
                    .set('fax', data.fax ? data.fax : null)
                    .set('street', data.street ? data.street : null)
                    .set('street2', data.street2 ? data.street2 : null)
                    .set('priority', data.priority ? data.priority : 2)
                    .set('probability', data.probability ? data.probability : 10)
                    .set('type', data.type ? data.type : 'lead')
                    .set('stage_id', angular.isArray(data.stage_id) ? data.stage_id[0] : 1)
                    .set('partner_id', data.partner_id ? data.partner_id : 0)
                    .where('id = ?', data.id)
                    .toParam();
            } else {
                query = squel.insert()
                    .into('crm_lead')
                    .set('remote_id', data.remote_id ? data.remote_id : 0)
                    .set('name', data.name)
                    .set('description', data.description ? data.description : null)
                    .set('contact_name', data.contact_name ? data.contact_name : null)
                    .set('phone', data.phone ? data.phone : null)
                    .set('mobile', data.mobile ? data.mobile : null)
                    .set('fax', data.fax ? data.fax : null)
                    .set('street', data.street ? data.street : null)
                    .set('street2', data.street2 ? data.street2 : null)
                    .set('meeting_count', data.meeting_count)
                    .set('message_bounce', data.message_bounce)
                    .set('planned_cost', data.planned_cost)
                    .set('planned_revenue', data.planned_revenue)
                    .set('priority', data.priority ? data.priority : '2')
                    .set('probability', data.probability ? data.probability : 0)
                    .set('type', data.type ? data.type : 'lead')
                    .set('active', data.active ? data.active : 1)
                    .set('company_currency', angular.isArray(data.company_currency) ? data.company_currency[0] : 0)
                    .set('company_id', angular.isArray(data.company_id) ? data.company_id[0] : configuration.company_id)
                    .set('create_uid', angular.isArray(data.create_uid) ? data.create_uid[0] : configuration.id)
                    .set('stage_id', angular.isArray(data.stage_id) ? data.stage_id[0] : 1)
                    .set('user_id', angular.isArray(data.user_id) ? data.user_id[0] : configuration.id)
                    .set('user_login', data.user_login ? data.user_login : configuration.user)
                    .set('partner_id', data.partner_id ? data.partner_id : 0)
                    .toParam();
            }

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

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

            if (data.remote_id) {
                query = squel.update()
                    .table('crm_lead')
                    .set('modified', 2)
                    .set('id = ?', data.id)
                    .toParam();
            } else {
                query = squel.delete()
                    .from('crm_lead')
                    .where('id = ?', data.id)
                    .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('crm_lead')
                .where('type = ?', 'lead')
                .toParam();

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

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