using Blazorise;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Seenginx.Components;
using Seenginx.Models;
using Seenginx.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Seenginx.Pages
{
	public class NginxBase : ComponentBase
	{
		[Inject]
		public INginxService NginxService { get; set; }
		[Inject]
		public IFileManager FileService { get; set; }

		public string InputSearch { get; set; }

		public List<ConfigFile> ConfigFiles { get; set; } = new List<ConfigFile>();
		public ConfigFile SelectedFile { get; set; }
		public List<string> Filters { get; set; } = new List<string>();
		public List<int> FilteredOutFiles { get; set; } = new List<int>();
		public Modal GeneralNotificationModal { get; set; } = new Modal();
		public GeneralNotificationSettings GeneralNotificationSettings { get; set; } = null;

		public Dictionary<string, string> FilterFolder { get; set; } = new Dictionary<string, string>();

		protected override async Task OnInitializedAsync()
		{
			ConfigFiles.AddRange(await NginxService.GetFilesAsync());
			Filters.AddRange(new List<string> { "All", "Root", "Conf.d", "Available", "Enabled" });
			FilterFolder.Add("All", null);
			FilterFolder.Add("Root", "/");
			FilterFolder.Add("Conf.d", "/conf.d");
			FilterFolder.Add("Available", "/sites-available");
			FilterFolder.Add("Enabled", "/sites-enabled");
			await base.OnInitializedAsync();
		}

		public async Task SelectedFileChanged(ConfigFile configFile)
		{
			SelectedFile = configFile;
		}


		protected Modal AddFileModal { get; set; }
		public Result<ConfigFile> AddFileResult { get; set; }
		public async Task ShowAddFileModal()
		{
			AddFileModal.Show();
		}
		public NewFileForm NewFileForm { get; set; } = new NewFileForm();
		public async Task AddFileAsync()
		{
			AddFileResult = await NginxService.AddFileAsync(NewFileForm);
			if (AddFileResult.AllOk)
				ConfigFiles.Add(AddFileResult.Data);
			else
			{
				GeneralNotificationSettings = new GeneralNotificationSettings
				{
					ButtonColor = Color.Danger,
					TitleClass = "mdi-error",
					Title = "Failure",
					Text = TestResult.ErrorMessage
				};
				GeneralNotificationModal.Show();
			}
		}

		public Result<ConfigFile> SaveUpdateDraftResult { get; set; }
		public async Task SaveUpdateDraftFileAsync()
		{
			SaveUpdateDraftResult = await FileService.SaveUpdateDraftFileAsync(SelectedFile);
		}

		public Result<ConfigFile> SaveUpdateResult { get; set; }
		public async Task SaveUpdateFileAsync()
		{
			SaveUpdateResult = await FileService.SaveUpdateFileAsync(SelectedFile);
		}

		public Result<string> TestResult { get; set; }
		public async Task TestConfiguration()
		{
			TestResult = await NginxService.TestNginxConfigurations(SelectedFile);
			if (TestResult.AllOk)
				GeneralNotificationSettings = new GeneralNotificationSettings
				{
					ButtonColor = Color.Success,
					TitleClass = "mdi-success",
					Title = "Success",
					Text = $"Test of config file '{SelectedFile.Name}' completed with success."
				};
			else
				GeneralNotificationSettings = new GeneralNotificationSettings
				{
					ButtonColor = Color.Danger,
					TitleClass = "mdi-error",
					Title = "Failure",
					Text = TestResult.ErrorMessage
				};
			GeneralNotificationModal.Show();
		}

		public Result<bool> DeleteResult { get; set; }
		public void DeleteFile()
		{
			DeleteResult = FileService.DeleteFile(SelectedFile);
		}

		public void CloseModal(Modal modal)
		{
			modal.Hide();
		}
	}
}
