using System;
using System.Drawing;
using System.Windows.Forms;
using NotepadEnhanced.AppState;

namespace NotepadEnhanced.GUI.Forms
{
    /// <summary>
    /// Specifies the different reading modes
    /// </summary>
    public enum ReadingMode
    {
        /// <summary>
        /// Reading will be done in a normal windowed fashon
        /// </summary>
        Normal,
        /// <summary>
        /// Reading will be adapted for a written script
        /// </summary>
        Script,
        /// <summary>
        /// Reading will be fullscreen
        /// </summary>
        FullScreen
    };

    public partial class ReadingForm : Form
    {
        //private readonly MainForm _frmMain;

        public ReadingForm(string text)
        {
            InitializeComponent();
            lblScrolling.SetLabelText(text);
            LoadSettingsAndDefaults();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.KeyCode.Equals(Keys.Escape))
            {
                if (this.WindowState == FormWindowState.Maximized)
                    this.WindowState = FormWindowState.Normal;
                else
                    this.Close();
            }
            else if (e.KeyCode.Equals(Keys.Left) || e.KeyCode.Equals(Keys.Right) ||
                     e.KeyCode.Equals(Keys.Down) || e.KeyCode.Equals(Keys.Up))
            {
                e.SuppressKeyPress = true;
            }
            else if (e.KeyCode == Keys.F && e.Control)
            {
                if (UserSettings.Instance.ReadingMode != ReadingMode.FullScreen)
                    SetReadingMode(ReadingMode.FullScreen);
                else
                    SetReadingMode(ReadingMode.Normal);
            }
        }

        private void SetReadingWidth()
        {
            this.MinimumSize = new Size(UserSettings.Instance.ReadingWidth, 0);
            lblScrolling.Width = UserSettings.Instance.ReadingWidth;
        }

        private void LoadSettingsAndDefaults()
        {
            this.BackColor = UserSettings.Instance.ReadingWindowColor;
            lblScrolling.BackColor = UserSettings.Instance.ReadingBackColor;
            lblScrolling.ForeColor = UserSettings.Instance.ReadingForeColor;
            lblScrolling.Font = UserSettings.Instance.ReadingFont;
            lblScrolling.LeftRightPadding = UserSettings.Instance.ReadingHorizPadding;
            lblScrolling.TopBottomPadding = UserSettings.Instance.ReadingVertPadding;

            SetReadingWidth();

            switch (UserSettings.Instance.ReadingMode)
            {
                case ReadingMode.Normal: tsmiNormal.PerformClick(); break;
                case ReadingMode.FullScreen: tsmiFullScreen.PerformClick(); break;
                case ReadingMode.Script: tsmiScript.PerformClick(); break;
            }
        }

        private void UpdateTextBoxBounds()
        {
            int x = (this.ClientSize.Width / 2) - (lblScrolling.Width / 2);
            int y = (controlExtender.Visible) ? controlExtender.Height : 0;
            int width = UserSettings.Instance.ReadingWidth;
            int height = this.ClientSize.Height - y;
            lblScrolling.Bounds = new Rectangle(x, y, width, height);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            UpdateTextBoxBounds();
        }

