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
{
/// <summary>
/// Gets or sets a first name
/// </summary>
public string FirstName
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets a last name
/// </summary>
public string LastName
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets a gender
/// </summary>
public string Gender
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets a result (feedback, error or another user message)
/// </summary>
public string Result
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets list of attributes
/// </summary>
public ObservableCollection<AttributeModel> Attributes
{
get { return this.Get<ObservableCollection<AttributeModel>>(); }
set { this.Set(value); }
}
public FeedbackGeneratorModel()
{
this.Attributes = new ObservableCollection<AttributeModel>();
this.Gender = "Male";
}
/// <summary>
/// Generates attribute sections and adds them to the given container
/// </summary>
/// <param name="binderHelper"> The binder helper </param>
/// <param name="container"> The attributes container </param>
public async void GenerateAttributes(BinderHelper binderHelper, WrapPanel container)
{
try
{
IEnumerable<AttributeModel> attributes = await AttributesHelper.GetAttributesAsync();
this.Attributes = new ObservableCollection<AttributeModel>(attributes);
for (int index = 0; index < this.Attributes.Count; index++)
{
this.GenerateAttributeSection(binderHelper, container, index);
}
}
catch (Exception ex)
{
this.Result = ex.Message;
}
}
/// <summary>
/// Generates feedback based on user selection
/// </summary>
/// <returns> Returns generated feedback (Task) </returns>
public Task<string> GenerateResult()
{
return Task<string>.Factory.StartNew(() =>
{
AttributesHelper.ConfigureReplacers(this.Gender);
List<string> messages = new List<string>();
foreach (AttributeModel attribute in this.Attributes.Where(x => x.IsSelected))
{
List<string> 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());
});
}
/// <summary>
/// Generates attribute section and adds it to the given container
/// </summary>
/// <param name="binderHelper"> The binding helper </param>
/// <param name="container"> The container </param>
/// <param name="attributeIndex"> The attribute index </param>
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);
}
}
}