@page "/manage/setting/Fridge"
@using MasaBlazorApp3.Util
@using Newtonsoft.Json.Linq
@using log4net
@layout SettingLayout
<div class="main">
    <div class="top-row px-4">
        <RadzenButton Icon="account_circle" Style="margin-left: auto;" Click="@SaveMethod">
            <ChildContent>
                保存设置
            </ChildContent>
        </RadzenButton>
    </div>

    <div class="content px-4">
        <RadzenCard class="rz-my-6">
            <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Start" Wrap="FlexWrap.Wrap">
                <RadzenStack Orientation="Orientation.Vertical" Gap="4px">
                    冰箱温度区间
                    <RadzenTextBox Placeholder="2-8" Value="@temperatureRange" Change="@(args=>temperatureRange=args)"></RadzenTextBox>
                </RadzenStack>
                <RadzenStack Orientation="Orientation.Vertical" Gap="4px">
                    冰箱状态
                    <RadzenRadioButtonList Value=@fridgeStateValue TValue="int" Change=@(args => fridgeStateValue = args)>
                        <Items>
                            <RadzenRadioButtonListItem Text="开" Value="0" />
                            <RadzenRadioButtonListItem Text="关" Value="1" />
                        </Items>
                    </RadzenRadioButtonList>
                </RadzenStack>
                <RadzenStack Orientation="Orientation.Vertical" Gap="4px">
                    报警状态
                    <RadzenRadioButtonList Value="@alertStateValue" TValue="int" Change="@(args=>alertStateValue=args)">
                        <Items>
                            <RadzenRadioButtonListItem Text="开" Value="0"/>
                            <RadzenRadioButtonListItem Text="关" Value="1"/>
                        </Items>
                    </RadzenRadioButtonList>
                </RadzenStack>
            </RadzenStack>
        </RadzenCard>
    </div>
</div>


@inject Microsoft.Extensions.Options.IOptions<Pojo.Config.fridgeConfig> setting;
@inject PortUtil port;
@inject NotificationService _message

@code {
    private readonly ILog logger = LogManager.GetLogger(typeof(LoginDialog));
    int fridgeStateValue = 1;
    int alertStateValue = 1;
    string temperatureRange = "2-8";
    protected override void OnInitialized()
    {
        fridgeStateValue = setting.Value.fridgeState;
        alertStateValue = setting.Value.alertState;
        temperatureRange = setting.Value.temperatureRange;
        base.OnInitialized();
    }
    //保存
    async Task SaveMethod()
    {
        // 获取当前工作目录
        string currentDirectory = Directory.GetCurrentDirectory();
        // setting.Value.fridgeState = fridgeStateValue;
        // setting.Value.alertState = alertStateValue;
        // setting.Value.temperatureRange = temperatureRange;
        string filePath =Path.Combine(currentDirectory, "appsettings.json");
        string jsonString= File.ReadAllText(filePath);
        var jsonNode = JObject.Parse(jsonString);
        jsonNode["fridge"]["temperatureRange"] = temperatureRange;
        jsonNode["fridge"]["fridgeState"]= fridgeStateValue;
        jsonNode["fridge"]["alertState"] = alertStateValue;
        var options = new JsonSerializerOptions { WriteIndented = true };
        File.WriteAllText(filePath, jsonNode.ToString(Newtonsoft.Json.Formatting.Indented));
        if (fridgeStateValue==0)
        {
            await port.FridegOpen(1);
        }
        else
        {
            await port.FridgeOff(1);
        }
        if (alertStateValue==0)
        {
            await port.FridgeAlarmOn(1);
        }
        else
        {
            await port.FridgeAlarmOff(1);
        }
        string[] newRange = temperatureRange.Split('-');
        if(newRange.Length>=2)
        {
            string[] range= setting.Value.temperatureRange.Split('-');
            bool bMix = float.TryParse(newRange[0], out float Min);
            bool bMax = float.TryParse(newRange[1], out float Max);
            if (bMix && bMax)
            {
                if (range == null || range[0] == null||(range != null && range[0] != null && range[0] != newRange[0]))
                {
                    //设定冰箱温度最小值
                    await port.FridgeMinSetting(Min, 1);
                }
                if (range == null || range[0] == null || (range != null && range[1] != null && range[1] != newRange[1]))
                {
                    //设定冰箱温度最大值
                    await port.FridgeMaxSetting(Max, 1);
                }
            }
        }
        _message.Notify(
             new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "提示", Detail = $"保存成功", Duration = 4000 }
         );
        logger.Info($"修改冰箱设置");
    }
}
