angular.module('odoo')

.factory('opportunitiesStorageFactory', function () {

    var save = function (opportunity, success, error) {
        var values = [
                        opportunity.remote_id ? opportunity.remote_id : 0,
                        1,
                        opportunity.name ? opportunity.name : '',
                        opportunity.description ? opportunity.description : '',
                        opportunity.contact_name ? opportunity.contact_name : '',
                        opportunity.phone ? opportunity.phone : '',
                        opportunity.mobile ? opportunity.mobile : '',
                        opportunity.fax ? opportunity.fax : '',
                        opportunity.street ? opportunity.street : '',
                        opportunity.street2 ? opportunity.street2 : '',
                        opportunity.meeting_count ? opportunity.meeting_count : 0,
                        opportunity.message_bounce ? opportunity.message_bounce : 0,
                        opportunity.planned_cost ? opportunity.planned_cost : 0,
                        opportunity.planned_revenue ? opportunity.planned_revenue : 0,
                        opportunity.priority ? opportunity.priority : 2,
                        opportunity.probability ? opportunity.probability : 0,
                        (opportunity.type == undefined || opportunity.type == 'lead') ?  'opportunity' : opportunity.type,
                        opportunity.partner_id ? opportunity.partner_id : 0
                    ];

        var sql = null;

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

            sql = 'UPDATE lead SET ' +
                  'remote_id = ?,' +
                  'modified = ?,' +
                  'modifiedDate = CURRENT_TIMESTAMP,' +
                  'name = ?,' +
                  'description = ?,' +
                  'contact_name = ?,' +
                  'phone = ?,' +
                  'mobile = ?,' +
                  'fax = ?,' +
                  'street = ?,' +
                  'street2 = ?,' +
                  'meeting_count = ?,' +
                  'message_bounce = ?,' +
                  'planned_cost = ?,' +
                  'planned_revenue = ?,' +
                  'priority = ?,' +
                  'probability = ?,' +
                  'type = ?,' +
                  '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,' +
                  'partner_id) ' +
                  'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
        }

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

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

        if (opportunity.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 = 'opportunity'";

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

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