"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vector_1 = require("./vector");
const constants_1 = require("./constants");
class ModelField {
    constructor(parent, index, field) {
        this.parent = parent;
        this.index = index;
        this.field = field;
    }
    getFieldType() {
        const typename = this.field.type;
        const isList = this.field.isList ? '[ ]' : '';
        const isRequired = this.field.isRequired ? '' : '?';
        return typename + isRequired + isList;
    }
    draw(ctx) {
        ctx.fillStyle = "white";
        ctx.fillRect(this.parent.pos.x, this.parent.pos.y + constants_1.FIELD_HEIGHT * (this.index + 1), this.parent.width, constants_1.FIELD_HEIGHT);
        ctx.fillStyle = "black";
        ctx.textAlign = "left";
        const textY = this.parent.pos.y + (constants_1.FIELD_HEIGHT * (this.index + 1));
        ctx.beginPath();
        ctx.fillStyle = "gray";
        ctx.arc(this.parent.pos.x + 10, textY - 2.5, 3, 0, Math.PI * 2, true);
        ctx.fill();
        ctx.fillStyle = "black";
        ctx.fillText(this.field.name, this.parent.pos.x + 20, textY);
        ctx.textAlign = "right";
        ctx.fillText(this.getFieldType(), this.parent.pos.x + this.parent.width - 10, textY);
    }
}
function getMaxTextWidth(model, ctx) {
    let padding = 20;
    let max = padding + ctx.measureText(model.name).width + padding;
    model.fields.forEach(field => {
        const width = padding + ctx.measureText(field.name).width + padding + ctx.measureText(field.type).width + padding;
        if (width > max) {
            max = width;
        }
    });
    return max;
}
class ModelHeader {
    constructor(parent, model) {
        this.parent = parent;
        this.height = 20;
        this.model = model;
    }
    draw(ctx) {
        ctx.fillStyle = "white";
        ctx.fillRect(this.parent.pos.x, this.parent.pos.y, this.parent.width, this.parent.height);
        ctx.fillStyle = "#15BD76";
        ctx.fillRect(this.parent.pos.x, this.parent.pos.y, this.parent.width, this.height);
        ctx.fillStyle = "white";
        ctx.textAlign = "left";
        ctx.fillText(this.model.name, this.parent.pos.x + this.height / 2, this.parent.pos.y + 13);
    }
}
class Model {
    constructor(ctx, model, pos) {
        this.pos = pos || new vector_1.Vec();
        this.velocity = new vector_1.Vec();
        this.model = model;
        this.ctx = ctx;
        this.height = 20 + model.fields.length * 20;
        this.width = getMaxTextWidth(model, ctx);
    }
    center() {
        return new vector_1.Vec(this.pos.x + (this.width / 2), this.pos.y + (this.height / 2));
    }
    cx() {
        return this.pos.x + (this.width / 2);
    }
    cy() {
        return this.pos.y + (this.height / 2);
    }
    draw(ctx, hovering, dragging) {
        const header = new ModelHeader(this, this.model);
        header.draw(ctx);
        ctx.fillStyle = "white";
        ctx.rect(this.pos.x, this.pos.y + 20, this.width, this.height - 20);
        this.model.fields.forEach((field, i) => {
            const f = new ModelField(this, i + 1, field);
            f.draw(ctx);
        });
    }
}
exports.Model = Model;
