public static void AddMaterials(this ModelEnergyProperties modelEnergyCollection, List <IMaterial> materials)
 {
     if (materials == null)
     {
         return;
     }
     foreach (var item in materials)
     {
         modelEnergyCollection.AddMaterial(item);
     }
 }
        public Dialog_Construction(HB.ModelEnergyProperties libSource, HB.Energy.IConstruction construction, bool lockedMode = false)
        {
            try
            {
                this.ModelEnergyProperties = libSource;

                _hbObj = construction;

                Padding     = new Padding(5);
                Resizable   = true;
                Title       = $"Construction - {DialogHelper.PluginName}";
                WindowStyle = WindowStyle.Default;
                MinimumSize = new Size(650, 500);
                this.Icon   = DialogHelper.HoneybeeIcon;

                var OkButton = new Button {
                    Text = "OK", Enabled = !lockedMode
                };
                OkButton.Click += (sender, e) =>
                {
                    // add new material to library source
                    foreach (var layer in _layers)
                    {
                        var m = OpaqueMaterials.FirstOrDefault(_ => _.Identifier == layer);
                        m = m ?? WindowMaterials.FirstOrDefault(_ => _.Identifier == layer);
                        var dup = m.Duplicate() as HB.Energy.IMaterial;
                        dup.DisplayName = m.DisplayName ?? m.Identifier;
                        libSource.AddMaterial(dup);
                    }
                    OkCommand.Execute(_hbObj);
                };

                AbortButton = new Button {
                    Text = "Cancel"
                };
                AbortButton.Click += (sender, e) => Close();

                //var panel = new Panel_Schedule(sch);
                //panel.AddSeparateRow(null, OkButton, AbortButton, null);
                //Content = panel;

                var leftLayout = new DynamicLayout();
                leftLayout.DefaultSpacing = new Size(5, 5);
                leftLayout.DefaultPadding = new Padding(5);
                leftLayout.Height         = 600;

                leftLayout.AddRow("Name");
                var name = new TextBox();
                _hbObj.DisplayName = _hbObj.DisplayName ?? _hbObj.Identifier;
                name.TextBinding.Bind(() => _hbObj.DisplayName, v => _hbObj.DisplayName = v);
                leftLayout.AddRow(name);



                var groupContent = new DynamicLayout();
                groupContent.DefaultSpacing = new Size(5, 5);
                Action <int, string> actionWhenItemChanged = (int layerIndex, string newValue) => {
                    _layers.RemoveAt(layerIndex);
                    _layers.Insert(layerIndex, newValue);
                    CalRValue(_hbObj, _showIP);
                };

                _layersPanel = new DynamicLayout();
                _layersPanel.DefaultSpacing = new Size(5, 5);
                GenMaterialLayersPanel(_layersPanel, actionWhenItemChanged);
                var fromLibDropin = GenAddMoreDropInArea(
                    (newValue) =>
                {
                    var newIndex = _layers.Count;
                    _layers.Add(newValue);

                    GenMaterialLayersPanel(_layersPanel, actionWhenItemChanged);
                    _layersPanel.Create();
                    CalRValue(_hbObj, _showIP);
                }
                    );
                groupContent.AddSeparateRow(null, "Outside", null);
                groupContent.AddRow(_layersPanel);
                groupContent.AddRow(fromLibDropin);
                groupContent.AddSeparateRow(null, "Inside", null);
                //group.Content = groupContent;
                leftLayout.AddRow(groupContent);
                leftLayout.AddRow(null);
                //leftLayout.AddAutoSized(new Button());

                //leftLayout.AddSeparateRow(new Button());
                // Thermal properties
                var thermGp = new GroupBox()
                {
                    Text = "Construction Thermal Properties"
                };
                var thermProp = new DynamicLayout()
                {
                    DefaultPadding = new Padding(4)
                };
                //thermProp.AddSeparateRow("Construction Thermal Properties", null);
                _r_label = new Label()
                {
                    Text = _r_si
                };
                _u_label = new Label()
                {
                    Text = _u_si
                };
                _uf_label = new Label()
                {
                    Text = _uf_si
                };
                _r_value = new Label()
                {
                    Text = "Not available"
                };
                _u_value = new Label()
                {
                    Text = "Not available"
                };
                _uf_value = new Label()
                {
                    Text = "Not available"
                };
                thermProp.AddSeparateRow(_r_label, ":", _r_value);
                thermProp.AddSeparateRow(_u_label, ":", _u_value);
                thermProp.AddSeparateRow(_uf_label, ":", _uf_value);
                CalRValue(_hbObj, false);

                // unit switchs
                var unit = new RadioButtonList();
                unit.Items.Add("Metric");
                unit.Items.Add("Imperial");
                unit.SelectedIndex         = 0;
                unit.Spacing               = new Size(5, 0);
                unit.SelectedIndexChanged += (s, e) => CalRValue(_hbObj, unit.SelectedIndex == 1);
                thermProp.AddSeparateRow("Unit:", unit);
                thermGp.Content = thermProp;
                leftLayout.AddRow(thermGp);
                leftLayout.AddRow(null);

                var buttonSource = new Button {
                    Text = "Schema Data"
                };
                buttonSource.Click += (s, e) =>
                {
                    Dialog_Message.Show(this, _hbObj.ToJson(true));
                };


                //leftLayout.AddRow(null);

                //Right panel
                var rightGroup = new GroupBox();
                rightGroup.Text = "Library";
                var groupPanel = new DynamicLayout();


                var materialType = new DropDown();
                materialType.Items.Add(new ListItem()
                {
                    Key = "Opaque", Text = "Opaque Material"
                });
                materialType.Items.Add(new ListItem()
                {
                    Key = "Window", Text = "Window Material"
                });
                //constructionTypes.Items.Add(new ListItem() { Key = "Shade Material" });
                //constructionTypes.Items.Add(new ListItem() { Key = "AirBoundary Material" });
                materialType.SelectedIndex = 0;
                groupPanel.AddRow(materialType);

                //Search tbox
                var searchTBox = new TextBox()
                {
                    PlaceholderText = "Search"
                };
                groupPanel.AddRow(searchTBox);

                var allMaterials = OpaqueMaterials;

                // Library
                var lib = new GridView();
                lib.Height = 400;
                groupPanel.AddRow(lib);

                lib.ShowHeader = false;
                var nameCol = new GridColumn()
                {
                    DataCell = new TextBoxCell(0)
                };
                lib.Columns.Add(nameCol);

                // Library items
                lib.DataStore = allMaterials;


                var idCell = new TextBoxCell
                {
                    Binding = Binding.Delegate <HB.Energy.IIDdEnergyBaseModel, string>(r => r.DisplayName ?? r.Identifier)
                };
                lib.Columns.Add(new GridColumn()
                {
                    DataCell = idCell
                });



                // material details
                var detailPanel    = new DynamicLayout();
                var materialDetail = new TextArea();
                materialDetail.Height = 150;
                materialDetail.Text   = "Material Details";
                //groupPanel.AddRow(materialDetail);

                var rightSplit = new Splitter();
                rightSplit.Panel1            = groupPanel;
                rightSplit.Panel2            = materialDetail;
                rightSplit.Panel1MinimumSize = 300;
                rightSplit.Orientation       = Orientation.Vertical;

                rightGroup.Content = rightSplit;


                materialType.SelectedIndexChanged += (sender, e) =>
                {
                    var selectedType = materialType.SelectedKey;
                    allMaterials    = selectedType == "Window" ? this.WindowMaterials : this.OpaqueMaterials;
                    searchTBox.Text = null;
                    //lib.Items.Clear();

                    lib.DataStore = allMaterials;
                    //var filteredItems = allMaterials.Select(_ => new ListItem() { Text = _.Identifier, Key = _.Identifier, Tag = _ });
                    //lib.Items.AddRange(filteredItems);
                };



                //// need this to make drag drop work on Mac
                //lib.MouseDown += (sender, e) => {
                //    _primMousePressed = e.Buttons == MouseButtons.Primary;
                //};
                //lib.MouseUp += (sender, e) => {
                //    _primMousePressed = false;

                //};

                lib.MouseMove += (sender, e) =>
                {
                    if (e.Buttons != MouseButtons.Primary)
                    {
                        return;
                    }
                    if (lib.SelectedItem == null)
                    {
                        return;
                    }

                    var dragableArea = lib.Bounds;
                    dragableArea.Width  -= 20;
                    dragableArea.Height -= 20;
                    var iscontained = e.Location.Y < dragableArea.Height && e.Location.X < dragableArea.Width;
                    //name.Text = $"{dragableArea.Width}x{dragableArea.Height}, {new Point(e.Location).X}:{new Point(e.Location).Y}, {dragableArea.Contains(new Point(e.Location))}";
                    if (!iscontained)
                    {
                        return;
                    }


                    var cell = lib.GetCellAt(e.Location);
                    if (cell.RowIndex == -1 || cell.ColumnIndex == -1)
                    {
                        return;
                    }

                    var selected = (lib.SelectedItem as HB.Energy.IMaterial).Identifier;
                    var data     = new DataObject();
                    data.SetString(selected, "HBObj");
                    lib.DoDragDrop(data, DragEffects.Move);
                    e.Handled = true;
                };

                lib.SelectedItemsChanged += (s, e) =>
                {
                    //Clear preview first
                    materialDetail.Text = null;

                    //Check current selected item from library
                    var selItem = lib.SelectedItem as HB.HoneybeeObject;
                    if (selItem == null)
                    {
                        return;
                    }

                    //Update Preview
                    materialDetail.Text = selItem.ToString(true);
                };


                searchTBox.TextChanged += (sender, e) =>
                {
                    var input = searchTBox.Text;
                    materialDetail.Text = null;

                    if (string.IsNullOrWhiteSpace(input))
                    {
                        lib.DataStore = allMaterials;
                        return;
                    }
                    var regexPatten = ".*" + input.Replace(" ", "(.*)") + ".*";
                    var filtered    = allMaterials.Where(_ => Regex.IsMatch(_.Identifier, regexPatten, RegexOptions.IgnoreCase) || (_.DisplayName != null ? Regex.IsMatch(_.DisplayName, regexPatten, RegexOptions.IgnoreCase) : false));

                    lib.DataStore = filtered;
                };



                //var split = new Splitter();
                //split.Orientation = Orientation.Horizontal;
                //split.Panel1 = leftLayout;
                //split.Panel2 = rightGroup;

                var locked = new CheckBox()
                {
                    Text = "Locked", Enabled = false
                };
                locked.Checked = lockedMode;

                var layout = new DynamicLayout();
                layout.DefaultSpacing = new Size(5, 5);
                layout.DefaultPadding = new Padding(5);
                layout.AddSeparateRow(leftLayout, rightGroup);
                layout.AddSeparateRow(locked, null, OkButton, AbortButton, null, buttonSource);
                layout.AddRow(null);

                //Create layout
                Content = layout;
            }
            catch (Exception e)
            {
                throw e;
            }
        }