﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

namespace AsynchronousXmlAligner
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        public string[] fileAddress = new string[3] {"", "", ""};
        private void selectLXmlBtn_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "XML files (*.xml)|*.xml";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                       fileAddress[1] = openFileDialog1.FileName;
                       lXmlTextBox.Text = fileAddress[1].Substring(fileAddress[1].LastIndexOf('\u005c') + 1);
                       myStream.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void selectRXmlBn_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "XML files (*.xml)|*.xml";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        fileAddress[2] = openFileDialog1.FileName;
                        rXmlTextBox.Text = fileAddress[2].Substring(fileAddress[2].LastIndexOf('\u005c') + 1);
                        myStream.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void selectXslBtn_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "XSL files (*.xsl)|*.xsl";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        fileAddress[0] = openFileDialog1.FileName;
                        xslTextBox.Text = fileAddress[0].Substring(fileAddress[0].LastIndexOf('\u005c') + 1);
                        myStream.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void okBtn_Click(object sender, EventArgs e)
        {

            Form2.fileAddressMngr[0] = fileAddress[0];
            Form2.fileAddressMngr[1] = fileAddress[1];
            Form2.fileAddressMngr[2] = fileAddress[2];

            if (fileAddress[0] == "" || fileAddress[1] == "" || fileAddress[2] == "" || sessionNameTxtBox.TextLength == 0)
            {
                MessageBox.Show("Please name your session and select every file!");
            }
            else
            {
                Form2.sessionName = sessionNameTxtBox.Text;
                Form2.config.Add(new XElement("Session",
                    new XElement("Name", sessionNameTxtBox.Text),
                    new XElement("xslAddress", fileAddress[0]),
                    new XElement("xmlAddressL", fileAddress[1]),
                    new XElement("xmlAddressR", fileAddress[2])
                        ));
                Form2.config.Save("config.xml");
                this.DialogResult = DialogResult.OK;
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}
