@page "/init"
@using MasaBlazorApp3.Pojo.Config;
@using MasaBlazorApp3.Pojo.Vo;
@using MasaBlazorApp3.Util;
@using Microsoft.Extensions.Options;
@using Newtonsoft.Json;
@using log4net;

<RadzenStack AlignItems="AlignItems.Center" Gap="2rem" class="rz-p-12">
    <RadzenImage Path="images/community.svg" Style="width: 200px;" AlternateText="community" />
    <RadzenText TextStyle="TextStyle.H6">加载程序必要连接中，请稍等。。。</RadzenText>
    <RadzenText TextStyle="TextStyle.H6">@msg</RadzenText>
    <RadzenText TextStyle="TextStyle.H6">@errMsg</RadzenText>
</RadzenStack>

@code {
    @inject Radzen.DialogService dialogService;
    @inject IOptions<DrawerConfig> setting;
    @inject NotificationService _message;
    @inject PortUtil PortUtil;
    @inject FingerprintUtil FingerprintUtil;
    @inject GlobalStateService globalStateService;
    @inject IOptions<PortConfig> portSetting;


    string msg;
    string errMsg;

    private readonly ILog logger = LogManager.GetLogger(typeof(InitPage));


    protected override async Task OnInitializedAsync()
    {

        base.OnInitializedAsync();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        base.OnAfterRender(firstRender);

        if (firstRender)
        {
            await Task.Delay(15);
            msg = "开始连接指纹模块";
            InvokeAsync(StateHasChanged);
            try
            {
                bool flag = await FingerprintUtil.ConnectionMain();
                if (flag)
                {
                    msg = "指纹模块连接成功";
                }
                else
                {
                    msg = "指纹模块连接失败";
                }
                InvokeAsync(StateHasChanged);
            }
            catch (Exception e)
            {
                msg = "指纹模块连接失败";
                errMsg += "【指纹模块连接失败】";
                logger.Info($"指纹模块连接失败【{e.Message}】");
                InvokeAsync(StateHasChanged);
            }
            await Task.Delay(500);
            msg = "开始连接抽屉串口";
            InvokeAsync(StateHasChanged);
            try
            {
                PortUtil.drawerSerial.Open();
                logger.Info($"抽屉串口打开结果【{PortUtil.drawerSerial.IsOpen}】");
                msg = "抽屉串口连接成功";
                InvokeAsync(StateHasChanged);
            }
            catch (Exception e)
            {
                msg = "抽屉串口连接失败";
                errMsg += "【抽屉串口连接失败】";
                logger.Info($"抽屉串口连接失败【{e.Message}】");
                InvokeAsync(StateHasChanged);
            }
            await Task.Delay(500);
            msg = "开始连接CanBus模块";
            InvokeAsync(StateHasChanged);
            try
            {
                PortUtil.canBusSerial.Open();
                logger.Info($"CanBus模块打开结果【{PortUtil.canBusSerial.IsOpen}】");
                msg = "CanBus模块连接成功";
                InvokeAsync(StateHasChanged);
            }
            catch (Exception e)
            {
                msg = "CanBus模块连接失败";
                errMsg += "【CanBus模块连接失败】";
                logger.Info($"模块连接失败【{e.Message}】"); 
                InvokeAsync(StateHasChanged);
            }
            await Task.Delay(500);
            msg = "开始连接第二个CanBus模块";
            InvokeAsync(StateHasChanged);
            try
            {
                PortUtil.canBusSerialTwo.Open();
                logger.Info($"第二个CanBus模块打开结果【{PortUtil.canBusSerialTwo.IsOpen}】");
                msg = "CanBus模块连接成功";
                InvokeAsync(StateHasChanged);
            }
            catch (Exception e)
            {
                msg = "第二个CanBus模块连接失败";
                logger.Info($"模块连接失败【{e.Message}】"); 
                errMsg += "【第二个CanBus模块连接失败】";
                InvokeAsync(StateHasChanged);
            }
            await Task.Delay(500);
            msg = "开始连接条码扫描模块";
            InvokeAsync(StateHasChanged);
            try
            {
                PortUtil.scanCodeSerial.Open();
                logger.Info($"条码扫描模块打开结果【{PortUtil.scanCodeSerial.IsOpen}】");
                msg = "条码扫描模块连接成功";
                InvokeAsync(StateHasChanged);
            }
            catch (Exception e)
            {
                msg = "条码扫描模块连接失败";
                errMsg += "【条码扫描模块连接失败】"; 
                logger.Info($"条码扫描模块打开结果【{e.Message}】"); 
                InvokeAsync(StateHasChanged);
            }
            if (portSetting.Value.fridgePortExist)
            {
                try
                {
                    PortUtil.fridgeSerial.Open();
                    logger.Info($"冰箱串口打开结果【{PortUtil.fridgeSerial.IsOpen}】");
                    msg = "冰箱串口连接成功";
                    InvokeAsync(StateHasChanged);
                }
                catch (Exception e)
                {
                    msg = "冰箱串口打开失败";
                    errMsg += "【冰箱串口打开失败】";
                    logger.Info($"冰箱串口打开失败【{e.Message}】");
                    InvokeAsync(StateHasChanged);
                }
            }
            globalStateService.isInit = true;
            dialogService.Close();
        }
    }




}
