using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Front.Program.Services;

public class QueryParamService
{
    public Dictionary<string, string> ParseQueryParam(System.Uri uri)
    {
        var result = new Dictionary<string, string>();
        if (!string.IsNullOrEmpty(uri.Query))
        {
            var query = uri.Query.TrimStart('?');
            var parameters = query.Split('&')
                .Select(p => p.Split('='))
                .Where(p => p.Length == 2)
                .ToDictionary(p => p[0], p => p[1]);
            result = parameters;
        }

        return result;
    }

    public async Task AuthCheck(Dictionary<string, string> parameters, StorageService storageService)
    {
        if (parameters.TryGetValue("auth", out var auth))
        {
            LoggerService.Write($"auth 파라미터 값: {auth}");
            if (auth == "true")
            {
                await storageService.SetItemAsync("IsLogin", "true");
                LoggerService.Write("로그인 상태를 true로 설정했습니다.");
            }
            else
            {
                await storageService.RemoveItemAsync("IsLogin");
                LoggerService.Write("로그인 상태를 제거했습니다.");
                
            }
        }
    }
}