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 = _objectMessage.CurrentGroup.Elements[0].ParticipantName;
        BuilderObject.GameRoundId = _objectMessage.CurrentGroup.GameRoundId;

        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);
            }
            var localElements = _combined.roundBuilderElementsTotalById(_objectMessage.CurrentGroup.GameRoundId);
            FillupResultTable(localElements);
        }
        return RoundElements;
    }

    private void FillupResultTable(IEnumerable<RoundBuilderElement> elements)
    {
        if (_playerColumns != null)
        {
            _playerColumns.Clear();
        }
        else
        {
            _playerColumns = new Collection<PlayerColumn>();
        }

        if (elements.Any(g => g.GameRegPoints > 0))
        {
            var oldPart = -1;
            PlayerColumn player = new PlayerColumn();

            foreach (var element in elements)
            {
                if (element.ParticipantId != oldPart)
                {
                    player = new PlayerColumn
                    {
                        PlayerName = element.ParticipantName
                    };

                }
                if (element.GameRegPoints > 0)
                    player.Values.Add(element.GameRegPoints.ToString());

                oldPart = element.ParticipantId;

                if (!_playerColumns.Contains(player))
                {
                    _playerColumns.Add(player);
                }
            }
        }
        else
        {
            Random slumper = new Random();

            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);
            }
        }
    }

}


