// ------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
// 此代码版权（除特别声明外的代码）归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
// 使用文档：https://thingsgateway.cn/
// QQ群：605534569
// ------------------------------------------------------------------------------

#pragma warning disable CA2007 // 考虑对等待的任务调用 ConfigureAwait
using BootstrapBlazor.Components;

using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;

using System.Text;

using ThingsGateway.Common;
using ThingsGateway.Gateway.Razor;
using ThingsGateway.Plugin.DB;
using ThingsGateway.Plugin.SqlDB;
using ThingsGateway.Plugin.TDengineDB;
using ThingsGateway.SqlSugar;

using TouchSocket.Core;

namespace ThingsGateway.Debug
{
    public partial class SqlDBProducerPropertyRazor : IPropertyUIBase
    {
        [Inject]
        IStringLocalizer<ThingsGateway.Razor._Imports> RazorLocalizer { get; set; }


        [Parameter, EditorRequired]
        public IEnumerable<IEditorItem> PluginPropertyEditorItems { get; set; }
        [Parameter, EditorRequired]
        public string Id { get; set; }
        [Parameter, EditorRequired]
        public bool CanWrite { get; set; }
        [Parameter, EditorRequired]
        public ModelValueValidateForm Model { get; set; }

        IStringLocalizer ProducerPropertyLocalizer { get; set; }

        protected override Task OnParametersSetAsync()
        {
            ProducerPropertyLocalizer = App.CreateLocalizerByType(Model.Value.GetType());

            return base.OnParametersSetAsync();
        }

