using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
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;

namespace Nappat.Testing.WPF
{
    /// <summary>
    /// Interaction logic for DataDialog.xaml
    /// </summary>
    public partial class InterpretDialog: Window
    {
        DataSender dataSenderTemp = new DataSender();
        DataSender dataSender;
        BackgroundWorker backgroundWorker = new BackgroundWorker();
        System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
        Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();

        public InterpretDialog(DataSender dataSender)
        {
            InitializeComponent();
            this.dataSender = dataSender;
            this.dataSenderTemp = dataSender;
            this.DataContext = dataSenderTemp;
            this.moreSettingExpander.Expanded += moreSettingExpander_Expanded;
            this.moreSettingExpander.Collapsed += moreSettingExpander_Collapsed;
        }

        void moreSettingExpander_Collapsed(object sender, RoutedEventArgs e)
        {
            this.MinHeight = 250;
            this.Height = 250;
        }

        void moreSettingExpander_Expanded(object sender, RoutedEventArgs e)
        {
            this.MinHeight = 500;
        }

        private void oKButton_Click(object sender, RoutedEventArgs e)
        {
            correctCloser();
            //this.dataSenderTemp.DriverPaths = "***";
        }

        private void correctCloser()
        {
            this.dataSenderTemp.ProgramChooser = 2;
            dataSender = this.dataSenderTemp;
            this.Close();
        }

        private void FileValidator(object sender, EventArgs e)
        {
                if (File.Exists(((TextBox)sender).Text))
                    ((TextBox)sender).Background = Brushes.LightGreen;
                else
                    ((TextBox)sender).Background = Brushes.Salmon;
        }

        private void DirectoryValidator(object sender, EventArgs e)
        {
                if (Directory.Exists(((TextBox)sender).Text))
                    ((TextBox)sender).Background = Brushes.LightGreen;
                else
                    ((TextBox)sender).Background = Brushes.Salmon;
        }

        private void NameValidator(object sender, EventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(((TextBox)sender).Text, @"^[a-žA-Ž0-9]+$"))
                ((TextBox)sender).Background = Brushes.LightGreen;
            else
                ((TextBox)sender).Background = Brushes.Salmon;
        }

        private void FileChooser(object sender, MouseButtonEventArgs e)
        {
            //dlg.Multiselect = true;            
            if (((TextBox)sender).Name == "")
            {
                openFileDialog.DefaultExt = ".xml";
                openFileDialog.Filter = "Config file|*.XML|All Files (*.*)|*.*";
            }
            if (((TextBox)sender).Name == "scriptPathTextBox")
            {
                openFileDialog.DefaultExt = ".scr";
                openFileDialog.Filter = "Script file|*.SCR|All Files (*.*)|*.*";
            }
            Nullable<bool> result = openFileDialog.ShowDialog();
            if (result == true)
            {
                foreach (String file in openFileDialog.FileNames)
                {
                    try
                    {
                        ((TextBox)sender).Text = file;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Cannot open the file(s): " + file
                            + ". You may not have permission to read the file, or " +
                            "it may be corrupt.\n\nReported error: " + ex.Message);
                    }
                }
            }
        }

        private void DirectoryChooser(object sender, EventArgs e)
        {
            this.folderBrowserDialog.Description = "Select the directory that you want to use.";
            this.folderBrowserDialog.ShowNewFolderButton = false;
            System.Windows.Forms.DialogResult result = folderBrowserDialog.ShowDialog();
            ((TextBox)sender).Text = folderBrowserDialog.SelectedPath;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                correctCloser();
            }
        }
    }
}
