using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace General
{
    class Tools
    {

	    public static byte[] bmp    = Encoding.ASCII.GetBytes("BM");     // BMP
	    public static byte[] gif    = Encoding.ASCII.GetBytes("GIF");    // GIF
	    public static byte[] png    = new byte[] { 137, 80, 78, 71 };    // PNG
	    public static byte[] tiff   = new byte[] { 73, 73, 42 };         // TIFF
	    public static byte[] tiff2  = new byte[] { 77, 77, 42 };         // TIFF
 	    public static byte[] jpeg   = new byte[] { 255, 216, 255 };      // jpeg
        public static Random rng = new Random(Guid.NewGuid().GetHashCode());

        public static void WriteStringToStream(Stream s, String str)
        {
            Byte[] bytes = Encoding.UTF8.GetBytes(str);
            s.Write(bytes, 0x00, bytes.Length);
        }
        public static void WriteUtf16StringToStream(Stream s, String str)
        {
            Byte[] bytes = Encoding.Unicode.GetBytes(str);
            s.Write(bytes, 0x00, bytes.Length);
        }
        public static MemoryStream ByteToStream(byte[] Array)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(Array, 0x00, Array.Length);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }

        public static byte[] StreamToByte(Stream stream)
        {
            int StreamLen = (int)stream.Length;
            byte[] Bytes = new byte[StreamLen];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(Bytes, 0x00, StreamLen);
            return Bytes;
        }

        public static string GenerateReplacePattern()
        {
            byte[] RandomNumber = new byte[0x20];
            rng.NextBytes(RandomNumber);
            return "{{" + BitConverter.ToString(RandomNumber).Replace("-", "") + "}}";
        }
        public static string GetFileExtension(byte[] Bytes)
        {
            if (bmp.SequenceEqual(Bytes.Take(bmp.Length)))
            {
                return ".bmp";
            }
            else if (gif.SequenceEqual(Bytes.Take(gif.Length)))
            {
                return ".gif";
            }
            else if (png.SequenceEqual(Bytes.Take(png.Length)))
            {
                return ".png";
            }
            else if (tiff.SequenceEqual(Bytes.Take(tiff.Length)))
            {
                return ".tiff";
            }
            else if (tiff2.SequenceEqual(Bytes.Take(tiff2.Length)))
            {
                return ".tiff";
            }
            else if (jpeg.SequenceEqual(Bytes.Take(jpeg.Length)))
            {
                return ".jpg";
            }
            else if (IsZlib(Bytes))
            {
                return ".z";
            }
            else if (IsRcf(Bytes))
            {
                return ".rcs";
            }
            else if (IsDDS(Bytes))
            {
                return ".dds";
            }
            else if (IsVAG(Bytes))
            {
                return ".vag";
            }
            else if (IsGim(Bytes))
            {
                return ".gim";
            }
            else if (!HasBinaryContent(Encoding.UTF8.GetString(Bytes)))
            {
                return ".txt";
            }
            else
            {
                return ".bin";
            }
        }

        public static bool IsVAG(byte[] Bytes)
        {
            MemoryStream Data = ByteToStream(Bytes);
            String header = ReadString(Data, 4);
            if (header.StartsWith("VAG"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsDDS(byte[] Bytes)
        {
            MemoryStream Data = ByteToStream(Bytes);
            String header = ReadString(Data, 4);
            if (header.StartsWith("DDS"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public static bool IsRcf(byte[] Bytes)
        {
            MemoryStream Data = ByteToStream(Bytes);
            String header = ReadString(Data, 5);
            if (header.StartsWith("RCSF"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsGim(byte[] Bytes)
        {
            MemoryStream Data = ByteToStream(Bytes);
            String header = ReadString(Data, 4);
            if (header.StartsWith("MIG"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsZlib(byte[] Bytes)
        {
            if (Bytes[0] == 0x78)
            {
                if (Bytes[1] == 0x01)
                    return true;
                if (Bytes[1] == 0x9C)
                    return true;
                if (Bytes[1] == 0xDA)
                    return true;
            }
            return false;
        }



        public static Bitmap GetBitmap(byte[] BitmapBytes)
        {
            MemoryStream ms = ByteToStream(BitmapBytes);
            Bitmap bmp = new Bitmap(ms);
            ms.Dispose();
            return bmp;
        }

        public static String ReadStringAt(Stream ms, int location)
        {
            long ogPos = ms.Position;
            ms.Seek(location, SeekOrigin.Begin);
            String str = ReadString(ms);
            ms.Seek(ogPos, SeekOrigin.Begin);
            return str;
        }

        public static int ReadIntAt(Stream ms, int location)
        {
            BinaryReader BinReader = new BinaryReader(ms);
            long ogPos = ms.Position;
            ms.Seek(location, SeekOrigin.Begin);
            int i = BinReader.ReadInt32();
            ms.Seek(ogPos, SeekOrigin.Begin);
            return i;
        }
        public static bool HasBinaryContent(string content)
        {
            return content.Any(ch => char.IsControl(ch) && ch != '\r' && ch != '\n');
        }
        public static int ReadInt(Stream ms)
        {
            BinaryReader BinReader = new BinaryReader(ms);
            int i = BinReader.ReadInt32();
            return i;
        }

        public static int ReadBigEndainIntAt(Stream ms, int location)
        {
            long ogPos = ms.Position;
            ms.Seek(location, SeekOrigin.Begin);
            int i = ReadBigEndainInt(ms);
            ms.Seek(ogPos, SeekOrigin.Begin);
            return i;
        }

        public static int ReadBigEndainInt(Stream ms)
        {
            byte[] IntBytes = new byte[4];
            ms.Read(IntBytes, 0x00, 4);
            IntBytes = IntBytes.Reverse().ToArray();
            int i = BitConverter.ToInt32(IntBytes, 0x00);
            return i;
        }
        public static String GetRootFolder(string path)
        {
            while (true)
            {
                string temp = Path.GetDirectoryName(path);
                if (String.IsNullOrEmpty(temp))
                    break;
                path = temp;
            }
            return path;
        }

        public static String GenerateHash(byte[] Data)
        {
            SHA1 sha = SHA1.Create();
            byte[] ShaBytes = sha.ComputeHash(Data);
            return BitConverter.ToString(ShaBytes).Replace("-", "").ToUpper();
        }

        public static string GenerateShortHash(byte[] Data)
        {
            SHA1 sha = SHA1.Create();
            byte[] ShaBytes = sha.ComputeHash(Data);
            return BitConverter.ToInt32(ShaBytes, 0).ToString("X8");
        }

        public static String ReadString(Stream ms, int limit = -1)
        {
            int i = 0xFF;
            int counter = 0;

            MemoryStream StringStream = new MemoryStream();

            do
            {
                i = ms.ReadByte();
                if (i == 0 || counter == limit)
                    break;
                StringStream.WriteByte((byte)i);
                counter += 1;
            }
            while (true);

            byte[] StringData = StringStream.ToArray();

            String str = Encoding.UTF8.GetString(StringData);

            return str;

        }
    }

}
