"use strict";

class Overload{
    constructor(attribute)
    {
        this.att = attribute;
    }
    static ConvertOverloads(overloads)
    {
        if (overloads.length)
        {
            return overloads;
        }
        var newOverloads = [];
        for (var object in overloads)
        {
            var newOverload = {
                type: "att",
                modifications: overloads[object]
            };
            if (object.startsWith("correction_")){
                newOverload.object = "correction";
                newOverload.correctionIndice = parseInt(object.substring(11), 10);
            }
            else{
                newOverload.object = object;
            }
            newOverloads.push(newOverload);
        }
        return newOverloads;
    }
    static Instanciate(attribute, json)
    {
        var type = Object.prototype.toString.call(json);
        if (type == "[object Object]" )
        {
            return new OverloadWithDictionaryValue(attribute,json);
        }
        else{
            return new OverloadWithValue(attribute,json);
        }
    }
    static DeleteAtt(json, att)
    {
        if (json[att])
        {
            delete json[att];
            if (json.conditionResults)
            {
                json.conditionResults = {};
            }
        }
    }
    overload(ruleManager, json)
    {
        debugger;
    }
}
module.exports = Overload;

class OverloadWithValue extends Overload{
    constructor(attribute, value)
    {
        super(attribute);
        this.value = value;
    }
    overload(ruleManager, json)
    {
        json[this.att] = this.value;
        if (json.conditionResults)
        {
            json.conditionResults = {};
        }
    }
}
class OverloadWithDictionaryValue extends Overload{
    constructor(attribute, json)
    {
        super(attribute);
        if (json.dictionary)
        {
            this.dictionary = json.dictionary;
        }
        else
        {
            debugger;
        }
        if (json.key)
        {
            this.key = json.key;
        }
        else
        {
            debugger;
        }
        if (json.attribute)
        {
            this.attribute = json.attribute;
        }
        else
        {
            debugger;
        }
    }
    overload(ruleManager, json)
    {
        var dictionary = ruleManager.dictionaries[this.dictionary];
        if (dictionary)
        {
            var data = dictionary[json[this.key]];
            if (data && data[this.attribute])
            {
                json[this.att] = data[this.attribute];
                json.conditionResults = {};
            }
            else{
                debugger;
            }
            return;
        }
        dictionary = ruleManager.objectDictionaries[this.dictionary];
        if (dictionary)
        {
            var value = dictionary.GetFirstValue(json[this.key],this.attribute);
            if (value){
                json[this.att] = value;
                if (json.conditionResults)
                {
                    json.conditionResults = {};
                }
            }
            else{
                debugger;
            }
        }
        else{
            debugger;
        }
    }
}