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.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Collections.Specialized;

namespace FSI.SIE.Starter.Csv.Exporter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnSlctSrcFile_Click(object sender, RoutedEventArgs e)
        {
            using OpenFileDialog dlg = new()
            {
                Filter = "Trace (*.trc)|*.trc|alle Dateien (*:*)|*.*"
            };

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbSrcFile.Text = dlg.FileName;
            }
        }

        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            string[] tbEntries = tbSrcFile.Text.Split("\n");

            foreach (string tbEntry in tbEntries)
            {
                if (!string.IsNullOrEmpty(tbEntry))
                {
                    Process p = new Process();
                    p.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\" + "Convert_SINAMICS_trace_CSV.exe";
                    p.StartInfo.Arguments = "\"" + tbEntry + "\"";

                    p.Start();
                    p.WaitForExit();
                }
            }

            tbSrcFile.Text = string.Empty;
        }

        private void TbSrcFile_PreviewDragEnter(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
            {
                e.Effects = System.Windows.DragDropEffects.Copy;
            }
            else
            {
                e.Effects = System.Windows.DragDropEffects.None;
            }
        }

        private void TbSrcFile_PreviewDragOver(object sender, System.Windows.DragEventArgs e)
        {
            e.Handled = true;
        }

        private void TbSrcFile_Drop(object sender, System.Windows.DragEventArgs e)
        {
            // Get data object
            var dataObject = e.Data as System.Windows.DataObject;

            // Check for file list
            if (dataObject.ContainsFileDropList())
            {
                // Clear values
                tbSrcFile.Text = string.Empty;

                // Process file names
                StringCollection fileNames = dataObject.GetFileDropList();
                StringBuilder bd = new();
                foreach (string fileName in fileNames)
                {
                    if (fileName.EndsWith(".trc"))
                    {
                        bd.Append(fileName + "\n");
                    }
                }

                // Set text
                tbSrcFile.Text = bd.ToString();
            }
        }

        private void TbSrcFile_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbSrcFile.Text))
            {
                btnStart.IsEnabled = true;
            }
            else
            {
                btnStart.IsEnabled = false;
            }
        }
    }
}
