using System;
using System.IO;
using System.Windows.Forms;

namespace Resolution_Editor
{
    public partial class Main : Form
    {
        private String Mode = String.Empty;
        private Byte[] PadFront = new Byte[0];
        private Int32 FileHeight = 0;
        private Int32 FileWidth = 0;
        private Byte[] PadBack = new Byte[0];

        private Boolean IsArgumentStartup = false;
        private Boolean IsOpen = false;
        private String OpenPath = String.Empty;

        public Main()
        {
            InitializeComponent();

            Text = Properties.Settings.Default.Name;
        }

        private void Main_Load(Object Sender, EventArgs Args)
        {
            AddResolutions();

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                IsArgumentStartup = true;

                OpenPath = Environment.GetCommandLineArgs()[1];

                OpenFile();
            }
        }

        private void Save_Click(Object Sender, EventArgs Args)
        {
            if (!IsOpen) { Program.Message("Please Open Option.mco!"); }
            else
            {
                try
                {
                    File.Delete(OpenPath);

                    Write();
                    OpenFile();

                    Program.Message("Mco Saved{0}Path -> {1}{0}Mode -> {2}{0}Height -> {3}{0}Width -> {4}", Environment.NewLine, OpenPath, CurrentMode_Value.Text, CurrentHeight_Value.Text, CurrentWidth_Value.Text);
                }
                catch
                {
                    Boolean Result = Program.Question("Option.mco Is In Use, Would You Like To Save Somewhere Else?");

                    if(Result == true)
                    {
                        SaveFileDialog SaveDialog = new SaveFileDialog();
                        SaveDialog.Filter = "Mco File|*.mco";
                        SaveDialog.FileName = OpenPath;
                        SaveDialog.ShowDialog();

                        OpenPath = SaveDialog.FileName;

                        Write();
                        OpenFile();

                        Program.Message("Mco Saved{0}Path -> {1}{0}Mode -> {2}{0}Height -> {3}{0}Width -> {4}", Environment.NewLine, OpenPath, CurrentMode_Value.Text, CurrentHeight_Value.Text, CurrentWidth_Value.Text);
                    }
                }
            }
        }

        private void AddResolutions()
        {
            Resolutions.Items.Clear();

            Resolutions.Items.Add("1024x768");
            Resolutions.Items.Add("1024x768 (Wide)");

            Resolutions.Items.Add("1152x864");
            Resolutions.Items.Add("1152x864 (Wide)");

            Resolutions.Items.Add("1280x720");
            Resolutions.Items.Add("1280x720 (Wide)");

            Resolutions.Items.Add("1280x768");
            Resolutions.Items.Add("1280x768 (Wide)");

            Resolutions.Items.Add("1280x800");
            Resolutions.Items.Add("1280x800 (Wide)");

            Resolutions.Items.Add("1280x960");
            Resolutions.Items.Add("1280x960 (Wide)");

            Resolutions.Items.Add("1280x1024");
            Resolutions.Items.Add("1280x1024 (Wide)");

            Resolutions.Items.Add("1360x768");
            Resolutions.Items.Add("1360x768 (Wide)");

            Resolutions.Items.Add("1366x768");
            Resolutions.Items.Add("1366x768 (Wide)");

            Resolutions.Items.Add("1600x900");
            Resolutions.Items.Add("1600x900 (Wide)");

            Resolutions.Items.Add("1600x900");
            Resolutions.Items.Add("1600x900 (Wide)");

            Resolutions.Items.Add("1600x1024");
            Resolutions.Items.Add("1600x1024 (Wide)");

            Resolutions.Items.Add("1680x1050");
            Resolutions.Items.Add("1680x1050 (Wide)");

            Resolutions.Items.Add("1920x1080");
            Resolutions.Items.Add("1920x1080 (Wide)");

            Resolutions.SelectedIndex = 0;
        }

        private void Write()
        {
            using (BinaryWriter Writer = new BinaryWriter(File.Create(OpenPath)))
            {
                String[] WideSplit = Convert.ToString(Resolutions.SelectedItem).Split(' ');
                String[] ResolutionSplit = WideSplit[0].Split('x');

                Writer.Write(PadFront);
                Writer.Write(Convert.ToUInt32(ResolutionSplit[0]));
                Writer.Write(Convert.ToUInt32(ResolutionSplit[1]));
                Writer.Write(PadBack);

                Writer.BaseStream.Seek((Int64)47, SeekOrigin.Begin);
                if (Convert.ToString(Resolutions.SelectedItem).Contains("(Wide)")) { Writer.Write(Convert.ToInt16(1)); }
                else { Writer.Write(Convert.ToInt16(0)); }
            }
        }

        private void Read()
        {
            try
            {
                using (BinaryReader Reader = new BinaryReader(File.Open(OpenPath, FileMode.Open)))
                {
                    PadFront = Reader.ReadBytes(39);
                    FileHeight = Reader.ReadInt32();
                    FileWidth = Reader.ReadInt32();
                    PadBack = Reader.ReadBytes(9);

                    Reader.BaseStream.Seek((Int64)47, SeekOrigin.Begin);
                    Int16 ModeValue = Reader.ReadInt16();

                    if (ModeValue == 0) { Mode = "Window"; }
                    else { Mode = "Full Screen"; }

                    CurrentHeight_Value.Text = Convert.ToString(FileHeight);
                    CurrentWidth_Value.Text = Convert.ToString(FileWidth);
                    CurrentMode_Value.Text = Mode;
                }

                if (!IsArgumentStartup) { IsArgumentStartup = false; }
            }
            catch
            {
                Program.Message("This File Cannot Be Read!");

                if (IsArgumentStartup) { Environment.Exit(0); }
                else
                {
                    IsOpen = false;

                    CurrentMode_Value.Text = "Unknown";
                    CurrentHeight_Value.Text = "Unknown";
                    CurrentWidth_Value.Text = "Unknown";
                }
            }
        }

        private void Open_Click(Object Sender, EventArgs Args)
        {
            OpenFileDialog OpenDialog = new OpenFileDialog();
            OpenDialog.Filter = "Mco File|*.mco";
            OpenDialog.FileName = OpenPath;
            OpenDialog.ShowDialog();

            if (OpenDialog.FileName == "") { }
            else
            {
                OpenPath = OpenDialog.FileName;

                OpenFile();
            }
        }

        private void OpenFile()
        {
            Read();

            IsOpen = true;
        }
    }
}
