public ItemLibraryWindow(CampaignSave state, bool is_selector = false, string selection = null)
 {
     this.valid        = false;
     this.need_refresh = false;
     this.dirty        = false;
     this.state        = state;
     this.item_rows    = new List <ItemLibraryItemRow>();
     this.populate_item_rows(selection);
     this.selection      = null;
     this.container_rows = new List <ItemSpecContainerRow>();
     this.property_rows  = new List <ItemSpecPropertyRow>();
     InitializeComponent();
     this.item_list.ItemsSource      = this.item_rows;
     this.cat_box.ItemsSource        = this.categories;
     this.containers                 = new List <ContainerSpec>();
     this.container_list.ItemsSource = this.container_rows;
     this.properties                 = new Dictionary <string, string>();
     this.prop_list.ItemsSource      = this.property_rows;
     if (is_selector)
     {
         this.ok_but.Visibility  = Visibility.Visible;
         this.cancel_but.Content = "Cancel";
     }
     else
     {
         this.ok_but.Visibility  = Visibility.Collapsed;
         this.cancel_but.Content = "Done";
     }
 }
        private ItemLibraryItemRow do_add_category(string name, decimal sale_value)
        {
            ItemCategory cat = new ItemCategory(name, sale_value);

            this.state.categories[name] = new ElementReference <ItemCategory>(cat);

            this.categories.Add(name);
            this.categories.Sort();
            this.cat_box.Items.Refresh();
            ItemLibraryItemRow row = new ItemLibraryItemRow(name, true);

            this.item_rows.Add(row);
            this.item_rows.Sort((x, y) => x.name.CompareTo(y.name));
            return(row);
        }
        public void populate_item_rows(string selection = null)
        {
            Dictionary <string, List <ItemLibraryItemRow> > cat_rows = new Dictionary <string, List <ItemLibraryItemRow> >();
            string selected_cat = null;

            foreach (string name in this.state.items.Keys)
            {
                string cat_name = this.state.items[name].element.category.name;
                if (!cat_rows.ContainsKey(cat_name))
                {
                    cat_rows[cat_name] = new List <ItemLibraryItemRow>();
                }
                cat_rows[cat_name].Add(new ItemLibraryItemRow(name)
                {
                    _is_selected = (name == selection)
                });
                if (name == selection)
                {
                    selected_cat = cat_name;
                }
            }
            foreach (string name in this.state.categories.Keys)
            {
                if (!cat_rows.ContainsKey(name))
                {
                    cat_rows[name] = new List <ItemLibraryItemRow>();
                }
            }

            this.categories = new List <string>(cat_rows.Keys);
            this.categories.Sort();
            foreach (string name in this.categories)
            {
                cat_rows[name].Sort((x, y) => x.name.CompareTo(y.name));
                ItemLibraryItemRow row = new ItemLibraryItemRow(name, true)
                {
                    _items = cat_rows[name], _is_expanded = (name == selected_cat)
                };
                this.item_rows.Add(row);
            }
        }
        private void remove_item(object sender, RoutedEventArgs e)
        {
            ItemLibraryItemRow sel = this.item_list.SelectedValue as ItemLibraryItemRow;

            if (sel is null)
            {
                return;
            }

            int cat_idx;

            if (sel.is_category)
            {
                if (!this.state.categories.ContainsKey(sel.name))
                {
                    return;
                }
                ElementReference <ItemCategory> cat_ref = this.state.categories[sel.name];
                if (cat_ref.ref_count > 0)
                {
                    return;
                }
                cat_idx = this.categories.IndexOf(sel.name);
                if (cat_idx < 0)
                {
                    return;
                }
                this.state.categories.Remove(sel.name);
                this.categories.RemoveAt(cat_idx);
                this.item_rows.RemoveAt(cat_idx);
                this.item_list.Items.Refresh();
                this.cat_box.Items.Refresh();
                return;
            }

            if (!this.state.items.ContainsKey(sel.name))
            {
                return;
            }
            ElementReference <ItemSpec> item_ref = this.state.items[sel.name];

            if (item_ref.ref_count > 0)
            {
                return;
            }
            cat_idx = this.categories.IndexOf(item_ref.element.category.name);
            if (cat_idx < 0)
            {
                return;
            }
            ItemLibraryItemRow cat_row = this.item_rows[cat_idx];
            int item_idx = cat_row.items.IndexOf(sel);

            if (item_idx < 0)
            {
                return;
            }
            this.state.remove_category_reference(item_ref.element);
            this.state.items.Remove(sel.name);
            cat_row.items.RemoveAt(item_idx);
            this.item_list.Items.Refresh();
        }
        private void add_item(object sender, RoutedEventArgs e)
        {
            string default_cat = null;

            if (this.selection is not null)
            {
                if (this.selection.is_category)
                {
                    default_cat = this.selection.name;
                }
                else
                {
                    ElementReference <ItemSpec> sel_ref = this.state.items[this.selection.name];
                    default_cat = sel_ref.element.category.name;
                }
            }
            QueryPrompt[] prompts = new QueryPrompt[] {
                new QueryPrompt("Name:", QueryType.STRING),
                new QueryPrompt("Category:", QueryType.LIST, default_cat, values: this.categories.ToArray(), canEdit: true),
                new QueryPrompt("Cost:", QueryType.FLOAT),
                new QueryPrompt("Weight:", QueryType.FLOAT),
            };
            object[] results = SimpleDialog.askCompound("Add Item", prompts, this);
            if (results is null)
            {
                return;
            }

            string name = results[0] as string, cat_name = results[1] as string;
            double?cost = results[2] as double?, weight = results[3] as double?;

            if ((name is null) || (this.state.items.ContainsKey(name)) || (cat_name is null) || (cost is null) || (weight is null))
            {
                return;
            }

            ItemLibraryItemRow cat_row;
            int cat_idx = this.categories.IndexOf(cat_name);

            if (cat_idx >= 0)
            {
                cat_row = this.item_rows[cat_idx];
            }
            else
            {
                double?sale_value = SimpleDialog.askFloat(cat_name, "Value Factor:", 1.0, step: 0.125, owner: this);
                if (sale_value is null)
                {
                    return;
                }
                cat_row = this.do_add_category(cat_name, (decimal)sale_value);
            }

            ItemSpec item = new ItemSpec(name, this.state.categories[cat_name].element, (decimal)cost, (decimal)weight);

            this.state.items[name] = new ElementReference <ItemSpec>(item);
            this.state.add_category_reference(item);

            ItemLibraryItemRow row = new ItemLibraryItemRow(name);

            if (!this.dirty)
            {
                if (this.selection is not null)
                {
                    this.selection._is_selected = false;
                }
                cat_row._is_expanded = true;
                row._is_selected     = true;
            }
            cat_row._items.Add(row);
            cat_row._items.Sort((x, y) => x.name.CompareTo(y.name));
            this.item_list.Items.Refresh();
        }
        private void item_list_sel_changed(object sender, RoutedEventArgs e)
        {
            this.selection = this.item_list.SelectedValue as ItemLibraryItemRow;
            if (this.selection is null)
            {
                this.item_rem_but.IsEnabled     = false;
                this.name_box.Text              = "";
                this.cat_label.Visibility       = Visibility.Collapsed;
                this.cat_box.Visibility         = Visibility.Collapsed;
                this.cost_box.Text              = "";
                this.value_label.Visibility     = Visibility.Collapsed;
                this.value_box.Visibility       = Visibility.Collapsed;
                this.weight_label.Visibility    = Visibility.Collapsed;
                this.weight_box.Visibility      = Visibility.Collapsed;
                this.cat_apply_but.Visibility   = Visibility.Collapsed;
                this.cat_revert_but.Visibility  = Visibility.Collapsed;
                this.container_group.Visibility = Visibility.Collapsed;
                this.prop_group.Visibility      = Visibility.Collapsed;
                this.item_apply_but.Visibility  = Visibility.Collapsed;
                this.item_revert_but.Visibility = Visibility.Collapsed;
                return;
            }

            this.name_box.Text = this.selection.name;

            if (this.selection.is_category)
            {
                if (!this.state.categories.ContainsKey(this.selection.name))
                {
                    return;
                }
                ElementReference <ItemCategory> cat_ref = this.state.categories[this.selection.name];
                this.item_rem_but.IsEnabled     = cat_ref.ref_count <= 0;
                this.cat_label.Visibility       = Visibility.Collapsed;
                this.cat_box.Visibility         = Visibility.Collapsed;
                this.cost_label.Content         = "Value Factor:";
                this.cost_box.SmallChange       = 0.125;
                this.cost_box.Value             = (double)(cat_ref.element.sale_value);
                this.value_label.Visibility     = Visibility.Collapsed;
                this.value_box.Visibility       = Visibility.Collapsed;
                this.weight_label.Visibility    = Visibility.Collapsed;
                this.weight_box.Visibility      = Visibility.Collapsed;
                this.cat_apply_but.IsEnabled    = false;
                this.cat_apply_but.Visibility   = Visibility.Visible;
                this.cat_revert_but.IsEnabled   = false;
                this.cat_revert_but.Visibility  = Visibility.Visible;
                this.container_group.Visibility = Visibility.Collapsed;
                this.prop_group.Visibility      = Visibility.Collapsed;
                this.item_apply_but.Visibility  = Visibility.Collapsed;
                this.item_revert_but.Visibility = Visibility.Collapsed;
                return;
            }

            if (!this.state.items.ContainsKey(this.selection.name))
            {
                return;
            }
            ElementReference <ItemSpec> item_ref = this.state.items[this.selection.name];
            bool is_unreferenced = item_ref.ref_count <= 0;

            this.item_rem_but.IsEnabled    = is_unreferenced;
            this.cat_label.Visibility      = Visibility.Visible;
            this.cat_box.SelectedValue     = item_ref.element.category.name;
            this.cat_box.Visibility        = Visibility.Visible;
            this.cost_label.Content        = "Cost:";
            this.cost_box.SmallChange      = 1;
            this.cost_box.Value            = (double)(item_ref.element.cost);
            this.value_label.Visibility    = Visibility.Visible;
            this.value_box.Value           = (double)(item_ref.element.value);
            this.value_box.Visibility      = Visibility.Visible;
            this.weight_label.Visibility   = Visibility.Visible;
            this.weight_box.Value          = (double)(item_ref.element.weight);
            this.weight_box.Visibility     = Visibility.Visible;
            this.cat_apply_but.Visibility  = Visibility.Collapsed;
            this.cat_revert_but.Visibility = Visibility.Collapsed;
            this.containers.Clear();
            this.container_rows.Clear();
            if (item_ref.element.containers is not null)
            {
                foreach (ContainerSpec cont in item_ref.element.containers)
                {
                    this.containers.Add(cont);
                    this.container_rows.Add(new ItemSpecContainerRow(cont.name, cont.weight_factor.ToString(), cont.weight_capacity.ToString()));
                }
            }
            this.container_list.Items.Refresh();
            this.container_add_but.IsEnabled  = is_unreferenced;
            this.container_edit_but.IsEnabled = is_unreferenced;
            this.container_up_but.IsEnabled   = is_unreferenced;
            this.container_down_but.IsEnabled = is_unreferenced;
            this.container_rem_but.IsEnabled  = is_unreferenced;
            this.container_group.Visibility   = Visibility.Visible;
            this.properties.Clear();
            this.property_rows.Clear();
            if (item_ref.element.properties is not null)
            {
                foreach (string prop_name in item_ref.element.properties.Keys)
                {
                    this.properties[prop_name] = item_ref.element.properties[prop_name];
                    this.property_rows.Add(new ItemSpecPropertyRow(prop_name, item_ref.element.properties[prop_name]));
                }
                this.property_rows.Sort((x, y) => x.name.CompareTo(y.name));
            }
            this.prop_list.Items.Refresh();
            this.prop_group.Visibility      = Visibility.Visible;
            this.item_apply_but.IsEnabled   = false;
            this.item_apply_but.Visibility  = Visibility.Visible;
            this.item_revert_but.IsEnabled  = false;
            this.item_revert_but.Visibility = Visibility.Visible;
        }