import * as puppeteer from "puppeteer";
import config from "../../config";
import { Rule } from "../rule";
import Word from "../word";

export default abstract class Action {

    public word: Word;
    public rule: Rule;

    constructor(word, rule: Rule) {
        this.rule = rule;
        this.word = word;
    }

    public execute(page: puppeteer.Page): Promise<void> {
        return new Promise((resolve, reject) => {
            this.exec(page)
                .then(() => {
                    if (config.VERBOSE) {
                        console.log("\x1b[32m", "success", "\x1b[37m");
                    }
                })
                .then(() => resolve())
                .catch((e) => {
                    if (config.VERBOSE) {
                        console.log("\x1b[31m", this.word, "failed", "for rule", this.rule, e.message, "\x1b[37m");
                    }
                    reject();
                });
        });
    }

    public toWord(): Word {
        if (!this.rule) {
            return this.word;
        }
        if (this.rule.suffix && this.rule.suffix === "value" && !config.ENABLE_VALUE_SPECIFIC_SUFFIX) {
            return new Word (this.word.prefix, "smartphone");
        } else {
            return this.word;
        }

    }

    public abstract exec(page: puppeteer.Page): Promise<any>;
}
