using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;

namespace QCLVR;

internal class Compression
{
	public static void DecompressCXML(FileInfo compressedFile, string FileName)
	{
		Stopwatch stopwatch = Stopwatch.StartNew();
		FileStream fileStream = compressedFile.OpenRead();
		using (FileStream destination = new FileStream(FileName, FileMode.Create))
		{
			using GZipStream gZipStream = new GZipStream(fileStream, CompressionMode.Decompress);
			gZipStream.CopyTo(destination);
		}
		stopwatch.Stop();
		Console.WriteLine("Decompressing: {0}", stopwatch.ElapsedMilliseconds);
		fileStream.Close();
	}

	public static void DecompressCXML(string inputFile, string outFile)
	{
		DecompressCXML(new FileInfo(inputFile), outFile);
	}

	public static void CompressXML(FileInfo XMLFile, string FileName)
	{
		FileStream fileStream = XMLFile.OpenRead();
		using (FileStream stream = File.Create(FileName))
		{
			using GZipStream destination = new GZipStream(stream, CompressionMode.Compress);
			fileStream.CopyTo(destination);
		}
		fileStream.Close();
	}

	public static void CompressXML(string inputFile, string outFile)
	{
		CompressXML(new FileInfo(inputFile), outFile);
	}

	public static void transform(string input, bool compress)
	{
		if (Directory.Exists(input))
		{
			if (compress)
			{
				string[] files = Directory.GetFiles(input, "*.xml");
				foreach (string text in files)
				{
					CompressXML(text, Path.GetDirectoryName(text) + "\\" + Path.GetFileNameWithoutExtension(text) + ".cxml");
					File.Delete(text);
				}
			}
			else
			{
				string[] files = Directory.GetFiles(input, "*.cxml");
				foreach (string text2 in files)
				{
					DecompressCXML(text2, Path.GetDirectoryName(text2) + "\\" + Path.GetFileNameWithoutExtension(text2) + ".xml");
					File.Delete(text2);
				}
			}
		}
		else
		{
			if (!File.Exists(input))
			{
				throw new NotImplementedException();
			}
			if (compress)
			{
				CompressXML(input, Path.GetDirectoryName(input) + "\\" + Path.GetFileNameWithoutExtension(input) + ".cxml");
				File.Delete(input);
			}
			else
			{
				DecompressCXML(input, Path.GetDirectoryName(input) + "\\" + Path.GetFileNameWithoutExtension(input) + ".cxml");
				File.Delete(input);
			}
		}
	}
}
