using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dodohold.core;


namespace molilian.core
{
    public partial class NotifyCore
    {
        static string _base_url = string.Empty;
        static string _anpush_url = string.Empty;
        static string _qyweixin_push_url = string.Empty;

        static NotifyCore()
        {
            _anpush_url = Environment.GetEnvironmentVariable("ANPush");
            _qyweixin_push_url = Environment.GetEnvironmentVariable("QYWeixinPush");

            string? publish_url = Environment.GetEnvironmentVariable("NtfyServer");
            if (!string.IsNullOrEmpty(publish_url))
            {
                _base_url = publish_url;
            }
        }

        // 同步版本保持不变
        public static void Notify(string message)
        {
            if (!string.IsNullOrEmpty(_base_url))
            {
                NifyPlus.Send(_base_url, new NifyMessage { message = message });
            }
        }

        // 新增异步版本
        public static async Task NotifyAsync(string message)
        {
            if (!string.IsNullOrEmpty(_base_url))
            {
                await NifyPlus.SendAsync(_base_url, new NifyMessage { message = message });
            }
        }

        // 同步版本保持不变
        public static void Notify(NifyMessage message)
        {
            if (!string.IsNullOrEmpty(_base_url))
            {
                NifyPlus.Send(_base_url, message);
            }
        }

        // 新增异步版本
        public static async Task NotifyAsync(NifyMessage message)
        {
            if (!string.IsNullOrEmpty(_base_url))
            {
                await NifyPlus.SendAsync(_base_url, message);
            }
        }

        public static async Task QYWeixinPushNotifyAsync(string title, string content)
        {
#if DEBUG
            await Task.CompletedTask;
#else
            if (!string.IsNullOrEmpty(_qyweixin_push_url))
            {
                var args = new
                {
                    msgtype = "markdown",
                    markdown = new
                    {
                        content = $"# {title}\n{content}",
                    }
                };
                string body = string.Empty;
                try
                {
                    WebClientUtility client = new WebClientUtility();
                    client.Post(args.Convert2Json());
                    client.SetContentType("application/json");
                    var response = await client.RequestAsync(_qyweixin_push_url, "POST");
                    body = response.Body();

                    new LoggerLibrary("qyweixin").Info(title, content).Info(body).Save();
                }
                catch (Exception ex)
                {
                    new LoggerLibrary("qyweixin", "error")
                        .Info(title, content)
                        .Info(body)
                        .Info(ex.Message, ex.StackTrace)
                        .Save();
                }
            }
#endif
        }

        // 同步版本保持不变
        public static void AnPushNotify(string title, string content)
        {
#if DEBUG
#else
        if (!string.IsNullOrEmpty(_anpush_url))
        {
            string url = _anpush_url;
            url = url.Replace("{title}", title?.UrlEncode());
            url = url.Replace("{content}", content?.UrlEncode());

            try
            {
                url.HttpRequest();
            }
            catch (Exception ex) { }
        }
#endif
        }

        // 新增异步版本
        public static async Task AnPushNotifyAsync(string title, string content)
        {
#if DEBUG
            await Task.CompletedTask;
#else
        if (!string.IsNullOrEmpty(_anpush_url))
        {
            string url = _anpush_url;
            url = url.Replace("{title}", title?.UrlEncode());
            url = url.Replace("{content}", content?.UrlEncode());

            try
            {
                await Task.Run(() => url.HttpRequest());
            }
            catch (Exception ex) { }
        }
#endif
        }
    }
}