namespace FeedbackGeneratorQQ.Models { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using FeedbackGeneratorQQ.Converters; using FeedbackGeneratorQQ.Utils; public class FeedbackGeneratorModel: BindableModel { /// /// Gets or sets a first name /// public string FirstName { get { return this.Get(); } set { this.Set(value); } } /// /// Gets or sets a last name /// public string LastName { get { return this.Get(); } set { this.Set(value); } } /// /// Gets or sets a gender /// public string Gender { get { return this.Get(); } set { this.Set(value); } } /// /// Gets or sets a result (feedback, error or another user message) /// public string Result { get { return this.Get(); } set { this.Set(value); } } /// /// Gets or sets list of attributes /// public ObservableCollection Attributes { get { return this.Get>(); } set { this.Set(value); } } public FeedbackGeneratorModel() { this.Attributes = new ObservableCollection(); this.Gender = "Male"; } /// /// Generates attribute sections and adds them to the given container /// /// The binder helper /// The attributes container public async void GenerateAttributes(BinderHelper binderHelper, WrapPanel container) { try { IEnumerable attributes = await AttributesHelper.GetAttributesAsync(); this.Attributes = new ObservableCollection(attributes); for (int index = 0; index < this.Attributes.Count; index++) { this.GenerateAttributeSection(binderHelper, container, index); } } catch (Exception ex) { this.Result = ex.Message; } } /// /// Generates feedback based on user selection /// /// Returns generated feedback (Task) public Task GenerateResult() { return Task.Factory.StartNew(() => { AttributesHelper.ConfigureReplacers(this.Gender); List messages = new List(); foreach (AttributeModel attribute in this.Attributes.Where(x => x.IsSelected)) { List allAttributeMessges = attribute.GetMessages(); int randomIndex = RandomGenerator.GetRandomNumber(0, allAttributeMessges.Count); string randomMessage = allAttributeMessges[randomIndex]; if (string.IsNullOrEmpty(randomMessage)) { continue; } randomMessage = AttributesHelper.ApplyReplacers(randomMessage, this.FirstName, this.LastName); if (!char.IsPunctuation(randomMessage.Last())) { randomMessage = string.Concat(randomMessage, "."); } messages.Add(randomMessage); } if (!messages.Any()) { messages.Add("No phrases found. Please select more attributes or check your Template.xml files."); } return string.Join(". ", messages.Shuffle()); }); } /// /// Generates attribute section and adds it to the given container /// /// The binding helper /// The container /// The attribute index private void GenerateAttributeSection(BinderHelper binderHelper, Panel container, int attributeIndex) { StackPanel stackPanel = ElementsGenerator.CreateDefaultAttributeContainer(); CheckBox checkbox = ElementsGenerator.CreateDefaultAttributeCheckbox(this.Attributes[attributeIndex].Name); binderHelper.AddBinding(checkbox, ToggleButton.IsCheckedProperty, $"Attributes[{attributeIndex}].IsSelected"); AttributeModel attribute = this.Attributes[attributeIndex]; ComboBox combobox = ElementsGenerator.CreateDefaultAttributeCombobox(attribute.Types); binderHelper.AddBinding(combobox, ComboBox.TextProperty, $"Attributes[{attributeIndex}].Type"); combobox.SelectedIndex = 0; BindingOperations.SetBinding(combobox, UIElement.VisibilityProperty, new Binding("IsChecked") { Converter = new CheckboxCheckedVisibilityConverter(), Source = checkbox }); stackPanel.Children.Add(checkbox); stackPanel.Children.Add(combobox); container.Children.Add(stackPanel); } } }