angular.module('odoo')

.factory('leadsStorageFactory', function () {

    // Save lead data to local storage
    var save = function (lead, success, error) {
        
        var values = [
                        lead.remote_id ? lead.remote_id : 0,
                        lead.name ? lead.name : '',
                        lead.description ? lead.description : '',
                        lead.contact_name ? lead.contact_name : '',
                        lead.phone ? lead.phone : '',
                        lead.mobile ? lead.mobile : '',
                        lead.fax ? lead.fax : '',
                        lead.street ? lead.street : '',
                        lead.street2 ? lead.street2 : '',
                        lead.meeting_count ? lead.meeting_count : 0,
                        lead.message_bounce ? lead.message_bounce : 0,
                        lead.planned_cost ? lead.planned_cost : 0,
                        lead.planned_revenue ? lead.planned_revenue : 0,
                        lead.priority ? lead.priority : 2,
                        lead.probability ? lead.probability : 0,
                        lead.type ? lead.type : 'lead',
                        typeof lead.stage_id == 'number' ? lead.stage_id : parseInt(lead.stage_id.split(',')[0]),
                        lead.user_id ? lead.user_id : 0,
                        lead.partner_id ? lead.partner_id : 0
                    ];

        var sql = null;

        if (lead.id) {
            values.push(lead.id);

            sql = 'UPDATE lead SET ' +
                  'remote_id = ?,' +
                  'modified = 1,' +
                  'modifiedDate = CURRENT_TIMESTAMP,' +
                  'name = ?,' +
                  'description = ?,' +
                  'contact_name = ?,' +
                  'phone = ?,' +
                  'mobile = ?,' +
                  'fax = ?,' +
                  'street = ?,' +
                  'street2 = ?,' +
                  'meeting_count = ?,' +
                  'message_bounce = ?,' +
                  'planned_cost = ?,' +
                  'planned_revenue = ?,' +
                  'priority = ?,' +
                  'probability = ?,' +
                  'type = ?,' +
                  'stage_id = ?,' +
                  'user_id = ?,' +
                  'partner_id = ? ' +
                  'WHERE id = ?';
        } else {
            sql = 'INSERT INTO lead(' +
                'remote_id,' +
                'modified,' +
                'name,' +
                'description,' +
                'contact_name,' +
                'phone,' +
                'mobile,' +
                'fax,' +
                'street,' +
                'street2,' +
                'meeting_count,' +
                'message_bounce,' +
                'planned_cost,' +
                'planned_revenue,' +
                'priority,' +
                'probability,' +
                'type,' +
                'stage_id,' +
                'user_id,' +
                'partner_id) ' +
                'VALUES(?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
        }

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

    // Remove lead from local storage
    var remove = function (lead, success, error) {
        var values = [lead.id];
        var sql = null;

        if (lead.remote_id) {
            sql = 'UPDATE lead SET modified = 2 WHERE id = ?';
        } else {
            sql = 'DELETE FROM lead WHERE id = ?'
        }

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

    var removeAll = function (success, error) {
        var sql = "DELETE FROM lead WHERE type = 'lead'";

        db.executeSql(sql, [], function(result) {
            success(result.rowsAffected);
        }, function(err) {
            error(err);
        });
    }

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