using Microsoft.AspNetCore.Components.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.share;
using wispro.sp.web.Auth;
using static System.Net.WebRequestMethods;

namespace wispro.sp.web.Services
{
    public class StaffGradeService
    {
        private readonly IHttpService _httpClient;
        private readonly JwtAuthenticationStateProvider _jwt;

        public StaffGradeService(IHttpService httpClient, AuthenticationStateProvider jwt)
        {
            _httpClient = httpClient;
            _jwt = (JwtAuthenticationStateProvider)jwt;
        }
        
        public async Task<List<StaffGrade>> GetAll()
        {
            try
            {
                var _StaffGrade = await _httpClient.Get<List<StaffGrade>>($"StaffGrade/GetAll");
                return _StaffGrade;
            }
            catch(Exception ex)
            {
                if (ex.Message.Contains("Unauthorized"))
                {
                    _jwt.NotifyUserLogOut();
                }
                
                return null;
                
            }
        }

        public async Task<ApiSaveResponse> Save(StaffGrade staffGrade)
        {
            try
            {
                var data = await _httpClient.Post<ApiSaveResponse>($"StaffGrade/Save", staffGrade);
                return data;
            }
            catch(Exception ex)
            {
                return new ApiSaveResponse() { Success = false, ErrorMessage = $"请求发生错误!\r\n {ex.Message}" };
            }
            
        }
    }
}
