angular.module('odoo')

.factory('leadsStorageFactory', function () {

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

        if (data.id) {
            query = squel.update()
                .table('crm_lead')
                .set('remote_id', data.remote_id)
                .set('name', data.name)
                .set('description', data.description)
                .set('contact_name', data.contact_name)
                .set('phone', data.phone)
                .set('mobile', data.mobile)
                .set('fax', data.fax)
                .set('street', data.street)
                .set('street2', data.street2)
                .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)
                .set('probability', data.probability)
                .set('type', data.type)
                .set('stage_id', typeof data.stage_id == 'object' ? data.stage_id[0] : data.stage_id)
                .set('user_id', data.user_id)
                .set('partner_id', data.partner_id)
                .where('id', data.id)
                .toParam();
        } else {
            query = squel.insert()
                .into('crm_lead')
                .set('remote_id', data.remote_id)
                .set('modified', data.modified)
                .set('name', data.name)
                .set('description', data.description)
                .set('contact_name', data.contact_name)
                .set('phone', data.phone)
                .set('mobile', data.mobile)
                .set('fax', data.fax)
                .set('street', data.street)
                .set('street2', data.street2)
                .set('meeting_count', data.meeting_count)
                .set('message_bounce', data.message_bounce)
                .set('planned_cost', data.planned_cost)
                .set('planned_revenue', data.street)
                .set('priority', data.priority)
                .set('probability', data.probability)
                .set('type', data.type)
                .set('stage_id', typeof data.stage_id == 'object' ? data.stage_id[0] : data.stage_id)
                .set('user_id', data.user_id)
                .set('partner_id', data.partner_id)
                .toParam();
        }
       
        db.executeSql(query.text, query.values, function (result) {
            console.log(result);
            success(query.text.startsWith('INSERT') ? result.insertId : data.id);
        }, function (err) {
            console.log(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);
        } else {
            query = squel.delete()
                from('crm_lead')
                    .where('id', data.id);
        }

        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('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
    }
});
