using Common.Library; using GreadyPoang.EntityLayer; using System.Collections.ObjectModel; using System.Diagnostics; namespace GreadyPoang.ViewModelLayer; public class RoundStartingViewModel : ViewModelBase { #region Constructors public RoundStartingViewModel() : base() { } public RoundStartingViewModel(IRepository repo, IRepository pointsRepo, IMethodSharingService sharingService) : base() { _Repository = repo; _pointsRepo = pointsRepo; _sharingService = sharingService; _roundElements = new ObservableCollection(); } #endregion private GameRound? _GameRoundObject = new(); private ObservableCollection _GameRoundList = new(); private ObservableCollection _ParticipantList = new(); private readonly IRepository? _Repository; private readonly IRepository _pointsRepo; private readonly IMethodSharingService _sharingService; private Participant _selectedItem; private ObservableCollection _roundElements; public ObservableCollection RoundElements { get { return _roundElements; } set { _roundElements = value; RaisePropertyChanged(nameof(RoundElements)); } } public Participant SelectedItem { get => _selectedItem; set { if (_selectedItem != value) { _selectedItem = value; RaisePropertyChanged(nameof(SelectedItem)); OnItemSelected(value); // Metod som triggas vid val } } } private void OnItemSelected(Participant item) { if (_roundElements.Count == 0) { var GameRound = new GameRound { GameRoundStartDate = DateTime.Now, GameRoundFinished = null }; var gameRoundId = _Repository?.Save(GameRound).GetAwaiter().GetResult(); if (gameRoundId != null && gameRoundId != -1) { GameRound.GameRoundId = gameRoundId.Value; } GameRoundObject = GameRound; } var GamePointStart = new GamePoint { ParticipantId = item.ParticipantId, GameRoundId = GameRoundObject?.GameRoundId ?? 0, GameDate = DateTime.Now, GameRoundRegNr = 0, GameRegPoints = 0, PointStatus = GamePointStatus.New }; var gamePointId = _pointsRepo.Save(GamePointStart).GetAwaiter().GetResult(); GamePointStart.GamePointId = gamePointId; var newElement = new RoundBuilderElement(); newElement.ParticipantId = item.ParticipantId; newElement.ParticipantName = item.LastNameFirstName; newElement.GameRoundRegNr = GamePointStart.GameRoundRegNr; newElement.GameRegPoints = GamePointStart.GameRegPoints; newElement.Status = GamePointStart.PointStatus; newElement.GameRoundStartDate = GameRoundObject?.GameRoundStartDate ?? DateTime.Now; newElement.GameRoundId = GamePointStart.GameRoundId; newElement.GamePointId = GamePointStart.GamePointId; _roundElements.Add(newElement); // Gör något med det valda objektet Debug.WriteLine($"Du valde: {item.LastNameFirstName}"); } public GameRound? GameRoundObject { get { return _GameRoundObject; } set { _GameRoundObject = value; RaisePropertyChanged(nameof(GameRoundObject)); } } public ObservableCollection GameRoundList { get { return _GameRoundList; } set { _GameRoundList = value; RaisePropertyChanged(nameof(GameRoundList)); } } public ObservableCollection ParticipantList { get { return _ParticipantList; } set { _ParticipantList = value; RaisePropertyChanged(nameof(ParticipantList)); } } #region Get Method public ObservableCollection Get() { if (_Repository != null) { var gameRoundsTask = _Repository.Get(); var gameRounds = gameRoundsTask is Task> task ? task.GetAwaiter().GetResult() : (IEnumerable)gameRoundsTask; foreach (var gameRound in gameRounds) { if (!_GameRoundList.Any(p => p.GameRoundId == gameRound.GameRoundId)) { GameRoundList.Add(gameRound); } } } return GameRoundList; } public ObservableCollection GetParticipants() { ParticipantList = _sharingService.Get(); return ParticipantList; } #endregion #region Get(id) Method public GameRound? Get(int id) { try { GameRoundObject = _Repository?.Get(id).GetAwaiter().GetResult(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}"); } return GameRoundObject; } public virtual bool Save() { if (_Repository == null || GameRoundObject == null) { return false; } var tmpTask = _Repository.Save(GameRoundObject); bool tmp = tmpTask.GetAwaiter().GetResult() != -1; if (tmp) { GameRoundObject = new GameRound(); this.Get(); } return tmp; } #endregion }