// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using SelmoStudio.Hmi.Server.Service;
using SelmoStudio.Hmi.Server.Shared.Components.Chart;

namespace SelmoStudio.Hmi.Server.SelmoHmi.History_Data.Pages
{
    public partial class HistoryDataView
    {
        private DateTime? _selectedDate = DateTime.Today;

        private TimeSpan? _timeFrom = new TimeSpan();
        private TimeSpan? _timeTo = new TimeSpan();
        private bool _disabledFrom = false;
        private bool _disabledTo = false;

        private LineChartJs? _chart;

        private DiagramConfiguration _configuration = new()
        {
            Title = "Historical Data",
            XLabel = "Time",
            YLabel = "Value"
        };

        private IReadOnlyList<GraphConfiguration> _graphs = Array.Empty<GraphConfiguration>();

        private DateTimeOffset? _dateTimeOffsetFrom;
        private DateTimeOffset? _dateTimeOffsetTo;

        [Inject]
        private IHistoryDataService _historyDataService { get; set; }

        private async Task LoadData()
        {
            if (_selectedDate.HasValue)
            {
                var date = _selectedDate.Value.Date;

                if (_timeFrom.HasValue)
                {
                    var dateTimeFrom = date.Add(_timeFrom.Value);
                    _dateTimeOffsetFrom = new DateTimeOffset(dateTimeFrom, TimeZoneInfo.Local.GetUtcOffset(dateTimeFrom));
                }
                else
                {
                    _dateTimeOffsetFrom = null;
                }

                if (_timeTo.HasValue)
                {
                    var dateTimeTo = date.Add(_timeTo.Value);
                    _dateTimeOffsetTo = new DateTimeOffset(dateTimeTo, TimeZoneInfo.Local.GetUtcOffset(dateTimeTo));
                }
                else
                {
                    _dateTimeOffsetTo = null;
                }
            }
            else
            {
                _dateTimeOffsetFrom = null;
                _dateTimeOffsetTo = null;
            }

            if (_dateTimeOffsetFrom != null && _dateTimeOffsetTo != null)
            {
                var dataSets = await _historyDataService.GetData(_dateTimeOffsetFrom.Value, _dateTimeOffsetTo.Value);

                await DrawGraph(dataSets);
                await InvokeAsync(() => StateHasChanged());
            }
        }

        private async Task DrawGraph(List<GraphDataSet> dataSets)
        {
            if (_chart != null)
            {
                _chart?.RemoveAllGraph();
            }
            var graphs = new List<GraphConfiguration>();
            for (int i = 0; i < dataSets.Count; i++)
            {
                var config = CreateConfig(dataSets[i], Colors.Palette1[i]);
                graphs.Add(config);
                if (_chart != null)
                {
                    await _chart?.AddGraph(config);
                }
            }
        }

        private GraphConfiguration CreateConfig(GraphDataSet dataSet, string color)
        {
            var config = new GraphConfiguration
            {
                Color = color,
                Data = dataSet.Data,
                Id = Guid.NewGuid(),
                Label = dataSet.Name
            };

            return config;
        }

        private async Task ResetXScale()
        {
            await _chart.UpdateXScale(DateTimeOffset.MinValue, DateTimeOffset.MaxValue);
        }
    }
}