using Seenginx.Models;
using Seenginx.Services.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Seenginx.Services
{
	public class NginxService : INginxService
	{
		private readonly ConfigPaths ConfigPaths;

		public NginxService(ConfigPaths configPaths)
		{
			ConfigPaths = configPaths;
		}

		public async Task<Result<ConfigFile>> AddFileAsync(NewFileForm newFileForm)
		{
			var addResult = new Result<ConfigFile>();
			addResult.Data.Name = newFileForm.Name;
			return addResult;
		}

		public async Task<IEnumerable<ConfigFile>> GetFilesAsync()
		{
			var rootConfigs = Directory.GetFiles(ConfigPaths.NginxPath, "*.conf");
			var confdConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "conf.d"), "*.conf");
			var sitesAvailableConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "sites-available"), "*.conf");
			var sitesEnabledConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "sites-enabled"), "*.conf");

			var rootConfigFiles = rootConfigs.Select(fp =>
			{
				var fileName = Path.GetFileName(fp);
				var configFile = new ConfigFile();
				configFile.CanBeDeleted = fileName != "nginx.conf";
				configFile.Folder = "/";
				configFile.LastUpdated = File.GetLastWriteTime(fp);
				configFile.Name = fileName;
				configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, configFile.Name);
				configFile.Body = File.ReadAllText(fp);
				return configFile;
			});
			var confdConfigFiles = confdConfigs.Select(fp =>
			{
				var fileName = Path.GetFileName(fp);
				var configFile = new ConfigFile();
				configFile.CanBeDeleted = true;
				configFile.Folder = "/conf.d";
				configFile.LastUpdated = File.GetLastWriteTime(fp);
				configFile.Name = fileName;
				configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, configFile.Name);
				configFile.Body = File.ReadAllText(fp);
				return configFile;
			});
			var sitesAvailableConfigFiles = sitesAvailableConfigs.Select(fp =>
			{
				var fileName = Path.GetFileName(fp);
				var configFile = new ConfigFile();
				configFile.CanBeDeleted = true;
				configFile.Folder = "/sites-available";
				configFile.LastUpdated = File.GetLastWriteTime(fp);
				configFile.Name = fileName;
				configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, configFile.Name);
				configFile.Body = File.ReadAllText(fp);
				return configFile;
			});
			var sitesEnabledConfigFiles = sitesEnabledConfigs.Select(fp =>
			{
				var fileName = Path.GetFileName(fp);
				var configFile = new ConfigFile();
				configFile.CanBeDeleted = true;
				configFile.Folder = "/sites-enabled";
				configFile.LastUpdated = File.GetLastWriteTime(fp);
				configFile.Name = fileName;
				configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, configFile.Name);
				configFile.Body = File.ReadAllText(fp);
				return configFile;
			});
			var finalList = new List<ConfigFile>();
			finalList.AddRange(rootConfigFiles);
			finalList.AddRange(confdConfigFiles);
			finalList.AddRange(sitesAvailableConfigFiles);
			finalList.AddRange(sitesEnabledConfigFiles);

			return finalList;
		}

		public async Task<Result<string>> TestNginxConfigurations(ConfigFile configFile)
		{
			var result = new Result<string>();
			result.SetData("Uhu");
			return result;
		}
	}
}
