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.IO;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace ReGen
{
    public partial class StudyResultsForm : Form
    {
        private SortedDictionary<String, List<String>> m_Results = new SortedDictionary<String, List<String>>();
        private String m_Document;

        public StudyResultsForm()
        {
            InitializeComponent();
        }

        public void SetResults(SortedDictionary<String, List<String>> Results)
        {
            m_Results.Clear();

            try
            {
                if (null != Results)
                {
                    foreach (String key in Results.Keys)
                    {
                        m_Results.Add(key, Results[key]);
                    }
                }
            }
            catch
            {

            }
        }

        public void SetDocument(String Document)
        {
            m_Document = Document;
        }

        private void AddDocumentName()
        {
            try
            {
                DetailedResultsTextBox.SelectionFont = new Font("Calibri", 24, FontStyle.Bold | FontStyle.Italic);
                DetailedResultsTextBox.SelectionColor = Color.SlateGray;
                DetailedResultsTextBox.SelectedText = DocumentNameWithExtension() + "\n";
            }
            catch
            {
            }
        }

        private void AddDateAndTime()
        {
            try
            {
                DetailedResultsTextBox.SelectionFont = new Font("Calibri", 8, FontStyle.Italic);
                DetailedResultsTextBox.SelectionColor = Color.SlateGray;
                DetailedResultsTextBox.SelectedText = "Created: " +  DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\n";
            }
            catch
            {
            }
        }

        private void AddDocumentHeader()
        {
            if (!String.IsNullOrWhiteSpace(m_Document))
            {
                try
                {
                    AddDocumentName();

                    AddDateAndTime();

                    DetailedResultsTextBox.SelectedText = "\n\n";
                }
                catch
                {
                }
            }
        }

        private void AddStudyHeader(String StudyName)
        {
            if (!String.IsNullOrWhiteSpace(StudyName))
            {
                try
                {
                    DetailedResultsTextBox.SelectionFont = new Font("Calibri", 16, FontStyle.Bold);
                    DetailedResultsTextBox.SelectionColor = Color.SteelBlue;

                    DetailedResultsTextBox.SelectedText = StudyName;

                    DetailedResultsTextBox.SelectionFont = new Font("Calibri", 12);
                    DetailedResultsTextBox.SelectedText = "\n";
                }
                catch
                {
                }
            }
        }

        private void AddFormattedResult(String Result)
        {
            if (!String.IsNullOrWhiteSpace(Result))
            {
                try
                {
                    DetailedResultsTextBox.SelectionIndent = 20;
                    DetailedResultsTextBox.SelectionBullet = true;

                    DetailedResultsTextBox.SelectionFont = new Font("Calibri", 12);

                    DetailedResultsTextBox.SelectedText = "  " + Result + "\n";

                    DetailedResultsTextBox.SelectionBullet = false;
                    DetailedResultsTextBox.SelectionIndent = 0;

                    DetailedResultsTextBox.SelectedText = "\n";
                }
                catch
                {
                }
            }
        }

        private void AddStudyResults(String StudyName)
        {
            if (!String.IsNullOrWhiteSpace(StudyName))
            {
                try
                {
                    foreach (String Result in m_Results[StudyName])
                    {
                        AddFormattedResult(Result);
                    }

                    DetailedResultsTextBox.SelectedText = "\n";
                }
                catch
                {
                }
            }
        }

        private void AddStudyDetails(String StudyName)
        {
            AddStudyHeader(StudyName);

            AddStudyResults(StudyName);
        }

        private void PopulateResultsText()
        {
            if (null != m_Results)
            {
                try
                {
                    AddDocumentHeader();

                    foreach (String Study in m_Results.Keys)
                    {
                        try
                        {
                            AddStudyDetails(Study);
                        }
                        catch
                        {
                        }
                    }
                }
                catch
                {

                }
            }
        }

        private String DocumentNameWithoutExtension()
        {
            String DocumentName = "";

            if (!String.IsNullOrWhiteSpace(m_Document))
            {
                try
                {
                    DocumentName = Path.GetFileNameWithoutExtension(m_Document);
                }
                catch
                {
                    DocumentName = "";
                }

            }

            return DocumentName;
        }

        private String DocumentNameWithExtension()
        {
            String DocumentName = "";

            if (!String.IsNullOrWhiteSpace(m_Document))
            {
                try
                {
                    DocumentName = Path.GetFileName(m_Document);
                }
                catch
                {
                    DocumentName = "";
                }

            }

            return DocumentName;
        }

        private void InitializeSaveFileDialog(ref SaveFileDialog SaveDlg)
        {
            try
            {
                if (null != SaveDlg)
                {
                    String DefaultBaseName = DocumentNameWithoutExtension();

                    if (String.IsNullOrWhiteSpace(DefaultBaseName))
                    {
                        DefaultBaseName = "ReGen Study";
                    }

                    SaveDlg.DefaultExt = "*.rtf";
                    SaveDlg.Filter = "Rich Text Files|*.rtf";
                    SaveDlg.FileName = DefaultBaseName + " Results.rtf";
                }
            }
            catch
            {
            }
        }

        private void SaveResultsToFile()
        {
            try
            {
                SaveFileDialog SaveDlg = new SaveFileDialog();

                if (null != SaveDlg)
                {
                    InitializeSaveFileDialog(ref SaveDlg);

                    if (DialogResult.OK == SaveDlg.ShowDialog())
                    {
                        if (!String.IsNullOrWhiteSpace(SaveDlg.FileName))
                        {
                            DetailedResultsTextBox.SaveFile(SaveDlg.FileName);
                        }
                    }
                }
            }
            catch
            {

            }
        }

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

        private void InitializeSizeAndLocation()
        {
            if (0 == (ModifierKeys & Keys.Shift))
            {
                try
                {
                    String initLocation = Properties.Settings.Default.StudyResultsInitialLocation;
                    Point InitialPoint = this.Location;
                    Size InitialSize = this.Size;

                    if (!String.IsNullOrWhiteSpace(initLocation))
                    {
                        String[] parts = initLocation.Split(',');

                        if (2 <= parts.Length)
                        {
                            InitialPoint = new Point(int.Parse(parts[0]), int.Parse(parts[1]));
                        }

                        if (4 <= parts.Length)
                        {
                            InitialSize = new Size(int.Parse(parts[2]), int.Parse(parts[3]));
                        }
                    }

                    this.Size = InitialSize;
                    this.Location = InitialPoint;
                }
                catch
                {
                }
            }
        }

        private void StudyResultsForm_Load(object sender, EventArgs e)
        {
            InitializeSizeAndLocation();

            PopulateResultsText();
        }

        private void SaveResultsButton_Click(object sender, EventArgs e)
        {
            SaveResultsToFile();
        }

        private void SaveSizeAndLocation()
        {
            if (0 == (ModifierKeys & Keys.Shift))
            {
                try
                {
                    Point SaveLocation = this.Location;
                    Size SaveSize = this.Size;

                    if (FormWindowState.Normal != this.WindowState)
                    {
                        SaveLocation = RestoreBounds.Location;
                        SaveSize = RestoreBounds.Size;
                    }

                    String initLocation = String.Join(",", SaveLocation.X, SaveLocation.Y, SaveSize.Width, SaveSize.Height);

                    Properties.Settings.Default.StudyResultsInitialLocation = initLocation;
                    Properties.Settings.Default.Save();
                }
                catch
                {

                }
            }
        }

        private void StudyResultsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveSizeAndLocation();
        }
    }
}
