using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Domain.Log;
using Aliyun.Api.LogService.Domain.LogStore.Index;
using Aliyun.Api.LogService.Infrastructure.Protocol;
using Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace COMMON
{
    /// <summary>
    /// 阿里云服务 
    /// </summary>
    public class ASLS
    {
        //此处以深圳为例，其它地域请根据实际情况填写。
        private static string endpoint = "cn-shenzhen.log.aliyuncs.com";

        //  private static string accessKeyId = "LTAI4GKcpyML2ihVUe6bk1qj";
        private static string accessKeyId = "LTAI5tFDX4g74gJvEsdidRtD";

        // private static string accessKeySecret = "IbeljP1LAuFYc0pGPvrX4AMAbrNhkj";
        private static string accessKeySecret = "mN7DDagPbjcKc5qFgHEIGvmasUsRF7";

        //Project名称。
        private static string project = "rucs-project";

        //Logstore名称。
        private static string logstore = "rucs-udp-log-db";



        private static string logstore_ = "rucs-eventlogging-db";


        /// <summary>
        /// 	主题
        /// </summary>
        private static string topic = "rucs-udp";

        /// <summary>
        /// 创建日志服务Client。
        /// </summary>
        public ILogServiceClient client => BuildSimpleClient();

        /// <summary>
        /// 初始化 阿里云服务
        /// </summary>
        public async void Init()
        {

            var data = await client.GetProjectAsync();

            if (data.IsSuccess == true)
            {
                LogHelp.Warning(project + "项目已经存在~");
            }
            if (/*data == null ||*/ !data.IsSuccess)
            {
                //创建Project。
                var proRes = await client.CreateProjectAsync(project, "代码测试~");
                check(proRes);
                LogHelp.Warning("Create project success,await 30 S ~");
                Thread.Sleep(30 * 1000);

            }

            var data1 = await client.ListLogStoreAsync(logstore, 0);
            check(data1);
            if (data1.Result == null || data1.Result.Logstores.IndexOf(logstore) < 0)
            {
                //创建Logstore。
                var storeRes = await client.CreateLogStoreAsync(logstore, 90, 9);
                check(storeRes);
                LogHelp.Warning("Create logstore success,await 30 s~");
                Thread.Sleep(30 * 1000);
                //为Logstore创建索引。
                var Index = new Dictionary<String, IndexKeyInfo>() { };

                Index.Add("id", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("datetime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("mac", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("msg_id", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("result", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("receipttime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("packagetime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("revdelaydatetime", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("udp_cmd", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("prot_timeout", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("remark", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("tcp_id", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("data_valid", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("processing_timespent", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("content_decrypt", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("content_encrypt", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("length", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("retry_count", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("sed_timeout", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("rcv_timeout", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("destination_port", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("destination", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("source_port", new IndexLongKeyInfo() { DocValue = true });
                Index.Add("direction", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("source_ip", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                Index.Add("udp_id", new IndexTextKeyInfo(new char[' ']) { DocValue = true });

                var indRes = await client.CreateIndexAsync(logstore, Index);
                check(indRes);
                LogHelp.Warning("Create Index success,await 30 s~");
                Thread.Sleep(30 * 1000);

            }
            else
                LogHelp.Warning("logstore已经存在~");
        }


        /// <summary>
        /// 写入阿里云服务
        /// </summary>
        /// <returns></returns>
        public  async Task<bool>write(string _logstore,params LogInfo[] log)
        {
            try
            {
            LogGroupInfo data = new LogGroupInfo();
            data.Logs = log;
            var indRes = await  client.PostLogStoreLogsAsync(
                new PostLogsRequest(_logstore, data)
                {
                    ProjectName = project
                }
                );
            check(indRes);
            }
            catch (Exception ex)
            {

               return false;
            }
            return true;
        }
        /// <summary>
        /// 初始化 阿里云服务
        /// </summary>
        public async void Init_()
        {
            try
            {
                var data = await client.GetProjectAsync();
                if (data.IsSuccess == true)
                {
                    LogHelp.Warning(project + "项目已经存在~");
                    Debug.Write(project + "项目已经存在~");
                }
                if (/*data == null ||*/ !data.IsSuccess)
                {
                    //创建Project。
                    var proRes = await client.CreateProjectAsync(project, "代码测试~");
                    check(proRes);
                    LogHelp.Warning("Create project success,await 30 S ~");
                    Debug.Write("Create project success,await 30 S ~");
                    Thread.Sleep(30 * 1000);
                }
                var data1 = await client.ListLogStoreAsync(logstore_, 0);
                check(data1);
                if (data1.Result == null || data1.Result.Logstores.IndexOf(logstore_) < 0)
                {
                    //创建Logstore。
                    var storeRes = await client.CreateLogStoreAsync(logstore_, 90, 9);
                    check(storeRes);
                    LogHelp.Warning("Create logstore success,await 30 s~");
                    Debug.Write("Create logstore success,await 30 S ~");
                    Thread.Sleep(30 * 1000);
                    //为Logstore创建索引。
                    var Index = new Dictionary<String, IndexKeyInfo>() { };
                    Index.Add("event_type", new IndexLongKeyInfo() { DocValue = true });
                    Index.Add("eventlogging_id", new IndexLongKeyInfo() { DocValue = true });
                    Index.Add("registrar", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("input_datetime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("remark", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("output_datatime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("steprecording", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("info", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("role", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("mac", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
                    Index.Add("hotel_id", new IndexDoubleKeyInfo() { DocValue = true });
                    Index.Add("hotel_old_id", new IndexDoubleKeyInfo() { DocValue = true });
                    Index.Add("room_old_id", new IndexDoubleKeyInfo() { DocValue = true });
                    Index.Add("room_id", new IndexDoubleKeyInfo() { DocValue = true });
                    var indRes = await client.CreateIndexAsync(logstore_, Index);
                    check(indRes);
                    LogHelp.Warning("Create Index success,await 30 s~");
                    Debug.Write("Create Index success,await 30 S ~");
                    Thread.Sleep(30 * 1000);

                }
                else
                {
                    LogHelp.Warning("logstore已经存在~");
                    Debug.Write("logstore已经存在~");
                }
            }
            catch (Exception EX)
            {
                Debug.Write(EX.ToString());
            }
        }


        /// <summary>
        /// 查询 阿里云数据  udp
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="line"></param>
        /// <param name="offset"></param>
        /// <param name="losg"></param>
        /// <returns></returns>
        public async Task<IList<IDictionary<string, string>>> GetLogs_(int losg, string sql, DateTime start = default , DateTime end = default, int line = 100, int offset = 0)
        {
            if(start == default)
            {
                start = ConfigEntity.Instance.STARTTIME;
            }
            if (end == default)
            {
                end = DateTime.Now;
            }
            var logstr = losg == 0 ? logstore : (losg==1? logstore_: "rucs-log-db");
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();
            if (end - start >= TimeSpan.FromDays(ConfigEntity.Instance.ASLS))
                start = end.AddDays(-ConfigEntity.Instance.ASLS);

            sql = sql.TrimEnd();

            IList<IDictionary<string, string>> RES = new List<IDictionary<string, string>>();

            DateTimeOffset start_ = start;//.AddHours  (-8);

            DateTimeOffset end_ = end;//.AddHours(-8);
            try
            {
                var logsRes = await client.GetLogsAsync(logstr, start_, end_, topic, sql, line, offset, true);

                check(logsRes);

                return logsRes.Result.Logs;
            }
            catch (Exception EX)
            {
                LogHelp.Error(EX.ToString());
                RES = new List<IDictionary<string, string>>();
            }
            finally
            {
                stopwatch.Stop();
                LogHelp.Error($"{sql}----  用时：" + (stopwatch.ElapsedMilliseconds / 1000.00) + "。</p>");
            }
            return RES;
        }

        /// <summary>
        ///  sls
        /// </summary>
        /// <returns></returns>
        public ILogServiceClient BuildSimpleClient()
            => LogServiceClientBuilders.HttpBuilder
                .Endpoint(endpoint, project)
                .Credential(accessKeyId, accessKeySecret)
                .Build();
        /// <summary>
        /// 检查结果
        /// </summary>
        /// <param name="res"></param>
        /// <exception cref="ApplicationException"></exception>
        public void check(IResponse res)
        {
            if (!res.IsSuccess)
            {
                throw new ApplicationException(res.Error != null ? res.Error.ErrorMessage : "请求错误~" + Newtonsoft.Json.JsonConvert.SerializeObject(res));
            }
        }
    }

}
