using System.Collections;
using System.Collections.Generic;
using System.Net.Http.Json;
using System.Text.Json;
using TIAM.Entities.Permissions;
using TIAM.Entities.Products;
using TIAM.Entities.TransferDestinations;
using TIAM.Models.Dtos.Products;
using TIAMWebApp.Shared.Application.Interfaces;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.ClientSide;
using TIAMWebApp.Shared.Application.Utility;

namespace TIAMWebApp.Client.Services
{
    public class PermissionService : IPermissionService
    {
        private readonly HttpClient http;
        private readonly LogToBrowserConsole logToBrowserConsole;

        public PermissionService(HttpClient http, LogToBrowserConsole logToBrowserConsole) 
        {
            this.http = http;
            this.logToBrowserConsole = logToBrowserConsole;
        }

        //1. create a permission to be assigned to users to access a context
        public Task CreatePermissionForContextByContextIdAsync(Guid contextId)
        {
            throw new NotImplementedException();
        }

        //2. get the contexts where the user has permission
        public Task<List<AssignedPermissionModel>> GetPermissionContextsByUserIdAsync(Guid userId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///this is the method that checks if there are any permissions for a context already to determine the next bit to be used in the bitmask         
        /// </summary>
        /// <param name="contextId"></param>
        /// <returns>The next bit to set</returns>
        //3. get the users who have permission for a context
        public Task<List<AssignedPermissionModel>?> GetPermissionsForContextByContextIdAsync(Guid contextId)
        {
            var url = APIUrls.GetPermissionsForContextByContextId;
            var response = http.PostAsJsonAsync(url, contextId);
            //get the dictionary from the response
            var result = response.Result.Content.ReadAsStringAsync().Result;


            //deserialize the json string to a dictionary
            List<AssignedPermissionModel>? permissions = JsonSerializer.Deserialize<List<AssignedPermissionModel>>(result);
            //return the list of permissions
            return Task.FromResult(permissions);

        }

        //4. give a user permission to access a context
        public Task AssignPermissionToUserForContextAsync(Guid contextId, Guid userId)
        {
            throw new NotImplementedException();
        }

        //5. remove a user's permission to access a context
        public Task RemovePermissionFromUserByContextIdAsync(Guid contextId, Guid permissionId, Guid userId)
        {
            throw new NotImplementedException();
        }

        //6. remove all permissions of a user for a context
        public Task RemoveAllPermissionsByContextIdAsync(Guid contextId, Guid userId)
        {
            throw new NotImplementedException();
        }

        //7. remove all permissions of a user (BAN user from all contexts, even the system)
        public Task RemoveAllPermissionsByUserIdAsync(Guid userId)
        {
            throw new NotImplementedException();
        }

        //8. create permission group
        public Task CreatePermissionGroupForContextAsync(Guid contextId, string name)
        {
            throw new NotImplementedException();
        }

        //9. add user to permission group
        public Task AddUserToPermissionGroupAsync(Guid permissionGroupId, Guid userId)
        {
            throw new NotImplementedException();
        }

        //10. create permission type
        public Task<bool> CreatePermissionTypeAsync(string name, Guid contextId)
        {
            //chech if the context already has permissiontypes

            //if it does, get the next bit value


            //if it doesn't, use 1

            //create the permissiontype

            return Task.FromResult(false);
        }

        //11. get permission types for context
        public Task<List<PermissionsType>>? GetPermissionTypesForContextByContextIdAsync(Guid contextId)
        {
            return http.GetFromJsonAsync<List<PermissionsType>>(APIUrls.GetPermissionGroupsForContextByContextId);
        }

        //12. get permission groups for context
        public Task GetPermissionGroupsForContextByContextIdAsync(Guid contextId)
        {
          throw new NotImplementedException();
        }

        

        
        
    }
}
