// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Novus.Shared.Format;
using Novus.Shared.Types.IssueHandling;
using SelmoStudio.Hmi.Common.SelmoProject;
using SelmoStudio.Hmi.Server.ProcessValue.Services;
using SelmoStudio.Hmi.Server.SelmoHmi.Connection.Service;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Service;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;
using SelmoStudio.Hmi.Server.Shared.Resources;
using System.IO.Compression;
using FileInfo = System.IO.FileInfo;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Diagnose.Pages
{
    public partial class Diagnose
    {
        private WellKnownContentFlexDirection contentFlexDirection = WellKnownContentFlexDirection.Column;
        private bool isServerConnectionIsEstablished => _processValuePollingService.IsConnectionActive();
        private bool islampTestStarted = false;
        private List<Issue> _issues = new List<Issue>();
        private HmiSettings _hmiSettings => _projectService.GetHmiSettings();

        [Inject]
        private IProjectService _projectService { get; set; }

        [Inject]
        private IProcessValuePollingService _processValuePollingService { get; set; }

        [Inject]
        private IConnectionService _connectionService { get; set; }

        private string GetConnectionStatus()
        {
            return $"{@_localizer[nameof(HmiResource.Status)]} : " +
                $"{(isServerConnectionIsEstablished ? _localizer[nameof(HmiResource.Connected)] : _localizer[nameof(HmiResource.Disconnected)])}";
        }

        private string GetLampTestStatus()
        {
            return $"{@_localizer[nameof(HmiResource.Status)]} : {(islampTestStarted ? _localizer[nameof(HmiResource.Started)] : _localizer[nameof(HmiResource.NotStarted)])}";
        }

        private void Connect()
        {
            _connectionService.Connect();
        }

        private Task StartLampTest()
        {
            return Task.CompletedTask;
        }

        private byte[] GetDiagnosePackageZipFile()
        {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var loggingPath = Path.Combine(path, "SelmoHmi", "Logging");

            var logFiles = Directory.GetFiles(loggingPath)
             .Select(x => new FileInfo(x))
             .OrderByDescending(x => x.LastWriteTime)
             .Take(3)
             .ToArray();

            var zipContent = new MemoryStream();
            var archive = new ZipArchive(zipContent, ZipArchiveMode.Update);

            foreach (var logFile in logFiles)
            {
                var prefix = "log_copy_";
                var newFileName = $"{prefix}{logFile.Name}";
                var fullNameString = System.IO.Path.Combine(logFile.Directory.FullName, newFileName);
                File.Delete(fullNameString);
                File.Copy(logFile.FullName, fullNameString);
                archive.CreateEntryFromFile(fullNameString, logFile.Name);
                File.Delete(fullNameString);
            }

            archive.Dispose();
            return zipContent.ToArray();
        }

        private string GetFilename()
        {
            return $"Diagnosepack_{DateTimeFormatter.FileFormatToSecond(DateTimeOffset.Now)}";
        }
    }
}