import State from "../domain/State";
import { querySelectorAllDeep } from 'query-selector-shadow-dom';

export default class ObjectiveView {

    init(): Promise<void> {
        return Promise.resolve();
    }

    refresh(state: State): Promise<void> {
        return this.clear().then(() => {
            state.checklist.nextActions.forEach(nextAction => {
                const elements = querySelectorAllDeep(`[aifex_${nextAction.prefix}=true]`)
                elements.forEach(element => {
                    element.setAttribute("aifex_next_action", "true")
                    element.setAttribute("aifex_next_action_value", nextAction.toString())
                })
            })
        })
    }

    clear(): Promise<void> {
        return new Promise((resolve) => {
            const dom_elements = querySelectorAllDeep("[aifex_next_action]")
            for (const dom_element of dom_elements) {
                dom_element.removeAttribute("aifex_next_action");
                dom_element.removeAttribute("aifex_next_action_value");
            }
            resolve();
        })
    }

    hide(): Promise<void> {
        return new Promise((resolve) => {
            const dom_elements = querySelectorAllDeep("[aifex_next_action]")
            for (const dom_element of dom_elements) {
                dom_element.setAttribute("aifex_next_action", "false");
            }
            resolve();
        })
    }

    show(): Promise<void> {
        return new Promise(resolve => {
            const dom_elements = querySelectorAllDeep("[aifex_next_action]");
            for (const element of dom_elements) {
                element.setAttribute("aifex_next_action", "true");
            }
            resolve();
        })
    }

}