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 { [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!; public ReadOnlyCollection Tags { get => tags.AsReadOnly(); private set { if (value == null || value.Count > 3) tags = new List(); else tags = value.ToList(); NotifyPropertyChanged(); } } [DataMember] private List tags; public ReadOnlyCollection Reviews => reviews.AsReadOnly(); [DataMember] private readonly List 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 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 List(c_tags); else tags = new List(); 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(); } 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 newtag) { if (newtag != null && newtag.Count<=3) tags = new List(newtag); } public void NameChange(string newname) { name = newname; } public void YearChange(int newyear) { year = newyear; } } }