using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace SHBD
{
    public class SHBDFile
    {
        public static void ConvertToPNG(String LoadPath)
        {
            if (File.Exists(String.Concat(LoadPath.TrimEnd("shbd".ToCharArray()), "png"))) { File.Move(String.Concat(LoadPath.TrimEnd("shbd".ToCharArray()), "png"), String.Format("C:\\SHNBackups\\{0}{1}.png", Path.GetFileNameWithoutExtension(String.Concat(LoadPath.TrimEnd("shbd".ToCharArray()), "png")), DateTime.Now.ToString("dd-MM-yyyy.hh-mm-ss-fff"))); }

            Bitmap ConvertedPNG;

            using (BinaryReader SHBDReader = new BinaryReader(File.Open(LoadPath, FileMode.Open, FileAccess.Read)))
            {
                UInt32 Width = SHBDReader.ReadUInt32();
                UInt32 Height = SHBDReader.ReadUInt32();

                ConvertedPNG = new Bitmap((Int32)Width * 8, (Int32)Height);

                for (Int32 Y = (Int32)Height - 1; Y >= 0; Y--)
                {
                    for (Int32 X = 0; X < Width; X++)
                    {
                        Byte Code = SHBDReader.ReadByte();

                        for (Int32 Counter = 0; Counter < 8; Counter++) { ConvertedPNG.SetPixel((X * 8) + Counter, Y, GetPixelColor(Code)); }
                    }
                }
            }

            using (FileStream PNGSaver = File.Create(String.Concat(LoadPath.TrimEnd("shbd".ToCharArray()), "png"))) { ConvertedPNG.Save(PNGSaver, ImageFormat.Png); }
        }

        private static Color GetPixelColor(Byte Code) { return Code >= 175 ? Color.FromArgb(8421504 * Code * -1) : Color.FromArgb(8421504 * Code); }

        public static void ConvertToSHBD(String LoadPath)
        {
            if (File.Exists(String.Concat(LoadPath.TrimEnd("png".ToCharArray()), "shbd"))) { File.Move(String.Concat(LoadPath.TrimEnd("png".ToCharArray()), "shbd"), String.Format("C:\\SHNBackups\\{0}{1}.shbd", Path.GetFileNameWithoutExtension(String.Concat(LoadPath.TrimEnd("png".ToCharArray()), "shbd")), DateTime.Now.ToString("dd-MM-yyyy.hh-mm-ss-fff"))); }

            Bitmap LoadedSHBD = new Bitmap(LoadPath);

            LoadedSHBD = ResizeImage(LoadedSHBD, new Size() { Height = LoadedSHBD.Height / 8, Width = LoadedSHBD.Width / 8 });

            using (FileStream PNGSaver = File.Create(@"C:\Users\Stormy\Desktop\RouN2.png")) { LoadedSHBD.Save(PNGSaver, ImageFormat.Png); }

            Bitmap Test = new Bitmap(2048, 256);

            for (Int32 Counter = 0; Counter < 2048; Counter = Counter + 256)
            {
                Graphics test1 = Graphics.FromImage(Test);
                test1.DrawImage(LoadedSHBD, new Point(Counter, 0));
            }

            using (FileStream PNGSaver = File.Create(@"C:\Users\Stormy\Desktop\RouN3.png")) { Test.Save(PNGSaver, ImageFormat.Png); }
        }

        public static void CopyRegionIntoImage(Bitmap srcBitmap, Rectangle srcRegion, ref Bitmap destBitmap, Rectangle destRegion)
        {
            using (Graphics grD = Graphics.FromImage(destBitmap))
            {
                grD.DrawImage(srcBitmap, destRegion, srcRegion, GraphicsUnit.Pixel);
            }
        }

        public static Bitmap ResizeImage(Bitmap imgToResize, Size size)
        {
            try
            {
                Bitmap b = new Bitmap(size.Width, size.Height);
                using (Graphics g = Graphics.FromImage((Image)b))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
                }
                return b;
            }
            catch
            {
                Console.WriteLine("Bitmap could not be resized");
                return imgToResize;
            }
        }

        private static Int32 GetPixelCode(Color C)
        {
            return C.ToArgb() >= 175 ? (C.ToArgb() * -1) / 8421504 : C.ToArgb() / 8421504;
        }
    }
}
