using DbController;
using DbController.SqlServer;
using Tabletop.Core.Filters;
using Tabletop.Core.Models;
using Tabletop.Core.Services;

namespace Tabletop.Pages.Games
{
    public partial class GameList
    {
        private int _page = 1;
        private User? _loggedInUser;

        public GameFilter Filter { get; set; } = new();

        public int Page { get => _page; set => _page = value < 1 ? 1 : value; }
        public int TotalItems { get; set; }

        public bool OpenFilter { get; set; }

        protected override async Task OnParametersSetAsync()
        {
            _loggedInUser = await authService.GetUserAsync();

            if (_loggedInUser != null)
            {
                Filter = new()
                {
                    Limit = AppdataService.PageLimit,
                    UserId = _loggedInUser.UserId
                };

                using IDbController dbController = new SqlController(AppdataService.ConnectionString);

                List<User> friends = await UserService.GetUserFriendsAsync(_loggedInUser.GetIdentifier() ?? 0, dbController); //TODO: TEST
            }

            OpenFilter = false;

            await LoadAsync();
        }

        protected override Task SaveAsync()
        {
            if (Input is not null)
            {
                AppdataService.Games[Input.GUID] = Input;

                navigationManager.NavigateTo($"/Games/Lobby/{Input.GUID}");
            }

            return Task.CompletedTask;
        }

        protected async Task JoinAsync(Game game)
        {
            if (game.Players.Count < game.NumberOfPlayers)
            {
                navigationManager.NavigateTo($"/Games/Lobby/{game.GUID}");
            }
            else
            {
                await JSRuntime.ShowToastAsync(ToastType.warning, @Localizer["FULL"]);
            }
        }

        protected Task RejoinAsync(Game game)
        {
            navigationManager.NavigateTo($"/Games/Lobby/{game.GUID}");

            return Task.CompletedTask;
        }

        protected async Task LoadAsync(bool navigateToPage1 = false)
        {
            Filter.PageNumber = navigateToPage1 ? 1 : Page;
            Data = [.. AppdataService.Games.Values];
            TotalItems = Data.Count;

            using IDbController dbController = new SqlController(AppdataService.ConnectionString);
            foreach (var game in Data)
            {
                game.Host = await userService.GetUsernameAsync(game.UserId, dbController);
            }
        }

        protected override async Task NewAsync()
        {
            if (_loggedInUser != null)
            {
                if (!AppdataService.Games.Values.Any(Input => Input.UserId == _loggedInUser?.UserId))
                {
                    Input = new Game
                    {
                        UserId = _loggedInUser.UserId,
                        GamemodeId = 1,
                        NumberOfRounds = 8,
                        NumberOfTeams = 2,
                        NumberOfPlayers = 2,
                        Force = 400,
                        SurfaceId = 1,
                        LayoutId = 3,
                        GUID = Guid.NewGuid().ToString()
                    };
                }
                else
                {
                    Input = null;
                    await JSRuntime.ShowToastAsync(ToastType.warning, @Localizer["ERROR"]);
                }
            }
        }

        protected async Task RefreshPageAsync()
        {
            await LoadAsync();
            await InvokeAsync(StateHasChanged);
        }
    }
}