using Blazored.Modal;
using Blazored.Modal.Services;
using Microsoft.AspNetCore.Components;
using Seenginx.Models;
using Seenginx.Services;
using Seenginx.Shared;
using Seenginx.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Seenginx.Pages
{
	public class SystemdBase : ComponentBase
	{
		[Inject] public ISystemDService SystemDService { get; set; }
		[CascadingParameter] public IModalService Modal { get; set; }

		public string InputSearch { get; set; }

		public List<ConfigFile> ConfigFiles { get; set; } = new List<ConfigFile>();
		public ConfigFile SelectedFile { get; set; }
		public List<int> FilteredOutFiles { get; set; } = new List<int>();
		public NotificationSettings GeneralNotificationSettings { get; set; } = null;
		public List<Template> Templates { get; set; } = new List<Template>();

		protected override async Task OnParametersSetAsync()
		{
			try
			{
				ConfigFiles.AddRange(await SystemDService.GetFilesAsync());
				Templates.AddRange(await SystemDService.GetTemplates());
			}
			catch (Exception ex)
			{
				throw ex;
			}
			await base.OnParametersSetAsync();
		}

		public void SelectedFileChanged(ConfigFile configFile)
		{
			SelectedFile = configFile;
		}

		public async Task AddFileModal()
		{
			var parameters = new ModalParameters();
			parameters.Add(nameof(Templates), Templates);

			var resultAwait = Modal.Show<SystemdConfigForm>(string.Empty, parameters);
			var result = await resultAwait.Result;
			if (result.Cancelled) return;


			var validationResult = await SystemDService.ValidateForAddFileAsync((NewFileForm)result.Data);

			if (!validationResult.AllOk)
			{
				var validationPopupParameters = new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage);
				var validationPopup = Modal.Show<GenericPopup>(string.Empty, validationPopupParameters);
				await validationPopup.Result;
				validationPopup.Close();
				return;
			}

			var addFileResult = await SystemDService.AddFileAsync((NewFileForm)result.Data);
			if (SelectedFile != null)
				SelectedFile.Deselect();
			ConfigFiles.Add(addFileResult.Data);
			ConfigFiles = ConfigFiles.OrderBy(cf => cf.Name).ToList();
			SelectedFile = ConfigFiles.Find(cf => cf.Name == addFileResult.Data.Name);
			SelectedFile.Select();
		}

		public async Task RenameFileModal()
		{
			var parameters = new ModalParameters();
			parameters.Add(nameof(RenameForm.Name), SelectedFile.Name);

			var resultAwait = Modal.Show<RenameForm>(string.Empty, parameters);
			var result = await resultAwait.Result;
			if (result.Cancelled) return;

			var validationResult = await SystemDService.ValidateForRenameFileAsync(ConfigFiles, SelectedFile, result.Data.ToString());
			if (!validationResult.AllOk)
			{
				var popupAwait = Modal.Show<GenericPopup>(string.Empty, new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage));
				await popupAwait.Result;
				return;
			}

			var renameResult = await SystemDService.RenameFileAsync(SelectedFile, $"{result.Data}.service");
			if (!renameResult.AllOk)
			{
				var popupAwait = Modal.Show<GenericPopup>(string.Empty, new ModalParameters().Setup(PopupType.Ok, renameResult.ErrorMessage));
				await popupAwait.Result;
				return;
			}

			SelectedFile.Name = result.Data.ToString();
			SelectedFile.FullPath = renameResult.Data.FullPath;
		}

		public async Task SaveFileAsync()
		{
			var validationResult = await SystemDService.ValidateForSaveFileAsync(SelectedFile);

			if (!validationResult.AllOk)
			{
				await Modal.Show<GenericPopup>(string.Empty, new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage)).Result;
				return;
			}

			var saveResult = await SystemDService.SaveFileAsync(SelectedFile);

			if (!saveResult.AllOk)
			{
				await Modal.Show<GenericPopup>(string.Empty, new ModalParameters().Setup(PopupType.Ok, saveResult.ErrorMessage)).Result;
				return;
			}
		}

		public async Task SaveDraftFileAsync()
		{
			//var validationResult = await NginxService.ValidateForSaveDraftFileAsync(SelectedFile);

			//if (!validationResult.AllOk)
			//{
			//	await Modal.Show<GenericPopup>(string.Empty, new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage)).Result;
			//	return;
			//}

			var saveDraftResult = await SystemDService.SaveDraftFileAsync(SelectedFile);

			if (!saveDraftResult.AllOk)
			{
				await Modal.Show<GenericPopup>(string.Empty, new ModalParameters().Setup(PopupType.Ok, saveDraftResult.ErrorMessage)).Result;
				return;
			}
		}

		public async Task DeleteFile()
		{
			var parameters = new ModalParameters().Setup(PopupType.YesNo, $"Are you sure you want to delete '{SelectedFile.Name}'?");

			var resultAwait = Modal.Show<GenericPopup>(string.Empty, parameters);
			var result = await resultAwait.Result;
			if ((PopupAnswer)result.Data == PopupAnswer.No) return;

			var deleteFileResult = await SystemDService.DeleteFileAsync(SelectedFile);

			if (!deleteFileResult.AllOk)
			{
				var errorParameters = new ModalParameters().Setup(PopupType.Ok, $"Something went wrong, here's the error message: '{deleteFileResult.ErrorMessage}'?");
				var errorReportModalAwait = Modal.Show<GenericPopup>(string.Empty, errorParameters);
				await errorReportModalAwait.Result;
				return;
			}

			ConfigFiles.Remove(SelectedFile);
			SelectedFile = null;
		}
	}
}