        private async Task CheckScript(SqlDBProducerProperty businessProperty, string pname)
        {
            string script = null;
            {
                script = pname == businessProperty.BigTextScriptHistoryTable ? businessProperty.BigTextScriptHistoryTable : businessProperty.BigTextScriptRealTable;

            }


            var op = new DialogOption()
            {
                IsScrolling = true,
                Title = RazorLocalizer["Check"],
                ShowFooter = false,
                ShowCloseButton = false,
                Size = Size.ExtraExtraLarge,
                FullScreenSize = FullScreenSize.None
            };

            op.Component = BootstrapDynamicComponent.CreateComponent<ScriptCheck>(new Dictionary<string, object?>
    {
        {nameof(ScriptCheck.Script),script },
                {nameof(ScriptCheck.GetResult),  async (string input,string script)=>
        {
                var type=  typeof(List<VariableBasicData>);

                var  data = (IEnumerable<object>)Newtonsoft.Json.JsonConvert.DeserializeObject(input, type);
               var getDeviceModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(script);
            StringBuilder stringBuilder=new($"Compilation successful{Environment.NewLine}");

            getDeviceModel.Logger=new EasyLogger(a=>stringBuilder.AppendLine(a));
                    using    var db = SqlDBBusinessDatabaseUtil.GetDb(businessProperty);
                await getDeviceModel.DBInit(db,default);
              await getDeviceModel.DBInsertable(db,data,default);
              return stringBuilder.ToString();

        }},

               {nameof(ScriptCheck.OnGetDemo),()=>
                {
                    return
                    pname == nameof(SqlDBProducerProperty.BigTextScriptHistoryTable)?
                    """"
                    using ThingsGateway.Foundation;
                    
                    using System.Dynamic;
                    
                    using TouchSocket.Core;
                    public class S1 : DynamicSQLBase
                    {

                        public override async Task DBInit(ISqlSugarClient db, CancellationToken cancellationToken)
                        {

                            var sql = $"""

                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                        public override async Task DBInsertable(ISqlSugarClient db, IEnumerable<object> datas, CancellationToken cancellationToken)
                        {
                            var sql = $"""

                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                    }
                    
                    """"
                    :

                    pname == nameof(SqlDBProducerProperty.BigTextScriptRealTable)?

                    """"

                    using System.Dynamic;
                    using ThingsGateway.Foundation;
                    

                    using TouchSocket.Core;
                    public class S1 : DynamicSQLBase
                    {

                        public override async Task DBInit(ISqlSugarClient db, CancellationToken cancellationToken)
                        {

                            var sql = $"""

                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                        public override async Task DBInsertable(ISqlSugarClient db, IEnumerable<object> datas, CancellationToken cancellationToken)
                        {
                            var sql = $"""

                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                    }

                    """"
                    :
                    ""
                    ;
                }
            },
        {nameof(ScriptCheck.ScriptChanged),EventCallback.Factory.Create<string>(this, v =>
        {
                 if (pname == nameof(SqlDBProducerProperty.BigTextScriptHistoryTable))
    {
            businessProperty.BigTextScriptHistoryTable=v;

    }
    else if (pname == nameof(SqlDBProducerProperty.BigTextScriptRealTable))
    {
           businessProperty.BigTextScriptRealTable=v;
    }

        }) },

    });
            await DialogService.Show(op);

        }



        private async Task CheckScript(RealDBProducerProperty businessProperty, string pname)
        {
            string script = businessProperty.BigTextScriptHistoryTable;

            var op = new DialogOption()
            {
                IsScrolling = true,
                Title = RazorLocalizer["Check"],
                ShowFooter = false,
                ShowCloseButton = false,
                Size = Size.ExtraExtraLarge,
                FullScreenSize = FullScreenSize.None
            };

            op.Component = BootstrapDynamicComponent.CreateComponent<ScriptCheck>(new Dictionary<string, object?>
    {
        {nameof(ScriptCheck.Script),script },
                {nameof(ScriptCheck.GetResult), async (string input,string script)=>
        {
                var type=  typeof(List<VariableBasicData>);

                var  data = (IEnumerable<object>)Newtonsoft.Json.JsonConvert.DeserializeObject(input, type);
               var getDeviceModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(script);
            StringBuilder stringBuilder=new($"Compilation successful{Environment.NewLine}");

            getDeviceModel.Logger=new EasyLogger(a=>stringBuilder.AppendLine(a));
             SqlSugarClient db=null;

                 if(businessProperty.DbType==SqlSugar.DbType.TDengine)
        db = TDengineDBUtil.GetDb(businessProperty.DbType, businessProperty.BigTextConnectStr, businessProperty.NumberTableNameLow);
            else
        db = BusinessDatabaseUtil.GetDb(businessProperty.DbType, businessProperty.BigTextConnectStr);

                await getDeviceModel.DBInit(db,default);
              await getDeviceModel.DBInsertable(db,data,default);
              return stringBuilder.ToString();

        }},

               {nameof(ScriptCheck.OnGetDemo),()=>
                {
                    return
                    pname == nameof(SqlDBProducerProperty.BigTextScriptHistoryTable)?
                    """"
                    using ThingsGateway.Foundation;
                    
                    using System.Dynamic;
                    using ThingsGateway.SqlSugar;
                    using ThingsGateway.Gateway.Application;
                    using ThingsGateway.Plugin.DB;
                    using System.Dynamic;
                    
                    using TouchSocket.Core;
                    public class S1 : DynamicSQLBase
                    {

                        public override async Task DBInit(ISqlSugarClient db, CancellationToken cancellationToken)
                        {

                            var sql = $"""
                                        111
                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                        public override async Task DBInsertable(ISqlSugarClient db, IEnumerable<object> datas, CancellationToken cancellationToken)
                        {
                            var sql = $"""
                                        111
                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                    }
                    
                    """"
                    :

                    pname == nameof(SqlDBProducerProperty.BigTextScriptRealTable)?

                    """"

                    using System.Dynamic;
                    using ThingsGateway.Foundation;
                    using ThingsGateway.SqlSugar;
                    using ThingsGateway.Gateway.Application;
                    using ThingsGateway.Plugin.DB;

                    using TouchSocket.Core;
                    public class S1 : DynamicSQLBase
                    {

                        public override async Task DBInit(ISqlSugarClient db, CancellationToken cancellationToken)
                        {

                            var sql = $"""
                                        111
                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                        public override async Task DBInsertable(ISqlSugarClient db, IEnumerable<object> datas, CancellationToken cancellationToken)
                        {
                            var sql = $"""
                                            111
                                    """;
                            await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
                        }
                    }

                    """"
                    :
                    ""
                    ;
                }
            },
        {nameof(ScriptCheck.ScriptChanged),EventCallback.Factory.Create<string>(this, v =>
        {
businessProperty.BigTextScriptHistoryTable=v;

        }) },

    });
            await DialogService.Show(op);

        }


        [Inject]
        DialogService DialogService { get; set; }
    }
}



