using Gremlin.GremlinData.DBClasses;
using Gremlin.GremlinUtilities;
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;

namespace Gremlin.GremlinUI
{
    /// <summary>
    /// Interaktionslogik für DataImport.xaml
    /// </summary>
    public partial class DataImport : Window
    {
        private string _filepath;
        private string _encodingName;
        
        public DataImport()
        {
            InitializeComponent();
        }

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

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            //GenericImporter.Run(tbDataSource.Text, tbSeparator.Text, tbEncoding.Text);
            bool success = await GenericImporter.RunAsync(_filepath, tbSeparator.Text, tbEncoding.Text);
            if (success) Close();
        }

        private void btnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            _filepath = FileHelper.GetFilepathFromUser();
            if (_filepath != "")
            {
                tbDataSource.Text = DbHelper.ExtractFileName(_filepath, true);
                tbEncoding.Text = "";
                tbSeparator.Text = "";
            }
        }

        private void tbDataSource_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            btnOpenFile_Click(sender, e);
        }

        private void btnGetEncoding_Click(object sender, RoutedEventArgs e)
        {
            _encodingName = FileHelper.GetEncoding(_filepath)?.HeaderName;
            tbEncoding.Text = FileHelper.GetEncoding(_filepath)?.EncodingName;
        }

        private void btnGetSeparator_Click(object sender, RoutedEventArgs e)
        {
            tbSeparator.Text = GenericImporter.GuessSeparator(_filepath, _encodingName);
        }
    }
}
