using System;
using System.IO;
using System.Windows.Controls;

namespace Fiesta_Client.Panels
{
    public partial class Resolution : UserControl
    {
        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 MainWindow MainHandle;

        public Resolution(MainWindow MW)
        {
            InitializeComponent();

            MainHandle = MW;

            AddResolutions();

            Read();
        }

        private void Set_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Write();

            MainHandle.ShowMain(false);
        }

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

            Resolutions.Items.Add("1024x768");
            Resolutions.Items.Add("1024x768 (Full Screen)");

            Resolutions.Items.Add("1152x864");
            Resolutions.Items.Add("1152x864 (Full Screen)");

            Resolutions.Items.Add("1280x720");
            Resolutions.Items.Add("1280x720 (Full Screen)");

            Resolutions.Items.Add("1280x768");
            Resolutions.Items.Add("1280x768 (Full Screen)");

            Resolutions.Items.Add("1280x800");
            Resolutions.Items.Add("1280x800 (Full Screen)");

            Resolutions.Items.Add("1280x960");
            Resolutions.Items.Add("1280x960 (Full Screen)");

            Resolutions.Items.Add("1280x1024");
            Resolutions.Items.Add("1280x1024 (Full Screen)");

            Resolutions.Items.Add("1360x768");
            Resolutions.Items.Add("1360x768 (Full Screen)");

            Resolutions.Items.Add("1366x768");
            Resolutions.Items.Add("1366x768 (Full Screen)");

            Resolutions.Items.Add("1600x900");
            Resolutions.Items.Add("1600x900 (Full Screen)");

            Resolutions.Items.Add("1600x900");
            Resolutions.Items.Add("1600x900 (Full Screen)");

            Resolutions.Items.Add("1600x1024");
            Resolutions.Items.Add("1600x1024 (Full Screen)");

            Resolutions.Items.Add("1680x1050");
            Resolutions.Items.Add("1680x1050 (Full Screen)");

            Resolutions.Items.Add("1920x1080");
            Resolutions.Items.Add("1920x1080 (Full Screen)");

            Resolutions.SelectedIndex = 0;
        }

        private void Read()
        {
            using (BinaryReader Reader = new BinaryReader(File.Open(String.Format("{0}\\ressystem\\Option.mco", AppDomain.CurrentDomain.BaseDirectory), 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.Content = Convert.ToString(FileHeight);
                CurrentWidth_Value.Content = Convert.ToString(FileWidth);
                CurrentMode_Value.Content = Mode;
            }
        }

        private void Write()
        {
            using (BinaryWriter Writer = new BinaryWriter(File.Create(String.Format("{0}\\ressystem\\Option.mco", AppDomain.CurrentDomain.BaseDirectory))))
            {
                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("(Full Screen)")) { Writer.Write(Convert.ToInt16(1)); }
                else { Writer.Write(Convert.ToInt16(0)); }
            }
        }
    }
}
