using economy.Helpers;

namespace economy.Models.Days
{
    public class DaysModel
    {
        private readonly DataGoKR _dataGoKR;

        public DaysModel(DataGoKR dataGoKR)
        {
            _dataGoKR = dataGoKR;
        }

        // 기념일 조회
        public async Task<Anniversary.Response> GetAnniversary(Anniversary.Request request)
        {
            Anniversary.Response parseData = new();

            try
            {
                var uriBuilder = new UriBuilder(_dataGoKR.APIUrl)
                {
                    Path = "/B090041/openapi/service/SpcdeInfoService/getAnniversaryInfo",
                    Query = $"ServiceKey={_dataGoKR.APIKey}&numOfRows=100&solYear={request.Year}"
                };

                var response = await _dataGoKR.httpClient.GetAsync(uriBuilder.Uri);

                if (response.IsSuccessStatusCode)
                {
                    var xmlString = await response.Content.ReadAsStringAsync();
                    parseData = await Common.ParseXmlDataAsync<Anniversary.Response>(xmlString);


                    if (parseData.Body is null)
                    {
                        return new Anniversary.Response();
                    }
                }

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }

            return parseData;
        }

        // 국경일 조회
        public async Task<Holiday.Response> GetHoliday(Holiday.Request request)
        {
            Holiday.Response parseData = new();

            try
            {
                var uriBuilder = new UriBuilder(_dataGoKR.APIUrl)
                {
                    Path = "/B090041/openapi/service/SpcdeInfoService/getHoliDeInfo",
                    Query = $"ServiceKey={_dataGoKR.APIKey}&numOfRows=100&solYear={request.Year}"
                };

                var response = await _dataGoKR.httpClient.GetAsync(uriBuilder.Uri);

                if (response.IsSuccessStatusCode)
                {
                    var xmlString = await response.Content.ReadAsStringAsync();
                    parseData = await Common.ParseXmlDataAsync<Holiday.Response>(xmlString);


                    if (parseData.Body is null)
                    {
                        return new Holiday.Response();
                    }
                }

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }

            return parseData;
        }

        // 기념일 조회
        public async Task<Seasonal.Response> GetSeasonal(Seasonal.Request request)
        {
            Seasonal.Response parseData = new();

            try
            {
                var uriBuilder = new UriBuilder(_dataGoKR.APIUrl)
                {
                    Path = "/B090041/openapi/service/SpcdeInfoService/get24DivisionsInfo",
                    Query = $"ServiceKey={_dataGoKR.APIKey}&numOfRows=100&solYear={request.Year}"
                };

                var response = await _dataGoKR.httpClient.GetAsync(uriBuilder.Uri);

                if (response.IsSuccessStatusCode)
                {
                    var xmlString = await response.Content.ReadAsStringAsync();
                    parseData = await Common.ParseXmlDataAsync<Seasonal.Response>(xmlString);


                    if (parseData.Body is null)
                    {
                        return new Seasonal.Response();
                    }
                }

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }

            return parseData;
        }

        // 잡절 조회
        public async Task<Sundry.Response> GetSundry(Sundry.Request request)
        {
            Sundry.Response parseData = new();

            try
            {
                var uriBuilder = new UriBuilder(_dataGoKR.APIUrl)
                {
                    Path = "/B090041/openapi/service/SpcdeInfoService/getSundryDayInfo",
                    Query = $"ServiceKey={_dataGoKR.APIKey}&numOfRows=100&solYear={request.Year}"
                };

                var response = await _dataGoKR.httpClient.GetAsync(uriBuilder.Uri);

                if (response.IsSuccessStatusCode)
                {
                    var xmlString = await response.Content.ReadAsStringAsync();
                    parseData = await Common.ParseXmlDataAsync<Sundry.Response>(xmlString);


                    if (parseData.Body is null)
                    {
                        return new Sundry.Response();
                    }
                }

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }

            return parseData;
        }
    }
}
