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

using ThingsGateway.Foundation;
using ThingsGateway.Foundation.Common;

using TouchSocket.Core;

namespace ThingsGateway.Debug;

/// <inheritdoc/>
public partial class DeviceComponent : DeviceComponentBase
{
    /// <summary>
    /// MaxPack
    /// </summary>
    public int MaxPack = 100;

    /// <summary>
    /// VariableRuntimes
    /// </summary>
    public List<VariableClass> VariableRuntimes;

    [Parameter]
    public string ClassString { get; set; }

    [Parameter]
    public string HeaderText { get; set; }

    [Parameter, EditorRequired]
    public string LogPath { get; set; }
    [Parameter]
    public ILog Logger { get; set; }
    /// <summary>
    /// 自定义模板
    /// </summary>
    [Parameter]
    public RenderFragment OtherContent { get; set; }

    /// <summary>
    /// 自定义模板
    /// </summary>
    [Parameter]
    public RenderFragment ReadWriteContent { get; set; }

    [Parameter]
    public bool ShowDefaultOtherContent { get; set; } = true;

    [Parameter]
    public bool ShowDefaultReadWriteContent { get; set; } = true;
    [Parameter]
    public Func<string, Task> OnShowAddressUI { get; set; }

    private async Task ShowAddressUI()
    {
        if (OnShowAddressUI != null)
        {
            await OnShowAddressUI.Invoke(RegisterAddress);
        }
    }

    [Inject]
    private IStringLocalizer<DeviceComponent> Localizer { get; set; }

    /// <summary>
    /// MulReadAsync
    /// </summary>
    /// <returns></returns>
    public async Task MulRead()
    {
        if (Plc != null)
        {
            var deviceVariableSourceReads = Plc.LoadSourceRead<VariableSourceClass, VariableClass>(VariableRuntimes, MaxPack, "1000");
            foreach (var item in deviceVariableSourceReads)
            {
                var result = await Plc.ReadAsync(item.RegisterAddress, item.Length);
                if (result.IsSuccess)
                {
                    try
                    {
                        var result1 = item.Variables.PraseStructContent(Plc, result.Content, exWhenAny: true);
                        if (!result1.IsSuccess)
                        {
                            item.LastErrorMessage = result1.ErrorMessage;
                            var time = DateTime.Now;
                            item.Variables.ForEach(a => a.SetValue(null, time, isOnline: false));
                            Plc.Logger?.Warning(result1.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        Plc.Logger?.LogWarning(ex);
                    }
                }
                else
                {
                    item.LastErrorMessage = result.ErrorMessage;
                    var time = DateTime.Now;
                    item.Variables.ForEach(a => a.SetValue(null, time, isOnline: false));
                    Plc.Logger?.Warning(result.ToString());
                }
            }
        }
    }

    /// <inheritdoc/>
    protected override void OnInitialized()
    {
        VariableRuntimes = new()
            {
                                new VariableClass()
                                {
                                    DataType=DataTypeEnum.Int16,
                                    RegisterAddress="40001",
                                    IntervalTime="1000",
                                },
                                   new VariableClass()
                                {
                                    DataType=DataTypeEnum.Int32,
                                    RegisterAddress="40011",
                                    IntervalTime="1000",
                                },
            };

        HeaderText = Localizer[nameof(HeaderText)];
        base.OnInitialized();
    }
}
