using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;

namespace CDAValidator
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class Settings : Window
    {
        public Settings()
        {
            InitializeComponent();
            tbPath.Text = ConfigSettings.Default.FilePath;
            tbLogPath.Text = ConfigSettings.Default.LogFilePath;
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            doBrowsing(0);
        }

        /// <summary>
        /// Replaces / with \ in the path.
        /// </summary>
        /// <param name="path"></param>
        private void correctPath(ref string path)
        {
            path = path.Replace('/', '\\');
            string invalid = new string(System.IO.Path.GetInvalidPathChars());

            foreach (char c in invalid)
            {
                path = path.Replace(c.ToString(), "");
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            saveSettings();
            this.Close();
        }

        private void saveSettings()
        {
            if (tbPath.Text.Length == 0)
            {
                tbPath.Text = @"C:\CASMed\CDALetters";
                tbLogPath.Text = @"C:\CASMed\CDALetters";
            }
            ConfigSettings.Default.FilePath = tbPath.Text;
            ConfigSettings.Default.LogFilePath = tbLogPath.Text;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnBrowseLog_Click(object sender, RoutedEventArgs e)
        {
            doBrowsing(1);
        }
        
        private void doBrowsing(int a)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            try
            {
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (a == 0)
                    {
                        tbPath.Text = dialog.SelectedPath + @"\";
                        string path = tbPath.Text;
                        correctPath(ref path);
                        tbPath.Text = path;
                        tbPath.CaretIndex = tbPath.Text.Length;
                        var rect = tbPath.GetRectFromCharacterIndex(tbPath.CaretIndex);
                        tbPath.ScrollToHorizontalOffset(rect.Right);
                    }
                    else if (a == 1)
                    {
                        tbLogPath.Text = dialog.SelectedPath + @"\";
                        string path = tbLogPath.Text;
                        correctPath(ref path);
                        tbLogPath.Text = path;
                        tbLogPath.CaretIndex = tbPath.Text.Length;
                        var rect = tbLogPath.GetRectFromCharacterIndex(tbLogPath.CaretIndex);
                        tbLogPath.ScrollToHorizontalOffset(rect.Right);
                    }  
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                logError(ex);
            }
        }

        private void logError(Exception e)
        {
            Logging.eventLog(e);
            Logging.writeLog(e.ToString());

        }

        private void Transformation_Click(object sender, RoutedEventArgs e)
        {
            SchematronTransWindow tmp = new SchematronTransWindow();
            tmp.Show();
        }

       
    }
}
