
import { Doc } from "../../../../../fields/Doc";
import { Docs } from "../../../../documents/Documents";
import { Field } from "./FieldTypes";
import { TemplateDocInfos } from "./TemplateBackend";

export class Template {

    width: number = 0;
    height: number = 0;
    fields: Field[] = [];
    mainDoc: Doc;

    constructor(templateInfo: TemplateDocInfos) {
        this.width = templateInfo.width;
        this.height = templateInfo.height;
        this.fields = templateInfo.fields.map(settings => new Field(settings, this));
        this.mainDoc = this.setupMainDoc();
    }

    setupMainDoc = (): Doc => {
        Docs.Create.FreeformDocument(, ){

        }
        return new Doc;
    }

    get childFields(): Field[] {
        return this.fields;
    }

    get allFields(): Field[] {
        const fields = this.fields;
        this.fields.forEach(field => fields.concat(field.subfields));
        return fields;
    }

    getChildDimensions = (coords: { tl: [number, number]; br: [number, number] }): { width: number; height: number; coord: { x: number; y: number } } => {
        const l = (coords.tl[0] * this.height) / 2;
        const t = coords.tl[1] * this.width / 2; //prettier-ignore
        const r = (coords.br[0] * this.height) / 2;
        const b = coords.br[1] * this.width / 2; //prettier-ignore
        const width = r - l;
        const height = b - t;
        const coord = { x: l, y: t };
        return { width, height, coord };
    };
}