using System;
using Alibabacloud.Rum.Unity.Logs;
using UnityEngine;
using Alibabacloud.Rum.Unity.Network;

namespace Alibabacloud.Rum.Unity.Rum
{
    
    /// <summary>
    /// Wrapper for Android TraceContext object
    /// Matches: com.alibabacloud.rum.capture.resource.customTrace.TraceContext
    /// </summary>
    public class TraceContext
    {
        public string TraceId { get; set; }
        public string SpanId { get; set; }
        public string ParentSpanId { get; set; }
        public int Sampled { get; set; }
        public TraceContextStandard Protocol { get; set; }
        
        public TraceContext()
        {
            TraceId = string.Empty;
            SpanId = string.Empty;
            ParentSpanId = null;
            Sampled = 1;
            Protocol = TraceContextStandard.W3C;
        }
        
        public TraceContext(string traceId, string spanId, TraceContextStandard protocol)
            : this(traceId, spanId, null, 1, protocol)
        {
        }
        
        public TraceContext(string traceId, string spanId, string parentSpanId, int sampled, TraceContextStandard protocol)
        {
            TraceId = traceId ?? string.Empty;
            SpanId = spanId ?? string.Empty;
            ParentSpanId = parentSpanId;
            Sampled = sampled;
            Protocol = protocol;
        }
        
        /// <summary>
        /// Convert to Android TraceContext Java object
        /// Constructor: TraceContext(String traceId, String spanId, String parentSpanId, int sampled, TraceProtocol protocol)
        /// </summary>
        public AndroidJavaObject ToJavaObject()
        {
            try
            {
                // Get TraceProtocol enum value
                var protocolClass = new AndroidJavaClass("com.alibabacloud.rum.capture.resource.customTrace.TraceProtocol");
                if (protocolClass == null) {
                    return null;
                }
                
                // Use valueOf method to get enum constant
                string enumName = Protocol == TraceContextStandard.W3C ? "W3C" : "SKYWALKING";
                
                AndroidJavaObject protocolValue = null;
                try
                {
                    // Call valueOf static method
                    protocolValue = protocolClass.CallStatic<AndroidJavaObject>("valueOf", enumName);
                }
                catch (Exception)
                {
                    return null;
                }

                if (protocolValue == null) {
                    return null;
                }
                
                // Create TraceContext instance
                var traceContext = new AndroidJavaObject(
                    "com.alibabacloud.rum.capture.resource.customTrace.TraceContext",
                    TraceId ?? string.Empty,
                    SpanId ?? string.Empty,
                    ParentSpanId, // Can be null
                    (int)Sampled,
                    protocolValue);
                    
                return traceContext;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
    
    /// <summary>
    /// Wrapper for Android Measuring object
    /// Matches: com.alibabacloud.rum.capture.resource.customResource.Measuring
    /// </summary>
    public class Measuring
    {
        public long Duration { get; set; }
        public long Size { get; set; }
        public long ConnectDuration { get; set; }
        public long SslDuration { get; set; }
        public long DnsDuration { get; set; }
        public long RedirectDuration { get; set; }
        public long FirstByteDuration { get; set; }
        public long DownloadDuration { get; set; }
        
        public Measuring()
        {
            Duration = 0;
            Size = 0;
            ConnectDuration = 0;
            SslDuration = 0;
            DnsDuration = 0;
            RedirectDuration = 0;
            FirstByteDuration = 0;
            DownloadDuration = 0;
        }
        
        public Measuring(long duration, long size)
        {
            Duration = duration;
            Size = size;
            ConnectDuration = 0;
            SslDuration = 0;
            DnsDuration = 0;
            RedirectDuration = 0;
            FirstByteDuration = 0;
            DownloadDuration = 0;
        }
        
        public Measuring(
            long duration,
            long size,
            long connectDuration,
            long sslDuration,
            long dnsDuration,
            long redirectDuration,
            long firstByteDuration,
            long downloadDuration)
        {
            Duration = duration;
            Size = size;
            ConnectDuration = connectDuration;
            SslDuration = sslDuration;
            DnsDuration = dnsDuration;
            RedirectDuration = redirectDuration;
            FirstByteDuration = firstByteDuration;
            DownloadDuration = downloadDuration;
        }
        
        /// <summary>
        /// Convert to Android Measuring Java object
        /// Constructor: Measuring(long duration, long size, long connectDuration, long sslDuration, 
        ///              long dnsDuration, long redirectDuration, long firstByteDuration, long downloadDuration)
        /// </summary>
        public AndroidJavaObject ToJavaObject()
        {
            try
            {
                // Create Measuring instance using full constructor
                var measuring = new AndroidJavaObject(
                    "com.alibabacloud.rum.capture.resource.customResource.Measuring",
                    Duration,
                    Size,
                    ConnectDuration,
                    SslDuration,
                    DnsDuration,
                    RedirectDuration,
                    FirstByteDuration,
                    DownloadDuration);
                    
                return measuring;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
