using System;
using System.Collections.Generic;
using System.IO;
// using System.Linq;
using System.Text;
//using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using Opc.Ua;
using Opc.Ua.Client;
using Npgsql; // 👈 PostgreSQL 라이브러리 추가

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; } = "";
    }

    public class TagMaster
    {
        public string TagName {get; set; } = string.Empty;
        public string FullNodeId {get; set; }= string.Empty;
        public string NodeClass {get; set; }= string.Empty;
        public string DataType {get; set; }= string.Empty;
        public int Level {get; set; }
    }

    public class HoneywellCrawler
    {
        private readonly Session _session;
        private readonly List<TagMaster> _discoveredTags = []; // IDE0028 적용

        public HoneywellCrawler(Session session)
        {
            // 의존성 주입 및 Null 체크
            _session = session ?? throw new ArgumentNullException(nameof(session));
        }

        /// <summary>
        /// 저인망 탐사 시작점
        /// </summary>
        public async Task RunAsync(string startNodeId)
        {
            Console.WriteLine($"\n🚀 비동기 저인망 탐사 시작: {startNodeId}");
            
            try 
            {
                NodeId rootNode = NodeId.Parse(startNodeId);
                await BrowseRecursiveAsync(rootNode, 0);

                Console.WriteLine("\n===============================================");
                Console.WriteLine($"✅ 탐사 완료! 총 {_discoveredTags.Count}개의 항목 발견.");
                Console.WriteLine("===============================================");
                
                SaveToCsv();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ 실행 중 치명적 오류: {ex.Message}");
            }
        }

        private async Task BrowseRecursiveAsync(NodeId nodeId, int level)
        {
            try
            {
                BrowseDescription description = new() {
                    NodeId = nodeId,
                    BrowseDirection = BrowseDirection.Forward,
                    ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
                    IncludeSubtypes = true,
                    NodeClassMask = (uint)(NodeClass.Variable | NodeClass.Object),
                    ResultMask = (uint)BrowseResultMask.All
                };

                // 1. BrowseAsync는 객체 방식 (에러 CS1061/CS8129 방지)
                BrowseResponse response = await _session.BrowseAsync(null, null, 0, [description], default);

                if (response?.Results == null || response.Results.Count == 0) return;

                foreach (var result in response.Results)
                {
                    await ProcessReferencesAsync(result.References, level);

                    byte[] cp = result.ContinuationPoint;
                    while (cp != null && cp.Length > 0)
                    {
                        // 2. BrowseNextAsync는 튜플 방식 (에러 CS0029 방지)
                        var (nextHeader, nextCp, nextRefs) = await _session.BrowseNextAsync(null, false, cp, default);

                        await ProcessReferencesAsync(nextRefs, level);
                        cp = nextCp;
                    }
                }
            }
            catch (Exception)
            {
                // 특정 노드 접근 권한 에러 등은 무시하고 진행
                // Console.WriteLine($"⚠️ [Level {level}] {nodeId} 탐색 건너뜀: {ex.Message}");
            }
        }

        private async Task ProcessReferencesAsync(ReferenceDescriptionCollection references, int level)
        {
            if (references == null || references.Count == 0) return;

            foreach (var rd in references)
            {
                // ExpandedNodeId를 실제 NodeId로 변환 (Namespace 관리용)
                NodeId childId = ExpandedNodeId.ToNodeId(rd.NodeId, _session.NamespaceUris);

                // 마스터 리스트에 추가
                _discoveredTags.Add(new TagMaster
                {
                    TagName = rd.BrowseName.Name ?? "Unknown",
                    FullNodeId = childId.ToString(),
                    NodeClass = rd.NodeClass.ToString(),
                    Level = level
                });

                // 콘솔 출력 (진행 상황 확인용)
                string indent = new string(' ', level * 2);
                Console.WriteLine($"{indent} [{rd.NodeClass}] {rd.BrowseName.Name} (ID: {childId})");

                // 3. Object(폴더/태그)인 경우 재귀 탐색 (하니웰 구조에 맞춰 깊이 5단계 제한)
                if (rd.NodeClass == NodeClass.Object && level < 5)
                {
                    await BrowseRecursiveAsync(childId, level + 1);
                }
            }
        }

        private void SaveToCsv()
        {
            try
            {
                using StreamWriter sw = new("Honeywell_FullMap.csv");
                sw.WriteLine("Level,Class,Name,NodeId");
                foreach (var tag in _discoveredTags)
                {
                    sw.WriteLine($"{tag.Level},{tag.NodeClass},{tag.TagName},{tag.FullNodeId}");
                }
                Console.WriteLine($"💾 CSV 저장 완료: {Path.GetFullPath("Honeywell_FullMap.csv")}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ CSV 저장 실패: {ex.Message}");
            }
        }
    }

    class Program
    {
        static Dictionary<string, StatusCodeInfo> _statusCodeMap = new(StringComparer.OrdinalIgnoreCase);
        
        // 1. DB 연결 문자열 (비밀번호를 본인 설정에 맞게 수정하세요)
        static string dbConnString = "Host=localhost;Username=postgres;Password=postgres;Database=opcdb";

        static void LoadStatusCodes()
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
            if (File.Exists(path))
            {
                try {
                    var json = File.ReadAllText(path);
                    var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
                    var list = JsonSerializer.Deserialize<List<StatusCodeInfo>>(json, options);
                    if (list != null) {
                        foreach (var item in list) _statusCodeMap[item.Hex] = item;
                        Console.WriteLine($"✅ {_statusCodeMap.Count}개의 에러 코드 정의 로드 완료.");
                    }
                } catch { }
            }
        }

        // 2. DB 저장 함수
        static async Task SaveToDatabase(string tagName, double val, string status)
        {
            try {
                using var conn = new NpgsqlConnection(dbConnString);
                await conn.OpenAsync();
                
                string sql = "INSERT INTO opc_history (tag_name, tag_value, status_code) VALUES (@tag, @val, @status)";
                using var cmd = new NpgsqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("tag", tagName);
                cmd.Parameters.AddWithValue("val", val);
                cmd.Parameters.AddWithValue("status", status);
                
                await cmd.ExecuteNonQueryAsync();
                Console.WriteLine("💾 DB 저장 완료.");
            }
            catch (Exception ex) {
                Console.WriteLine($"❌ DB 저장 실패: {ex.Message}");
            }
        }

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

            string serverHostName = "192.168.0.20";
            string clientHostName = "dbsvr"; 
            string endpointUrl = $"opc.tcp://{serverHostName}:4840";
            string applicationUri = $"urn:{clientHostName}:OpcTestClient"; 
            string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
            string pfxPassword = ""; 
            string userName = "mngr";
            string password = "mngr";

            Directory.CreateDirectory(Path.GetDirectoryName(pfxPath)!);
            Directory.CreateDirectory("pki/trusted/certs");
            Directory.CreateDirectory("pki/issuers/certs");
            Directory.CreateDirectory("pki/rejected/certs");

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

            var config = new ApplicationConfiguration {
                ApplicationName = "OpcTestClient",
                ApplicationType = ApplicationType.Client,
                ApplicationUri = applicationUri,
                SecurityConfiguration = new SecurityConfiguration {
                    ApplicationCertificate = new CertificateIdentifier { 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,
                    AddAppCertToTrustedStore = true
                },
                TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
                ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
            };

            await config.ValidateAsync(ApplicationType.Client);
            config.CertificateValidator.CertificateValidation += (v, e) => { if (e.Error.StatusCode != StatusCodes.Good) e.Accept = true; };

            ISession? session = null;
            try
            {
                Console.WriteLine($"Step 1: Connecting to {endpointUrl}...");
                var endpointConfig = EndpointConfiguration.Create(config);
                using var discovery = await DiscoveryClient.CreateAsync(config, new Uri(endpointUrl), DiagnosticsMasks.All, CancellationToken.None);
                var endpoints = await discovery.GetEndpointsAsync(null);
                var selected = endpoints.OrderByDescending(e => e.SecurityLevel)
                                       .FirstOrDefault(e => e.SecurityPolicyUri.Contains("Basic256Sha256")) ?? endpoints[0];
                
                var endpoint = new ConfiguredEndpoint(null, selected, endpointConfig);
                var identity = new UserIdentity(userName, Encoding.UTF8.GetBytes(password));

                #pragma warning disable CS0618
                session = await Session.Create(
                    config,
                    endpoint,
                    false,
                    "OpcTestSession",
                    60000,
                    identity,
                    null
                );
                #pragma warning restore CS0618
                Console.WriteLine($"✅ Connected! SessionID: {session.SessionId}");

                Console.WriteLine("\n🔍 하니웰 전체 노드 탐사(저인망) 시작...");
                // ISession을 실제 Session 클래스로 형변환하여 전달
                if (session is Session clientSession)
                {
                    HoneywellCrawler crawler = new HoneywellCrawler(clientSession);
                    // 'shinam' 노드를 기점으로 바닥까지 훑고 CSV 저장까지 수행합니다.
                    await crawler.RunAsync("ns=1;s=$assetmodel");
                }

                  // 3. 데이터 읽기 및 DB 저장 루프 (테스트용으로 5회 반복)
                string nodeID = "ns=1;s=shinam:p-6102.hzset.fieldvalue";
                for (int i = 0; i < 5; i++)
                {
                    var result = await session.ReadValueAsync(nodeID);
                    double val = Convert.ToDouble(result.Value);
                    string status = result.StatusCode.ToString();

                    Console.WriteLine($"[{i+1}] TAG: {val} (Status: {status})");
                    
                    // DB에 저장
                    await SaveToDatabase("p-6102", val, status);
                    
                    await Task.Delay(2000); // 2초 대기
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ 오류 발생: {ex.Message}");
            }
            finally { if (session != null) { await session.CloseAsync(); session.Dispose(); } }
            Console.WriteLine("\n작업 완료. 엔터를 누르세요...");
            Console.ReadLine();
        }
    }
}