using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text;

namespace OpcConnectionTest
{
    public class StatusCodeInfo
    {
        public string Name { get; set; } = "";
        public string Hex { get; set; } = "";
        public ulong Decimal { get; set; }
        public string Description { get; set; } = "";
    }

    class Program
    {
        static Dictionary<uint, StatusCodeInfo> _statusCodeMap = new();

        static void LoadStatusCodes()
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
            if (!File.Exists(path))
            {
                Console.WriteLine("⚠️ statuscode.json 파일을 찾을 수 없습니다.");
                return;
            }

            try
            {
                var json = File.ReadAllText(path);
                var list = JsonSerializer.Deserialize<List<StatusCodeInfo>>(json);
                if (list != null)
                    foreach (var item in list)
                        _statusCodeMap[(uint)item.Decimal] = item;

                Console.WriteLine($"✅ StatusCode {_statusCodeMap.Count}개 로드 완료\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ statuscode.json 로드 실패: {ex.Message}");
            }
        }

        static async Task Main(string[] args)
        {
            LoadStatusCodes();

            string endpointUrl = "opc.tcp://192.168.0.20:4840";
            string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
            string pfxPassword = "";
            string userName = "mngr";
            string password = "mngr";

            if (!File.Exists(pfxPath))
            {
                Console.WriteLine($"❌ PFX 파일 없음: {pfxPath}");
                return;
            }

            var clientCert = new X509Certificate2(
                pfxPath,
                pfxPassword,
                X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet
            );

            if (!clientCert.HasPrivateKey)
            {
                Console.WriteLine("❌ 인증서에 개인키 없음");
                return;
            }

            var config = new ApplicationConfiguration
            {
                ApplicationName = "OPC Test Client",
                ApplicationType = ApplicationType.Client,
                SecurityConfiguration = new SecurityConfiguration
                {
                    ApplicationCertificate = new CertificateIdentifier
                    {
                        StoreType = "Directory",
                        StorePath = Path.GetFullPath("pki/own"),
                        Certificate = clientCert
                    },
                    TrustedPeerCertificates = new CertificateTrustList
                    {
                        StoreType = "Directory",
                        StorePath = Path.GetFullPath("pki/trusted")
                    },
                    TrustedIssuerCertificates = new CertificateTrustList
                    {
                        StoreType = "Directory",
                        StorePath = Path.GetFullPath("pki/issuers")
                    },
                    RejectedCertificateStore = new CertificateTrustList
                    {
                        StoreType = "Directory",
                        StorePath = Path.GetFullPath("pki/rejected")
                    },
                    AutoAcceptUntrustedCertificates = true
                },
                TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
                ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
            };

            config.CertificateValidator.CertificateValidation += (s, e) =>
            {
                Console.WriteLine($"⚠️ 서버 인증서 검증 실패: {e.Error.StatusCode} - {e.Certificate.Subject}");
            };

            await config.ValidateAsync(ApplicationType.Client);

            ISession? session = null;

            try
            {
                Console.WriteLine("Step 1: Discovering endpoints...");
                var discovery = DiscoveryClient.Create(new Uri(endpointUrl));
                var endpoints = discovery.GetEndpoints(null);

                Console.WriteLine($"✅ {endpoints.Count} 엔드포인트 발견");

                foreach (var ep in endpoints)
                    Console.WriteLine($" - {ep.EndpointUrl} | {ep.SecurityMode} | {ep.SecurityPolicyUri?.Split('#').Last()}");

                // AES256/SHA256 우선 선택
                var selected = endpoints.FirstOrDefault(ep =>
                    ep.SecurityMode == MessageSecurityMode.SignAndEncrypt &&
                    ep.SecurityPolicyUri.EndsWith("Basic256Sha256"))
                    ?? endpoints.First();

                Console.WriteLine($"\n선택된 엔드포인트: {selected.EndpointUrl} ({selected.SecurityMode})");

                var configured = new ConfiguredEndpoint(null, selected, EndpointConfiguration.Create(config));

                // ✅ Session.CreateAsync 사용
                session = await Session.Create(
                    config,
                    configured,
                    false,
                    "OPC Test Session",
                    60000,
                    new UserIdentity(userName, Encoding.UTF8.GetBytes(password)),
                    null
                );

                Console.WriteLine($"✅ Connected! SessionID: {session.SessionId}");

                await ReadServerInfoAsync(session);
            }
            catch (ServiceResultException sre)
            {
                uint code = (uint)sre.StatusCode;
                Console.WriteLine($"❌ OPC UA 오류: {sre.Message}");
                Console.WriteLine($"   StatusCode: 0x{code:X8}");

                if (_statusCodeMap.TryGetValue(code, out var info))
                    Console.WriteLine($"   Name: {info.Name}\n   Description: {info.Description}");
                else
                    Console.WriteLine(" ⚠️ statuscode.json에 정의되지 않은 코드입니다.");
            }
            finally
            {
                if (session != null)
                {
                    await session.CloseAsync();
                    session.Dispose();
                    Console.WriteLine("✅ Session closed");
                }
            }

            Console.WriteLine("\nPress Enter to exit...");
            Console.ReadLine();
        }

        static async Task ReadServerInfoAsync(ISession session)
        {
            var state = await session.ReadValueAsync(Variables.Server_ServerStatus_State);
            var time = await session.ReadValueAsync(Variables.Server_ServerStatus_CurrentTime);

            Console.WriteLine($"Server State: {state.Value}");
            Console.WriteLine($"Current Time: {time.Value}");
        }
    }
}
