using System.Net.Http;
using System.Text;
using System.Text.Json; // 適用於 .NET Core 3.0+ / .NET 5/6/7/8
using System.Net;
using System.Net.Sockets;

namespace HeartbeatSender
{
    
    public class HeartbeatData
    {
        public string branch_name { get; set; }
        public string room_name { get; set; }
        public string room_ip { get; set; }
        public string email { get; set; }
        public string password { get; set; }
    }
    public class heartbeatSender
    {
        private readonly HttpClient httpClient = new HttpClient();
        private string token;
        private string heartbeatUrl;

        public heartbeatSender(string heartbeatUrl)
        {
            this.heartbeatUrl = heartbeatUrl;
        }
        public static string GetLocalIPv4()
        {
            foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip))
                {
                    return ip.ToString();
                }
            }
            return "127.0.0.1"; // fallback
        }
        public async Task<bool> LoginAndGetTokenAsync()
        {
            var loginUrl = "http://zqd.superstar.dnsnet.cc/api/room/receiveRegister";
            string hostName = System.Net.Dns.GetHostName();

            var loginPayload = new
            {
                branch_name = "測試",
                room_name = "PC" + hostName.Substring(Math.Max(0, hostName.Length - 3)),
                room_ip = GetLocalIPv4(),
                email = "MachineKTV@gmail.com",
                password = "aa147258-"
            };

            var json = JsonSerializer.Serialize(loginPayload);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            try
            {
                var response = await httpClient.PostAsync(loginUrl, content);
                response.EnsureSuccessStatusCode();

                var responseJson = await response.Content.ReadAsStringAsync();
                // Console.WriteLine("API 回傳內容：" + responseJson);

                using var doc = JsonDocument.Parse(responseJson);
                if (doc.RootElement.TryGetProperty("data", out JsonElement dataElement) &&
                    dataElement.ValueKind == JsonValueKind.Object)
                {
                    if (dataElement.TryGetProperty("token", out JsonElement tokenElement))
                    {
                        token = tokenElement.GetString();
                    }
                }
                // Console.WriteLine("登入成功，取得 token：" + token);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"登入失敗：{ex.Message}");
                return false;
            }
        }

        public async Task SendHeartbeatAsync()
        {
            if (string.IsNullOrEmpty(token))
            {
                Console.WriteLine("請先登入取得 token");
                return;
            }
            //Console.WriteLine(GetLocalIPv4());
            string hostName = System.Net.Dns.GetHostName();

            var heartbeatData = new
            {
                branch_name = "測試",
                hostname = "PC" + hostName.Substring(Math.Max(0, hostName.Length - 3)),
                ip = GetLocalIPv4(),
            };

            var json = JsonSerializer.Serialize(heartbeatData);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            heartbeatUrl = "http://zqd.superstar.dnsnet.cc/api/room/heartbeat";
            var request = new HttpRequestMessage(HttpMethod.Post, heartbeatUrl);
            request.Content = content;
            request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            // Console.WriteLine("送出的 JSON：");
            // Console.WriteLine(json);
            var response = await httpClient.SendAsync(request);
            try
            {

                // Console.WriteLine($"心跳送出狀態：{response.StatusCode}");

                response.EnsureSuccessStatusCode();

                var responseJson = await response.Content.ReadAsStringAsync();

                using var doc = JsonDocument.Parse(responseJson);
                // Console.WriteLine("API 回傳內容：" + responseJson);

            }
            catch (Exception ex)
            {
                var errorContent = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"送出心跳錯誤：{ex.Message}");
                Console.WriteLine($"後端回應內容：{errorContent}");
            }
        }

    }

}
