import IndexStrategy from "./ActionStrategy/IndexStrategy";
import SimpleStrategy from "./ActionStrategy/SimpleStrategy";
import InnerTextStrategy from "./ActionStrategy/InnerTextStrategy";
import ValueStrategy from "./ActionStrategy/ValueStrategy";
import Action from "../Action";
import ActionStrategy from "./ActionStrategy/ActionStrategy";

export default class Rule {

    event: string;
    css: string;
    xpath: string;
    prefix: string;
    suffix: string;
    key: string;
    code: string;
    strategy;

    constructor(prefix, suffix, event, css, xpath, code, key) {
        this.event = event;
        this.code = code;
        this.css = css;
        this.xpath = xpath;
        this.prefix = prefix;
        this.suffix = suffix;
        this.key = key;

        switch(suffix) {
            case "index":
                this.strategy = IndexStrategy;
                break;
            case "value":
                this.strategy = ValueStrategy;
                break;
            case "innerText":
                this.strategy = InnerTextStrategy;
                break;
            case "none":
                this.strategy = SimpleStrategy;
                break;
            default:
                this.strategy = SimpleStrategy; 
        }
    }

    match(event) {
        if (event.type !== this.event) {
            return false;
        } if (this.event === "keyup" || this.event === "keydown" || this.event === "keypress") {
            if (this.code && event.code !== this.code) {
                return false;
            }
            if (this.key && event.key !== this.key) {
                return false;
            }
        }
        return true;
    }

    makeAction(event, listenedElement): Action {
        console.log('make action CSS:',this.css);
        console.log('make action Xpath:',this.css)
        return this.strategy.makeAction(this, event, listenedElement)
    }

    actionToElements(action): Element[] {
        return this.strategy.actionToElements(this, action);
    } 

}