/src/Toolbox.js
import Controller from './Controller';
import Pen from './Pen';
import Fill from './Fill';
import Line from './Line';
import Rect from './Rect';
import Snow from './Snow';

export const TOOL_PEN = 0;
export const TOOL_FILL = 1;
export const TOOL_LINE = 2;
export const TOOL_RECT = 3;
export const TOOL_SNOW = 4;
export const TOOL_COUNT = 5;

// And also insert them into the window object for use in the UI
window.TOOL_PEN = TOOL_PEN;
window.TOOL_FILL = TOOL_FILL;
window.TOOL_LINE = TOOL_LINE;
window.TOOL_RECT = TOOL_RECT;
window.TOOL_SNOW = TOOL_SNOW;

export default class Toolbox extends Controller {
    constructor(appState) {
        super(appState);
        
        this.tools = [];
        this.elems = [];

        this.tools[TOOL_PEN] = new Pen(appState);
        this.elems[TOOL_PEN] = document.getElementById('pen');

        this.tools[TOOL_FILL] = new Fill(appState);
        this.elems[TOOL_FILL] = document.getElementById('fill');

        this.tools[TOOL_LINE] = new Line(appState);
        this.elems[TOOL_LINE] = document.getElementById('line');

        this.tools[TOOL_RECT] = new Rect(appState);
        this.elems[TOOL_RECT] = document.getElementById('rect');

        this.tools[TOOL_SNOW] = new Snow(appState);
        this.elems[TOOL_SNOW] = document.getElementById('snow');

        this.selectTool(TOOL_PEN);
    }

    selectTool(t) {
        if (t < 0 || t >= TOOL_COUNT) {
            throw Error("Invalid tool: " + t);
        }
        if (this.currentTool != undefined) {
            this.elems[this.currentTool].className = null;
        }
        this.currentTool = t;
        this.elems[this.currentTool].className = 'active';
    }
    getTool(t = this.currentTool) {
        if (t < 0 || t >= TOOL_COUNT) {
            throw Error("Invalid tool: " + t);
        }
        return this.tools[t];
    }
}