using Renci.SshNet;
using System;
using File = System.IO.File;

namespace JWH.NETWORK
{
    public static class StpClientWrap
    {
        private static SftpClient _sftpClient;
        private static string _host;
        private static int _port = 22;
        private static string _userId;
        private static string _password;

        public static void Initialize(string host, string userId, string password, int port = 22)
        {
            _host = host;
            _userId = userId;
            _password = password;
            _port = port;
            _sftpClient = new SftpClient(_host, _port, _userId, _password);
        }

        public static void Connect()
        {
            if (_sftpClient == null)
            {
                throw new InvalidOperationException("SFTP client is not initialized. Call Initialize() first.");
            }

            if (!_sftpClient.IsConnected)
            {
                _sftpClient.Connect();
            }
        }

        public static void Disconnect()
        {
            if (_sftpClient != null && _sftpClient.IsConnected)
            {
                _sftpClient.Disconnect();
            }
        }

        public static bool IsDirExists(string path)
        {
            return IsFileExists(path) && _sftpClient.GetAttributes(path).IsDirectory;
        }


        public static bool IsFileExists(string path)
        {
            try
            {
                Connect();
                return _sftpClient.Exists(path);
            }
            catch
            {
                return false;
            }
            finally
            {
                Disconnect();
            }
        }

        public static Renci.SshNet.Sftp.SftpFileAttributes GetFileInfo(string remoteFilePath)
        {
            try
            {
                Connect();
                if (_sftpClient.Exists(remoteFilePath))
                {
                    return _sftpClient.GetAttributes(remoteFilePath);
                }
                else
                {
                    Console.WriteLine("파일이 존재하지 않습니다.");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"파일 정보 조회 오류: {ex.Message}");
                return null;
            }
            finally
            {
                Disconnect();
            }
        }

        public static object GetSftpList(string path)
        {
            try
            {
                Connect();
                return _sftpClient.ListDirectory(path);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Disconnect();
            }
        }

        public static bool DownloadSftpFile(string remotePath, string localPath)
        {
            try
            {
                Connect();
                using (var fileStream = File.OpenWrite(localPath))
                {
                    _sftpClient.DownloadFile(remotePath, fileStream);
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Download error: {ex.Message}");
                return false;
            }
            finally
            {
                Disconnect();
            }
        }

        public static bool UploadSftpFile(string localPath, string remotePath)
        {
            try
            {
                Connect();
                using (var fileStream = File.OpenRead(localPath))
                {
                    _sftpClient.UploadFile(fileStream, remotePath);
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Upload error: {ex.Message}");
                return false;
            }
            finally
            {
                Disconnect();
            }
        }
    }
}