        protected override void OnClosed(EventArgs e)
        {
            UserSettings.Instance.ReadingViewBounds = this.DesktopBounds;
            UserSettings.Instance.ReadingVertPadding = lblScrolling.Padding.Top;
            UserSettings.Instance.ReadingHorizPadding = lblScrolling.Padding.Right;
            base.OnClosed(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Put this in the constructor?
            UpdateTextBoxBounds();
            // Activate the control so we can scroll and what not right away
            this.ActiveControl = lblScrolling;
        }

        private void tsmiNormal_Click(object sender, EventArgs e)
        {
            tsmiNormal.Checked = true;
            tsmiScript.Checked = false;
            tsmiFullScreen.Checked = false;
            SetReadingMode(ReadingMode.Normal);
        }

        private void tsmiScript_Click(object sender, EventArgs e)
        {
            tsmiScript.Checked = true;
            tsmiNormal.Checked = false;
            tsmiFullScreen.Checked = false;
            SetReadingMode(ReadingMode.Script);
        }

        private void tsmiFullScreen_Click(object sender, EventArgs e)
        {
            tsmiFullScreen.Checked = true;
            tsmiNormal.Checked = false;
            tsmiScript.Checked = false;
            SetReadingMode(ReadingMode.FullScreen);
        }

        private void tsmiEditor_Click(object sender, EventArgs e)
        {
            UserSettings.Instance.ReadingFont = UserSettings.Instance.Font;
            UserSettings.Instance.ReadingForeColor = UserSettings.Instance.FontColor;
            UserSettings.Instance.ReadingWindowColor = UserSettings.Instance.BackColor;
            UserSettings.Instance.ReadingBackColor = UserSettings.Instance.BackColor;
            LoadSettingsAndDefaults();
        }

        private void tsmiOldScripture_Click(object sender, EventArgs e)
        {
            if (MainForm.IsFontInstalled("Book Antiqua"))
                UserSettings.Instance.ReadingFont = new Font("Book Antiqua", 14f, FontStyle.Regular);

            UserSettings.Instance.ReadingForeColor = Color.FromArgb(64, 64, 64);
            UserSettings.Instance.ReadingBackColor = Color.PapayaWhip;
            UserSettings.Instance.ReadingWindowColor = Color.Tan;
            LoadSettingsAndDefaults();
        }

        private void tsmiComputerConsole_Click(object sender, EventArgs e)
        {
            if (MainForm.IsFontInstalled("Calibri"))
                UserSettings.Instance.ReadingFont = new Font("Calibri", 14f, FontStyle.Regular);

            UserSettings.Instance.ReadingForeColor = Color.LimeGreen;
            UserSettings.Instance.ReadingBackColor = Color.Black;
            UserSettings.Instance.ReadingWindowColor = Color.Black;
            LoadSettingsAndDefaults();
        }

        private void tsmiNovel_Click(object sender, EventArgs e)
        {
            if (MainForm.IsFontInstalled("Courier"))
                UserSettings.Instance.ReadingFont = new Font("Courier", 14f, FontStyle.Regular);

            UserSettings.Instance.ReadingForeColor = Color.Black;
            UserSettings.Instance.ReadingBackColor = Color.White;
            UserSettings.Instance.ReadingWindowColor = Color.DarkGray;
            UserSettings.Instance.ReadingVertPadding = 10;
            UserSettings.Instance.ReadingHorizPadding = 20;
            LoadSettingsAndDefaults();
        }

        private void tsmiModernDark_Click(object sender, EventArgs e)
        {
            if (MainForm.IsFontInstalled("Lyon"))
                UserSettings.Instance.ReadingFont = new Font("Lyon", 14f, FontStyle.Regular);
            else if (MainForm.IsFontInstalled("Ideal Sans"))
                UserSettings.Instance.ReadingFont = new Font("Ideal Sans", 14f, FontStyle.Regular);
            else if (MainForm.IsFontInstalled("Helvetica"))
                UserSettings.Instance.ReadingFont = new Font("Helvetica", 14f, FontStyle.Regular);
            else if (MainForm.IsFontInstalled("Trebuchet MS"))
                UserSettings.Instance.ReadingFont = new Font("Trebuchet MS", 14f, FontStyle.Regular);
            else if (MainForm.IsFontInstalled("Calibri"))
                UserSettings.Instance.ReadingFont = new Font("Calibri", 14f, FontStyle.Regular);

            UserSettings.Instance.ReadingForeColor = Color.LightGray;
            UserSettings.Instance.ReadingBackColor = Color.FromArgb(20, 20, 20);
            UserSettings.Instance.ReadingWindowColor = Color.FromArgb(20, 20, 20);
            UserSettings.Instance.ReadingVertPadding = 10;
            UserSettings.Instance.ReadingHorizPadding = 20;
            LoadSettingsAndDefaults();
        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetReadingMode(ReadingMode mode)
        {
            switch (mode)
            {
                case ReadingMode.FullScreen:
                    controlExtender.Visible = false;
                    this.TopMost = true;
                    this.FormBorderStyle = FormBorderStyle.None;
                    this.Bounds = Screen.GetBounds(this);
                    this.BringToFront();
                    break;
                case ReadingMode.Normal:
                    this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
                    controlExtender.Visible = false;
                    this.Bounds = this.RestoreBounds;
                    this.TopMost = UserSettings.Instance.ReadingViewTopMost;
                    this.Size = new Size(UserSettings.Instance.ReadingWidth, 500);
                    this.CenterToScreen();
                    this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
                    break;
                case ReadingMode.Script:
                    controlExtender.Visible = true;
                    this.TopMost = true;
                    this.WindowState = FormWindowState.Normal;
                    this.FormBorderStyle = FormBorderStyle.None;

                    const int HEIGHT = 200;
                    var screenRect = Screen.GetBounds(this);
                    var size = new Size(screenRect.Width, HEIGHT);
                    int y = screenRect.Y + screenRect.Height - HEIGHT;
                    var pos = new Point(screenRect.X, y);
                    this.Bounds = new Rectangle(pos, size);
                    break;
            }

            UserSettings.Instance.ReadingMode = mode;
        }
    }
}
