"use strict";
const TokenRuleCondition = require('./tokenRuleCondition').TokenRuleCondition;
const TokenRuleAttCondition = require('./tokenRuleCondition').TokenRuleAttCondition;

var CalculatedTokenConditionCount = 0;


class TokenRuleConditionIsInObj extends TokenRuleAttCondition {
    constructor(ruleManager, json) {
        super(json);
        this.object = null;
        if (json.dictionary) {
            this.object = ruleManager.objectDictionnaries[json.dictionary];
            if (!this.object) {
                console.error("dictionary is not in dictionaries: " + json.dictionary);
                debugger;
                this.object = null;
            }
        }
        else{
            debugger;
        }
    }
    ContainsMandatoryEqualCondition(att, values)
    {
        if (att == this.attribute)
        {
            var keys = this.object.GetKeys();
            if (keys)
            {
                for (var key in keys)
                {
                    if (values.indexOf(key) == -1)
                    {
                        values.push(key);
                    }
                }
            }
        }
    }
    match(token, params, tokenRuleResult) {
        var status = token.conditionResults[this.id];
        if (status == undefined) {
            status = this.object.IsIn(token[this.attribute]);
            token.conditionResults[this.id] = status;
        }
        else {
            //debugger;
            CalculatedTokenConditionCount++;
        }
        return this.addOverloads(status, tokenRuleResult);
    }
}
class TokenRuleConditionIsNotInObj extends TokenRuleConditionIsInObj {
    constructor(ruleManager, json) {
        super(ruleManager, json);
    }
    match(token, params, tokenRuleResult) {
        var status = token.conditionResults[this.id];
        if (status == undefined) {
            status = this.object.IsNotIn(token[this.attribute]);
            token.conditionResults[this.id] = status;
        }
        else {
            //debugger;
            CalculatedTokenConditionCount++;
        }
        return this.addOverloads(status, tokenRuleResult);
    }
}
//
// Condition on dictionary attribute
//
class TokenRuleDicAttConditionObj extends TokenRuleCondition {
    constructor(ruleManager, json) {
        super(json);
        this.dicAttribute = json.dicAttribute;
        this.object = null;
        if (json.dictionary) {
            this.object = ruleManager.objectDictionnaries[json.dictionary];
            if (!this.object) {
                console.error("dictionary is not in dictionaries: " + json.dictionary);
                debugger;
                this.object = null;
            }
        }
        else{
            console.error("dictionary must be defined!");
            debugger;
        }

        if (json.key) {
            this.key = json.key;
        }
        else {
            debugger;
        }
    }
    match(token, params, tokenRuleResult) {
        debugger;
        return 0;
    }
    GetValues(token) {
        var key = token[this.key];
        if(key)
        {
            return this.object.GetValues(key, this.dicAttribute);
        }
        debugger;
        return [];        
    }
}
class TokenRuleDicAttConditionEqualObj extends TokenRuleDicAttConditionObj {
    constructor(ruleManager, json) {
        super(ruleManager, json);
        if (json.value != undefined) {
            this.value = json.value;
        }
        else {
            debugger;
        }
    }
    match(token, params, tokenRuleResult) {
        var status = token.conditionResults[this.id];
        if (status == undefined) {
            var values = this.GetValues(token);
            if (values.length) {
                if (this.value != undefined)
                {
                    status = values.indexOf(this.value)!=-1 ? 1 : 0;
                }
                else{
                    debugger;
                    status = 0;
                }
            }
            else {
                status = 0;
            }
            token.conditionResults[this.id] = status;
        }
        else {
            //debugger;
            CalculatedTokenConditionCount++;
        }
        return this.addOverloads(status, tokenRuleResult);
    }
}
class TokenRuleDicAttConditionNotEqualObj extends TokenRuleDicAttConditionObj {
    constructor(ruleManager, json) {
        super(ruleManager, json);
        if (json.value != undefined) {
            this.value = json.value;
        }
        else {
            debugger;
        }
    }
    match(token, params, tokenRuleResult) {
        var status = token.conditionResults[this.id];
        if (status == undefined) {
            var values = this.GetValues(token);
            if (values.length) {
                if (this.value != undefined)
                {
                    status = values.indexOf(this.value)==-1 ? 1 : 0;
                }
                else{
                    debugger;
                    status = 0;
                }
            }
            else {
                status = 1;
            }
            token.conditionResults[this.id] = status;
        }
        else {
            // debugger;
            CalculatedTokenConditionCount++;
        }
        return this.addOverloads(status, tokenRuleResult);
    }
}

class TokenRuleDicAttConditionRegExpObj extends TokenRuleDicAttConditionObj {
    constructor(ruleManager, json) {
        super(ruleManager, json);
        if (json.value != undefined) {
            this.re = new RegExp(json.value);
        }
        else {
            debugger;
        }
    }
    match(token, params, tokenRuleResult) {
        var status = token.conditionResults[this.id];
        if (status == undefined) {
            var values = this.GetValues(token);
            if (values.length) {
                status = 0;
                for (var i=0; i<values.length;i++)
                {
                    status = values[i].match(this.re);
                    if (status != undefined)
                    {
                        status = 1;
                        break;
                    }
                }
            }
            else {
                status = 0;
            }
            token.conditionResults[this.id] = status;
        }
        else {
            CalculatedTokenConditionCount++;
        }
        return this.addOverloads(status, tokenRuleResult);
    }
}
class TokenRuleDicAttConditionNotRegExpObj extends TokenRuleDicAttConditionRegExpObj {
    constructor(ruleManager, json) {
        super(ruleManager, json);
    }
    match(token, params, tokenRuleResult) {
        var status = token.conditionResults[this.id];
        if (status == undefined) {
            var values = this.GetValues(token);
            if (values.length) {
                status = 1;
                for (var i=0; i<values.length;i++)
                {
                    status = values[i].match(this.re);
                    if (status != undefined)
                    {
                        status = 0;
                        break;
                    }
                }
            }
            else {
                status = 0;
            }
            token.conditionResults[this.id] = status;
        }
        else {
            // debugger;
            CalculatedTokenConditionCount++;
        }
        return this.addOverloads(status, tokenRuleResult);
    }
}

module.exports = {
    TokenRuleConditionIsInObj: TokenRuleConditionIsInObj,
    TokenRuleConditionIsNotInObj: TokenRuleConditionIsNotInObj,
    TokenRuleDicAttConditionEqualObj: TokenRuleDicAttConditionEqualObj,
    TokenRuleDicAttConditionNotEqualObj: TokenRuleDicAttConditionNotEqualObj,
    TokenRuleDicAttConditionRegExpObj: TokenRuleDicAttConditionRegExpObj,
    TokenRuleDicAttConditionNotRegExpObj: TokenRuleDicAttConditionNotRegExpObj
};

