package mara.mybox.db.table;

import mara.mybox.db.data.ColumnDefinition;
import mara.mybox.db.data.ColumnDefinition.ColumnType;
import mara.mybox.db.data.Data2DColumn;
import mara.mybox.db.data.DataNode;
import mara.mybox.value.Fxmls;
import static mara.mybox.value.Languages.message;

/**
 * @Author Mara
 * @CreateDate 2024-12-19
 * @License Apache License Version 2.0
 */
public class TableNodeDataColumn extends BaseNodeTable {

    public TableNodeDataColumn() {
        tableName = "Node_Data_Column";
        treeName = message("DataColumn");
        dataName = message("DataColumn");
        tableTitle = message("DataColumn");
        dataFxml = Fxmls.ControlDataDataColumnFxml;
        examplesFileName = "DataColumn";
        majorColumnName = "column_name";
        defineColumns();
    }

    public final TableNodeDataColumn defineColumns() {
        defineNodeColumns();
        addColumn(new ColumnDefinition("column_name", ColumnType.String)
                .setLength(StringMaxLength)
                .setLabel(message("ColumnName")));
        addColumn(new ColumnDefinition("column_type", ColumnType.String)
                .setLabel(message("Type")));
        addColumn(new ColumnDefinition("length", ColumnType.Integer)
                .setLabel(message("Length")));
        addColumn(new ColumnDefinition("width", ColumnType.Integer)
                .setLabel(message("Width")));
        addColumn(new ColumnDefinition("scale", ColumnType.Integer)
                .setLabel(message("DecimalScale")));
        addColumn(new ColumnDefinition("color", ColumnType.Color).setLength(16)
                .setLabel(message("Color")));
        addColumn(new ColumnDefinition("is_auto", ColumnType.Boolean)
                .setLabel(message("AutoGenerated")));
        addColumn(new ColumnDefinition("not_null", ColumnType.Boolean)
                .setLabel(message("NotNull")));
        addColumn(new ColumnDefinition("editable", ColumnType.Boolean)
                .setLabel(message("Editable")));
        addColumn(new ColumnDefinition("fix_year", ColumnType.Boolean)
                .setLabel(message("FixTwoDigitYears")));
        addColumn(new ColumnDefinition("century", ColumnType.Integer)
                .setLabel(message("Century")));
        addColumn(new ColumnDefinition("format", ColumnType.String)
                .setLength(StringMaxLength)
                .setLabel(message("DisplayFormat")));
        addColumn(new ColumnDefinition("default_value", ColumnType.String)
                .setLength(StringMaxLength)
                .setLabel(message("DefaultValue")));
        addColumn(new ColumnDefinition("description", ColumnType.String)
                .setLength(StringMaxLength)
                .setLabel(message("Description")));
        return this;
    }

    public static DataNode fromColumn(DataNode node, Data2DColumn column) {
        if (column == null) {
            return null;
        }
        if (node == null) {
            node = DataNode.create().setTitle(column.getColumnName());
        }
        node.setValue("column_name", column.getColumnName());
        node.setValue("column_type", ColumnDefinition.columnTypeName(column.getType()));
        node.setValue("length", column.getLength());
        node.setValue("width", column.getWidth());
        node.setValue("scale", column.getScale());
        node.setValue("color", ColumnDefinition.colorValue(column.getColor()));
        node.setValue("is_auto", column.isAuto());
        node.setValue("not_null", column.isNotNull());
        node.setValue("editable", column.isEditable());
        node.setValue("fix_year", column.isFixTwoDigitYear());
        node.setValue("century", column.getCentury());
        node.setValue("format", column.getFormat());
        node.setValue("default_value", column.getDefaultValue());
        node.setValue("description", column.getDescription());
        return node;
    }

    public static DataNode fromColumn(Data2DColumn column) {
        return fromColumn(null, column);
    }

    public static Data2DColumn toColumn(DataNode node) {
        if (node == null) {
            return null;
        }
        Data2DColumn column = new Data2DColumn();
        column.setColumnName(node.getStringValue("column_name"));
        column.setType(ColumnDefinition.columnTypeFromName(node.getStringValue("column_type")));
        column.setLength(node.getIntValue("length"));
        column.setWidth(node.getIntValue("width"));
        column.setScale(node.getIntValue("scale"));
        column.setColor(ColumnDefinition.color(node.getStringValue("color")));
        column.setAuto(node.getBooleanValue("is_auto"));
        column.setNotNull(node.getBooleanValue("not_null"));
        column.setFixTwoDigitYear(node.getBooleanValue("fix_year"));
        column.setCentury(node.getIntValue("century"));
        column.setFormat(node.getStringValue("format"));
        column.setDefaultValue(node.getStringValue("default_value"));
        column.setDescription(node.getStringValue("description"));
        return column;
    }

}
