using LAPS_XMLQC_Service.App_Data;
using LAPS_XMLQC_Service.Models.Configuration;
using Newtonsoft.Json;
using System;

public class GrantFolderPermission : IDisposable
{
    private readonly CommonRepository _commonRepository;
    private readonly IImpartunateFactory _impartunateFactory;
    private Impartunate _impartunate;

    public GrantFolderPermission(CommonRepository commonRepository, IImpartunateFactory impartunateFactory)
    {
        _commonRepository = commonRepository ?? throw new ArgumentNullException(nameof(commonRepository));
        _impartunateFactory = impartunateFactory ?? throw new ArgumentNullException(nameof(impartunateFactory));
    }
    public void FolderPermission_vlex(string path, string pdefid, string transactionId = "", string lotId = "", string jobId = "")
    {
        if (string.IsNullOrWhiteSpace(pdefid) && string.IsNullOrWhiteSpace(transactionId) &&
            string.IsNullOrWhiteSpace(lotId) && string.IsNullOrWhiteSpace(jobId))
        {
            throw new ArgumentException("At least one parameter must be provided.");
        }

        string option = GetOption(pdefid, transactionId, lotId, jobId, out var data);
        string inputJson = JsonConvert.SerializeObject(data);
        lotmasterModel ds = _commonRepository.ExecuteSP_ReturnListForLot("spusrserverpathinfo", inputJson, option);

        if (ds != null)
        {
            // Example: @"DOMAIN\username"
            string user = string.IsNullOrWhiteSpace(ds.domain)
                ? ds.username
                : $@"{ds.domain}\{ds.username}";

            NetworkShareUtility.ConnectToShare(path, user, ds.password);
            ConfigureImpartunate(ds);
        }
    }
    public void FolderPermission(string pdefid, string transactionId = "", string lotId = "", string jobId = "")
    {
        if (string.IsNullOrWhiteSpace(pdefid) && string.IsNullOrWhiteSpace(transactionId) &&
            string.IsNullOrWhiteSpace(lotId) && string.IsNullOrWhiteSpace(jobId))
        {
            throw new ArgumentException("At least one parameter must be provided.");
        }

        string option = GetOption(pdefid, transactionId, lotId, jobId, out var data);
        string inputJson = JsonConvert.SerializeObject(data);
        lotmasterModel ds = _commonRepository.ExecuteSP_ReturnListForLot("spusrserverpathinfo", inputJson, option);

        if (ds != null)
        {
            ConfigureImpartunate(ds);
        }

   
}

    private static string GetOption(string pdefid, string transactionId, string lotId, string jobId, out object data)
    {
        if (!string.IsNullOrWhiteSpace(transactionId))
        {
            data = new { transactionid = transactionId, lotid = 0, option = "logininfo" };
            return "logininfo";
        }

        if (!string.IsNullOrWhiteSpace(pdefid))
        {
            data = new { transactionid = pdefid, lotid = 0, option = "serverinfo_projid" };
            return "serverinfo_projid";
        }

        if (!string.IsNullOrWhiteSpace(lotId))
        {
            data = new { transactionid = 0, lotid = lotId, option = "loginlotinfo" };
            return "loginlotinfo";
        }

        if (!string.IsNullOrWhiteSpace(jobId))
        {
            data = new { transactionid = jobId, lotid = 0, option = "serverinfo_job" };
            return "loginlotinfo";
        }

        throw new ArgumentException("Unable to determine the option for folder permission.");
    }

    private void ConfigureImpartunate(lotmasterModel ds)
    {
        _impartunate = _impartunateFactory.Create(ds.username, ds.password, ds.domain);
        _impartunate.AllowAccesstoServer();
    }
    public Impartunate GetImpartunate()
    {
        return _impartunate;
    }
    public void Dispose()
    {
        try
        {
            _impartunate?.RemoveServerAccess();
            _impartunate = null;
            GC.SuppressFinalize(this);
        }
        catch(Exception ex)
        {
            
        }
       
    }
}
