@page "/client"
@using DevExpress.Blazor
@using OpenCvSharp
@using squirrowse.core
@using squirrowse.web.Cognitive
@using squirrowse.web.Common
@using squirrowse.web.Data

<h1>live</h1>

<img src="@_imgSrc" />

<img src="@_tempcogni" />

<div style="padding-top: 10px">
    <button id="Save" class="btn btn-success btn-sm" @onclick="() => GetDataFromAzure()">
        Azure cognitive
    </button>
</div>
<div style="padding-top: 10px">
</div>
<div class="card demo-card ju" style="width: 18rem;">
    <ul class="list-group list-group-flush bg-white text-dark" style="border: none">
        <li class="list-group-item d-flex justify-content-between" style="background-color: transparent; color: inherit;">
            <DxCheckBox CheckType="CheckType.Switch" LabelPosition="@LabelPosition.Left" style="width: 100%" Checked="@fetchDataFromAzure" CheckedChanged="@Update(v => fetchDataFromAzure = v)">Fetch Data from azure</DxCheckBox>
        </li>
        <li class="list-group-item d-flex justify-content-between" style="background-color: transparent; color: inherit;">
            <DxCheckBox CheckType="CheckType.Switch" LabelPosition="@LabelPosition.Left" style="width: 100%" Checked="@filter.Face" CheckedChanged="@Update(v => filter.Face = v)">Face</DxCheckBox>
        </li>
        <li class="list-group-item d-flex justify-content-between" style="background-color: transparent; color: inherit;">
            <DxCheckBox CheckType="CheckType.Switch" LabelPosition="@LabelPosition.Left" style="width: 100%" Checked="@filter.UpperBody" CheckedChanged="@Update(v => filter.UpperBody = v)">Upper Body</DxCheckBox>
        </li>
        <li class="list-group-item d-flex justify-content-between" style="background-color: transparent; color: inherit;">
            <DxCheckBox CheckType="CheckType.Switch" LabelPosition="@LabelPosition.Left" style="width: 100%" Checked="@filter.LowerBody" CheckedChanged="@Update(v => filter.LowerBody = v)">Lower Body</DxCheckBox>
        </li>
    </ul>
</div>
@inject SaveVideo save
@inject IAzureCV azureCv
@inject VideoQ Video
@inject Detection detect

@code { private readonly Filters filter = new Filters(false, false, false);
            private string _imgSrc;
            private string _tempcogni;
            private byte[] buffer;
            private bool fetchDataFromAzure = true;

            private readonly Frame Frametest = new Frame();

            private Mat tempory = new Mat();

            public Action<bool> Update(Action<bool> set)
            {
                return v =>
                {
                    set(v);
                    InvokeAsync(StateHasChanged);
                };
            }

            protected override async Task OnInitializedAsync()
            {
                using var temp = new Mat(480, 640, MatType.CV_8UC3, new Scalar(0, 0, 100));

                Cv2.PutText(temp, "No clients".ToUpper(),
                    new Point(240, 320),
                    HersheyFonts.HersheyComplex, 0.9f, Scalar.Yellow);
                var base64T = Convert.ToBase64String(temp.ToBytes());


                while (true)
                {

                    try
                    {
                        using var frame = Video.getFrame();
                        tempory = Frametest.CopyMat(frame);
                        var face = detect.DetectTypes(frame, filter);
                        buffer = face.ToBytes();
                        var base64 = Convert.ToBase64String(buffer);
                        _imgSrc = $"data:image/gif;base64,{base64}";
                        Cv2.WaitKey(1);
                        await Task.Delay(1000 / 33);

                        StateHasChanged();
                    }
                    catch (Exception e)
                    {
                        _imgSrc = $"data:image/gif;base64,{base64T}";
                        await Task.Delay(1000 / 33);

                        StateHasChanged();
                    }
                }

            }

            private async Task GetDataFromAzure()
            {
                while (fetchDataFromAzure)
                {
                    var azrespons = await azureCv.GetObjects(tempory);
                    var azureFrame = azureCv.DrawResults(tempory, azrespons);
                    var b_tempcogni = Convert.ToBase64String(azureFrame.Result.ToBytes());
                    _tempcogni = $"data:image/gif;base64,{b_tempcogni}";
                    await Task.Delay(TimeSpan.FromSeconds(5));
                    StateHasChanged();
                }
            } }