using Microsoft.Win32;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace FSI.Lib.Guis.SieStarterCsvExporter
{
    /// <summary>
    /// Interaktionslogik für frmMain.xaml
    /// </summary>
    public partial class FrmMain : Window
    {
        public FrmMain()
        {
            InitializeComponent();
            DataContext = new MVVM.ViewModel.CurrentTimeViewModel();
        }
        private void BtnSlctSrcFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new()
            {
                Filter = "Trace (*.trc)|*.trc|alle Dateien (*:*)|*.*"
            };           

            if ((bool)dlg.ShowDialog())
            {
                tbSrcFile.Text = dlg.FileName;
            }
        }

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

            string test = GetType().Namespace;

            var test2 = Assembly.GetExecutingAssembly;


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

                    process.Start();
                    process.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;
            }
        }
    }
}
