using Common.Library;
using GreadyPoang.DataLayer;
using GreadyPoang.EntityLayer;
using GreadyPoang.Services;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace GreadyPoang.ViewModelLayer;

public class RoundRunningViewModel : ViewModelBase
{

    public RoundRunningViewModel() : base()
    {
    }

    public RoundRunningViewModel(

        IRepository<GameRound> roundsRepo,
        IRepository<GamePoint> pointsRepo,
        IMethodSharingService<Participant> sharingService,
        ICombinedRepository combined,
        IObjectMessageService objectMessage
        ) : base()
    {
        _roundsRepo = roundsRepo;
        _pointsRepo = pointsRepo;
        _sharingService = sharingService;
        _combined = combined;
        _objectMessage = objectMessage;
        _roundElements = new ObservableCollection<RoundBuilderElement>();
        _builderObject = new();
    }

    private readonly IRepository<GameRound>? _roundsRepo;
    private readonly IRepository<GamePoint> _pointsRepo;
    private readonly IMethodSharingService<Participant> _sharingService;
    private readonly ICombinedRepository _combined;
    private readonly IObjectMessageService _objectMessage;
    private ObservableCollection<RoundBuilderElement> _roundElements;

    private ObservableCollection<RoundBuilderGroup> _GameRoundList = new();
    private ObservableCollection<Participant> _ParticipantList = new();
    private Collection<PlayerColumn> _playerColumns;

    public ObservableCollection<RoundBuilderElement> RoundElements
    {
        get { return _roundElements; }
        set
        {
            _roundElements = value;
            RaisePropertyChanged(nameof(RoundElements));
        }
    }

    public Collection<PlayerColumn> PlayerColumns
    {
        get { return _playerColumns; }
        set
        {
            _playerColumns = value;
            RaisePropertyChanged(nameof(PlayerColumns));
        }
    }



    private RoundBuilderElement _builderObject;

    public RoundBuilderElement BuilderObject
    {
        get { return _builderObject; }
        set
        {
            _builderObject = value;
            RaisePropertyChanged(nameof(BuilderObject));
        }
    }


    public ObservableCollection<RoundBuilderElement> Get()
    {
        BuilderObject.ParticipantName = "Kalle Default";

        if (_objectMessage.CurrentGroup != null)
        {
            Debug.WriteLine($"Chosen round: {_objectMessage.CurrentGroup.GameRoundId}");
            if (RoundElements.Count > 0)
            {
                RoundElements.Clear();
            }
            foreach (var item in _objectMessage.CurrentGroup.Elements)
            {
                _roundElements.Add(item);
            }
            FillupResultTable(_objectMessage.CurrentGroup.Elements);
        }
        return RoundElements;
    }

    private void FillupResultTable(List<RoundBuilderElement> elements)
    {
        Random slumper = new Random();
        if (_playerColumns != null)
        {
            _playerColumns.Clear();
        }
        else
        {
            _playerColumns = new Collection<PlayerColumn>();
        }
        foreach (var element in elements)
        {
            var player = new PlayerColumn
            {
                PlayerName = element.ParticipantName
            };

            for (int i = 0; i < slumper.Next(6); i++)
            {
                player.Values.Add(slumper.Next(1, 10).ToString());
            }

            _playerColumns.Add(player);
        }
    }

}


