using Microsoft.AspNetCore.Components;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net.Http.Json;
using BlazorBootstrap;
using System.Net;
using System.Text;

namespace Front.Services
{
    public class HttpClientController
    {
        public readonly HttpClient _hc;
        public readonly NavigationManager _nav;
        public HttpClientController(HttpClient hc, NavigationManager nav)
        {
            _hc = hc;
            _nav = nav;
        }
        public async Task<HttpResponseMessage> Get(string route) 
        {
           var request = await _hc.GetAsync(route);
            if (request.StatusCode==System.Net.HttpStatusCode.Unauthorized)
                _nav.NavigateTo("/Sign-in/unon");
            return request;
        }
        public async Task<HttpResponseMessage> Delete(string route)
        {
            var request = await _hc.DeleteAsync(route);
            if (request.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                _nav.NavigateTo("/Sign-in/unon");
            return request;
        }
        public async Task<HttpResponseMessage> Post<T>(string route,T mode)
        {
            var request = await _hc.PostAsJsonAsync(route,mode);
            if (request.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                _nav.NavigateTo("/Sign-in/unon");
            return request;
        }
        public async Task<HttpResponseMessage> Post(string route)
        {
            var jsonString = "{\"appid\":1,\"platformid\":1,\"rating\":3}";
            var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var request = await _hc.PostAsync(route, httpContent);
            if (request.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                _nav.NavigateTo("/Sign-in/unon");
            return request;
        }
        public async Task<HttpResponseMessage> Put<T>(string route, T mode)
        {
            var request = await _hc.PutAsJsonAsync(route, mode);
            if (request.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                _nav.NavigateTo("/Sign-in/unon");
            return request;
        }
        public async Task<HttpResponseMessage> Put(string route)
        {
            var jsonString = "{\"appid\":1,\"platformid\":1,\"rating\":3}";
            var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var response = await _hc.PutAsync(route, httpContent);
            if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                _nav.NavigateTo("/Sign-in/unon");
            return response;

           
           
        }
    }
}
