Ext.define("Portfolio.designer.controller.domainModelEntityFormDesignerTreeGrid", {

    extend: "Ext.app.ViewController",

    alias: "controller.designer.controller.domainModelEntityFormDesignerTreeGrid",

    onCreatePropertyField: function(i_record, o_entity) {

        var i_property = Ext.widget("textfield", {
            name: "property",
            fieldLable: ip2.i18n({
                da: "Navn",
                en: "Name"
            }),
            emptyText: ip2.i18n({
                da: "Property navn",
                en: "Property name"
            }),
            allowBlank: false,
            validator: function(s_value) {

                s_value = s_value || "";

                var a_match = s_value.match(/^\w+(?:_)*(?!_).$/g);

                return a_match ? true : ip2.i18n({
                    da: "Ikke et validt navn",
                    en: "Not a valid name"
                });
            }
        });

        var i_form = Ext.widget("form", {
            layout: "anchor",
            defaults: {
                anchor: "100%"
            },
            bodyPadding: 10,
            items: [
                i_property
            ]
        });

        var i_window = Ext.widget("window", {
            title: ip2.i18n({
                da: "Tilføj property",
                en: "Add property"
            }),
            width: 400,
            height: 130,
            modal: true,
            layout: "fit",
            items: [
                i_form
            ]
        });

        var i_buttonOk = Ext.widget("button", {
            text: ip2.i18n({
                da: "Opret",
                en: "Create"
            }),
            iconCls: "pf-add",
            enabled: false,
            handler: function() {

                var o_values = i_form.getValues();

                var o_properties = i_record.get("properties");

                var o_field = {
                    itemId: o_values.property,
                    iconCls: "ip2-designer-field",
                    leaf: true,
                    allowDrop: false,
                    allowDrag: true,
                    isProperty: true,
                    hostField: i_record.get("itemId"),
                    properties: {
                        nodeType: "field",
                        name: o_values.property,
                        title: o_values.property,
                        entityClass: o_entity.name,
                        isProperty: true,
                        hostField: i_record.get("itemId")
                    }
                };

                i_record.appendChild(o_field);

                o_properties.isHost = true;

                i_record.set({
                    leaf: false,
                    properties: o_properties
                });

                i_record.collapse();
                i_record.expand();

                i_window.close();
            }
        });

        var i_toolbar = Ext.widget("toolbar", {
            ui: "footer",
            dock: "bottom",
            items: [
                "->",
                i_buttonOk
            ]
        });

        i_window.addDocked(i_toolbar);

        i_form.on({
            afterrender: {
                fn: function(i_panel) {
                    i_panel.isValid()
                }
            },
            validitychange: {
                fn: function(i_form, b_valid) {

                    i_buttonOk[b_valid ? "enable" : "disable"]();
                }
            }
        });

        i_window.show();
    }
});
