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;

namespace ReGen
{
    public partial class DocumentResultsForm : Form
    {
        private List<String> m_ProcessedDocuments = new List<String>();
        private DocumentResults m_DocumentResults = new DocumentResults();

        public DocumentResultsForm()
        {
            InitializeComponent();
        }

        public void SetResults(DocumentResults Results)
        {
            if (null != m_DocumentResults)
            {
                try
                {
                    m_DocumentResults = Results;
                }
                catch
                {

                }
            }
        }

        public void SetProcessedDocuments(List<String> ProcessedDocuments)
        {
            if (null != m_ProcessedDocuments)
            {
                m_ProcessedDocuments.Clear();

                if ((null != ProcessedDocuments) && (0 != ProcessedDocuments.Count))
                {
                    m_ProcessedDocuments.AddRange(ProcessedDocuments);
                }
            }
        }

        private void PopulateProcessedList()
        {
            try
            {
                ProcessedDocumentsListBox.Items.Clear();

                if (null != m_ProcessedDocuments)
                {
                    foreach (String DocumentPath in m_ProcessedDocuments)
                    {
                        try
                        {
                            ProcessedDocumentsListBox.Items.Add(DocumentPath);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void InitializeSizeAndLocation()
        {
            if (0 == (ModifierKeys & Keys.Shift))
            {
                try
                {
                    String initLocation = Properties.Settings.Default.DocumentResultsInitialLocation;
                    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 DocumentErrorForm_Load(object sender, EventArgs e)
        {
            InitializeSizeAndLocation();

            PopulateProcessedList();
        }

        private void ShowResultsForDocument(String DocumentName)
        {
            if (!String.IsNullOrWhiteSpace(DocumentName) && (null != m_DocumentResults))
            {
                try
                {
                    SortedDictionary<String, List<String>> ResultsForDocumentStudies = m_DocumentResults.GetDocumentResults(DocumentName);

                    if (null != ResultsForDocumentStudies)
                    {
                        StudyResultsForm ResultsDlg = new StudyResultsForm();

                        if (null != ResultsDlg)
                        {
                            ResultsDlg.SetDocument(DocumentName);
                            ResultsDlg.SetResults(ResultsForDocumentStudies);

                            ResultsDlg.Show(this);
                        }
                    }
                }
                catch
                {
                }
            }
        }

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

        private void ProcessedDocumentsListBox_DoubleClick(object sender, EventArgs e)
        {
            if (1 == ProcessedDocumentsListBox.SelectedItems.Count)
            {
                try
                {
                    String DocumentName = ProcessedDocumentsListBox.SelectedItem.ToString();

                    ShowResultsForDocument(DocumentName);
                }
                catch
                {
                }
            }
        }

        private void ProcessedDocumentsListBox_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (ListBox.NoMatches == ProcessedDocumentsListBox.IndexFromPoint(e.Location))
                {
                    ProcessedDocumentsListBox.SelectedIndex = -1;
                }
            }
            catch
            {

            }
        }

        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.DocumentResultsInitialLocation = initLocation;
                    Properties.Settings.Default.Save();
                }
                catch
                {

                }
            }
        }

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