using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using DPackLib;

namespace Generators
{
    public partial class MainWindow 
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            foreach (var knownType in CommonConstants.DetectorsTypes)
            {
                GeneratorsList.Items.Add(knownType);
                AddGenerator(knownType);
            }

            if (GeneratorsList.Items.Count > 0)
            { GeneratorsList.SelectedIndex = 0; }
        }

        private void AddGenerator_OnClick(object sender, RoutedEventArgs e)
        {
            string genType = GeneratorsList.SelectedItem.ToString();
            if (string.IsNullOrEmpty(genType)) { genType = "Speed"; }

            AddGenerator(genType);
        }

        private void AddGenerator(string genType)
        {
            int nr = 1;

            foreach (var children in GeneratopStack.Children)
            {
                var gen = (GeneratorPanel)children;
                if (gen != null && gen.PanelName.Contains(genType)) { nr++; }
            }

            var genPanel = new GeneratorPanel
            {
                PanelName = nr + "-" + genType,
                Margin = new Thickness(0, 5, 0, 5)
            };

            GeneratopStack.Children.Add(genPanel);
        }

        private void CleanData()
        {
            var datFiles = Directory.GetFiles(CommonConstants.RootDir).Where(x => x.EndsWith(".dat"));
            foreach (var datFile in datFiles)
            {
                File.Delete(datFile);
            }
        }

        private void MainWindow_OnClosing(object sender, CancelEventArgs e)
        {
            CleanData();
        }
    }
}
