import Rule from "./Rule";
import Action from "./Action";
import RuleMapper from "./RuleMapper";
import IndexRule from "./IndexRule";
import InnerTextRule from "./InnerTextRule";
import ValueRule from "./ValueRule";
import SimpleRule from "./SimpleRule";
import CSSSelectorRule from "./CSSSelectorRule";

export default class RuleService {
    elementRules: Map<Element, Rule[]>;
    private _ruleMapper : RuleMapper;

    constructor() {
        this.elementRules = new Map();
        this._ruleMapper = new RuleMapper([]);
    }

    getEventsToHandle(): string[] {
        return this._ruleMapper.eventInRules;
    }

    loadRules(rules : Rule[]) {
        this._ruleMapper = new RuleMapper(rules);
    }

    attachRulesToElements() {
        this.elementRules = this._ruleMapper.buildElementToRuleMap();
    }

    getMatchingRule(event): Rule | void {
        //console.log('getMatchingRule', event);
        for (const element of event.composedPath()) {
            //console.log('in',element);
            if (this.elementRules.has(element)) {
                //console.log('has');
                const rules = this.elementRules.get(element);
                const matchingRule = rules.find((rule) => rule.match(event))
                if (matchingRule) {
                    return matchingRule;
                }
            }
        }
        return;
    }

    actionToElements(action: Action) : Element[] {
        let elements : Set<Element> = new Set();
        const rules = this._ruleMapper.getRulesByPrefix(action.prefix);
        rules.forEach(rule => {
            const elementListForRule = rule.actionToElements(action);
            elementListForRule.forEach(element => elements.add(element));
        });
        return [...elements.values()];
    }

    createRule(data) : Rule {
        switch (data.output.suffix) {
            case "innerText":
                return new InnerTextRule(data.output.prefix,data.output.suffix, data.match.event, data.match?.css, data.match?.xpath, data.match?.code, data.match?.key, data?.context?.url, data?.context?.css, data?.context?.xpath);
            case "index":
                return new IndexRule(data.output.prefix,data.output.suffix, data.match.event, data.match?.css, data.match?.xpath, data.match?.code, data.match?.key, data?.context?.url, data?.context?.css, data?.context?.xpath);
            case "value":
                return new ValueRule(data.output.prefix,data.output.suffix, data.match.event, data.match?.css, data.match?.xpath, data.match?.code, data.match?.key, data?.context?.url, data?.context?.css, data?.context?.xpath);
            case "cssSelector":
                return new CSSSelectorRule(data.output.prefix,data.output.suffix, data.match.event, data.match?.css, data.match?.xpath, data.match?.code, data.match?.key, data?.context?.url, data?.context?.css, data?.context?.xpath);
            default:
                return new SimpleRule(data.output.prefix,data.output.suffix, data.match.event, data.match?.css, data.match?.xpath, data.match?.code, data.match?.key, data?.context?.url, data?.context?.css, data?.context?.xpath);
        }

    }


}