using Ionic.Zip;
using System;
using System.IO;
using System.Net;

namespace CXMLDecompiler
{
    class GimConv
    {

        static WebClient wc = new WebClient();
        static bool DownloadCompleted = true;
        public static void  DownloadGimConv()
        {
            wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
            wc.DownloadDataCompleted += Wc_DownloadDataCompleted;
            wc.DownloadDataAsync(new Uri("http://e1.dl.playstation.net/e1/downloads/ps3/themes/370/PS3_Custom_Theme_v200.zip")); // Thanks Sony :3
            while (wc.IsBusy || !DownloadCompleted) { };
        }

        private static void Wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            Console.Write("\r\n");
            MemoryStream zipStream = new MemoryStream(e.Result);
            ZipFile zip = ZipFile.Read(zipStream);
            foreach(ZipEntry zEntry in zip.Entries)
            {
                string filename = Path.GetFileName(zEntry.FileName);
                if (filename == "msvcp71.dll" || filename == "msvcr71.dll" || zEntry.FileName.Contains("GimConv"))
                {
                    string outputFilename = zEntry.FileName;
                    if (outputFilename.Contains("GimConv"))
                        outputFilename = outputFilename.Substring(outputFilename.IndexOf("GimConv"));
                    else
                        outputFilename = Path.Combine("GimConv", filename);
                    outputFilename = outputFilename.Replace("/", "\\");
                    Console.WriteLine("Extracting: " + outputFilename);
                    if (zEntry.IsDirectory)
                    {
                        Directory.CreateDirectory(outputFilename);
                        continue;
                    }

                    FileStream fs = File.Open(outputFilename, FileMode.CreateNew, FileAccess.ReadWrite);
                    zEntry.Extract(fs);
                    fs.Close();
                }
            }
            zip.Dispose();
            zipStream.Dispose();

            File.AppendAllText("GimConv\\GimConv.cfg", "option -psvrgba8888 {\r\n    format_endian = little\r\n    format_style = psp\r\n    image_format = rgba8888\r\n    pixel_order = normal\r\n    pixel_channel = rgba\r\n    limit_image_width = 4096\r\n    limit_image_height = 4096\r\n}\r\noption -psvindex4 {\r\n    format_endian = little\r\n    format_style = psp\r\n    image_format = index4\r\n    pixel_order = normal\r\n    pixel_channel = rgba\r\n    limit_image_width = 4096\r\n    limit_image_height = 4096\r\n}\r\noption -psvindex8 {\r\n    format_endian = little\r\n    format_style = psp\r\n    image_format = index8\r\n    pixel_order = normal\r\n    pixel_channel = rgba\r\n    limit_image_width = 4096\r\n    limit_image_height = 4096\r\n}");
            DownloadCompleted = true;
        }

        private static void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            DownloadCompleted = false;
            Console.Write("\rDownloading GimConv Directly from Sony " + e.BytesReceived + "/" + e.TotalBytesToReceive + " - " + e.ProgressPercentage.ToString() + "%");
        }
    }
}
