using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Resources;


namespace EduNetworkBuilder
{
    public partial class RTFWindow : Form
    {
        public RTFWindow()
        {
            InitializeComponent();
            LanguagifyComponents();
        }
        /// <summary>
        /// Used when viewing help from the ? button.  This displays network specific help information
        /// </summary>
        /// <param name="theHelp"></param>
        /// <param name="theTests"></param>
        public RTFWindow(string title, string theHelp, List<NetTest> theTests)
        {
            InitializeComponent();
            LanguagifyComponents();
            BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
            if (myWin != null)
            {
                StartPosition = FormStartPosition.Manual;
                Point newlocation = new Point(myWin.Location.X + myWin.Width + 5, myWin.Location.Y);
                if (newlocation.X > (Screen.PrimaryScreen.Bounds.Width + 5))
                {
                    newlocation = new Point(Screen.PrimaryScreen.Bounds.Width - 5, newlocation.Y);
                }
                Location = newlocation;
                Height = myWin.Height;
            }
            System.Drawing.Font currentFont = rtbContent.SelectionFont;
            Text = title;
            rtbContent.Text = theHelp;
            rtbContent.SelectAll();
            rtbContent.SelectionFont = new Font(
                currentFont.FontFamily,
                currentFont.Size + 2, FontStyle.Regular);
            rtbContent.DeselectAll();
        }

        /// <summary>
        /// Start an RTFWindow with the specified information showing
        /// </summary>
        /// <param name="WhatToShow"></param>
        public RTFWindow(RTFWindowContents WhatToShow)
        {
            InitializeComponent();
            LanguagifyComponents();
            string currentDir = Directory.GetCurrentDirectory();
            string myRTF;
            Form myWin = null;
            myWin = Application.OpenForms["RTFWindow"];
            if (myWin == null)
            {
                myWin = Application.OpenForms["BuilderWindow"];
            }
            if (myWin != null)
            {
                StartPosition = FormStartPosition.Manual;
                Point newlocation = new Point(myWin.Location.X + myWin.Width + 5, myWin.Location.Y);
                if (newlocation.X > (Screen.PrimaryScreen.Bounds.Width + 5))
                {
                    newlocation = new Point(Screen.PrimaryScreen.Bounds.Width - 5, newlocation.Y);
                }
                Location = newlocation;
                Height = myWin.Height;
            }
            if (WhatToShow == RTFWindowContents.help)
            {                
                myRTF = Properties.Resources.Help;
                rtbContent.Rtf = myRTF;
            }
            else if (WhatToShow == RTFWindowContents.about)
            {
                myRTF = Properties.Resources.about;
                rtbContent.Rtf = myRTF;
            }
            else if (WhatToShow == RTFWindowContents.release_notes)
            {
                myRTF = Properties.Resources.ReleaseNotes;
                rtbContent.Rtf = myRTF;
            }
        }

        public void JumpToSelection(string WhatToFind)
        {
            int indexToText = rtbContent.Find(WhatToFind);
            if (indexToText > 0)
            {
                rtbContent.SelectionStart = indexToText;
                rtbContent.ScrollToCaret();
            }
        }
        public void JumpToSelection(HelpTopics WhatToFind)
        {
            string toFind = NB.Translate(NB.GetHelpTopicKey(WhatToFind));
            JumpToSelection(toFind);
        }
        private void LanguagifyComponents()
        {
            rtbContent.Text = NB.Translate("RTFW_rtbContent");
            btnOK.Text = NB.Translate("_OK");
            Text = NB.Translate("RTFW_Form");
            lblFind.Text = NB.Translate("RTFW_lblFind");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Search(bool GoForward)
        {
            int loc = rtbContent.SelectionStart;
            int indexToText;
            string WhatToFind =  tbFind.Text;
            if (tbFind.Text == "") return; //We do nothing if nothing to find.
            if (GoForward)
            {
                loc++; //move forward just a smidge
                if (loc >= rtbContent.MaxLength) loc = rtbContent.MaxLength;
                indexToText = rtbContent.Find(WhatToFind,loc,RichTextBoxFinds.None);
            }
            else
            {
                loc--;//Move back a smidge
                if (loc < 0) loc = 0;
                indexToText = rtbContent.Find(WhatToFind, loc, RichTextBoxFinds.Reverse);
            }
            if (indexToText > 0)
            {
                rtbContent.SelectionStart = indexToText;
                rtbContent.SelectionBackColor = Color.LightBlue;
                rtbContent.ScrollToCaret();
            }
            if(indexToText == -1)
            {
                //We did not go anywhere
                MessageBox.Show(NB.Translate("RTFW_NotFound"));
            }
        }

        private void btnFwd_Click(object sender, EventArgs e)
        {
            Search(true);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            Search(false);
        }

    }
}
