You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.6 KiB

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
namespace Model
{
[DataContract]
public sealed class Game : INotifyPropertyChanged, IEquatable<Game>
{
[DataMember]
public string Name
{
get => name;
private set
{
if (string.IsNullOrWhiteSpace(value)) name = "Default";
else name = value;
NotifyPropertyChanged();
}
}
private string name = default!;
[DataMember]
public string Description
{
get => description;
private set
{
if (string.IsNullOrWhiteSpace(value)) description = "Defaut";
else description = value;
NotifyPropertyChanged();
}
}
private string description = default!;
[DataMember]
public int Year
{
get => year;
private set
{
if (value < 1957 || value > 2023) year = 2023;
else year = value;
NotifyPropertyChanged();
}
}
private int year = default!;
[DataMember]
public string Cover
{
get => cover;
private set
{
if (string.IsNullOrWhiteSpace(value)) cover = "no_cover.png";
else cover = value;
NotifyPropertyChanged();
}
}
private string cover = default!;
[DataMember]
public ObservableCollection<string> Tags
{
get => tags;
private set
{
if (value == null || value.Count > 3) tags = new ObservableCollection<string>();
else tags = value;
NotifyPropertyChanged();
}
}
private ObservableCollection<string> tags;
public ReadOnlyCollection<Review> Reviews => reviews.AsReadOnly();
[DataMember]
private readonly List<Review> reviews;
public double Average => Reviews.Any() ? Math.Round(Reviews.Select(review => review.Rate).Average(), 1) : 0;
[DataMember]
public string Lien {
get => lien;
private set
{
if (string.IsNullOrWhiteSpace(value)) lien = "Pas de lien";
else lien = value;
NotifyPropertyChanged();
}
}
private string lien = default!;
public Game(string name, string description, int year, List<string> c_tags, string cover, string c_lien)
{
if (string.IsNullOrWhiteSpace(name)) Name = "Default";
else Name = name;
if (string.IsNullOrWhiteSpace(description)) Description = "Pas de description";
else Description = description;
Year = year;
if (c_tags is not null) tags = new ObservableCollection<string>(c_tags);
else tags = new ObservableCollection<string>();
if (string.IsNullOrWhiteSpace(cover)) Cover = "no_cover.png";
else Cover = cover;
if (string.IsNullOrWhiteSpace(c_lien)) Lien = "Pas de lien";
else Lien = c_lien;
reviews = new List<Review>();
}
public event PropertyChangedEventHandler? PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public override int GetHashCode()
{
if (string.IsNullOrWhiteSpace(Name)) return 0;
return Name.GetHashCode();
}
public override bool Equals(object? obj)
{
if (object.ReferenceEquals(obj, null)) return false;
if (object.ReferenceEquals(this, obj)) return true;
if (this.GetType() != obj.GetType()) return false;
return this.Equals(obj as Game);
}
public bool Equals(Game? other)
{
if (string.IsNullOrWhiteSpace(Name)) return false;
return other != null && Name.Equals(other.Name);
}
public override string ToString()
{
StringBuilder builder = new();
builder.Append($"{Name} : {Description} : {Year} : {Cover} : {lien}\n");
foreach (Review review in Reviews)
{
builder.Append($"{review}\n");
}
return builder.ToString();
}
public void AddReview(Review review)
{
reviews.Add(review);
UpdateReviews();
}
public void RemoveReview(Review review)
{
reviews.Remove(review);
UpdateReviews();
}
public void UpdateReviews()
{
NotifyPropertyChanged(nameof(Reviews));
NotifyPropertyChanged(nameof(Average));
}
public void DescChange(string newdesc)
{
description = newdesc;
}
public void TagChange(List<string> newtag)
{
if (newtag != null && newtag.Count<=3) tags = new ObservableCollection<string>(newtag);
}
public void NameChange(string newname)
{
name = newname;
}
public void YearChange(int newyear)
{
year = newyear;
}
}
}