using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using McLaren_WPF.Controls;
using McLaren_WPF.Events;
using McLaren_WPF.Interfaces;
using McLaren_WPF.Views;
using Microsoft.Practices.Unity;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System.Diagnostics;
using System.Windows.Threading;
using DPackLib;

namespace McLaren_WPF.ViewModels
{
    public class MonitorsSetViewModel:BindableBase, IMonitorsSetViewModel
    {

        [Dependency]public IEventAggregator EventAggregator { get; set; }
        [Dependency]public IUnityContainer Container { get; set; }

        #region Var

        private WrapPanel MonitorWrapPanel { get; set; }
        List<string> panelsList = new List<string>();
        private DispatcherTimer genTimer;

        #endregion

        [InjectionMethod]
        public void InitializeDataChannelView()
        {
            //SubmitCommand = new DelegateCommand<object>(OnSubmit, CanSubmit);
            EventAggregator.GetEvent<DataPackEvent>().Subscribe(CameNewData);
            EventAggregator.GetEvent<SetMonitorTypeEvent>().Subscribe(SetMonitorType);
            MonitorWrapPanel = new WrapPanel {Orientation = Orientation.Horizontal};
            MonitorsSet = MonitorWrapPanel;
            CreateTimer();
        }

        private void CreateTimer()
        {
            genTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1000) };
            genTimer.Tick += GenTimer_Tick;
            genTimer.Start();
        }

        private void GenTimer_Tick(object sender, EventArgs e)
        {
            CheckGenerators();
        }

        private void CheckGenerators()
        {
            if (!Directory.Exists(CommonConstants.RootDir)) return;
            var datFiles = Directory.GetFiles(CommonConstants.RootDir);//.Where(x => Path.HasExtension("dat"));

            foreach (var type in CommonConstants.DetectorsTypes)
            {
                foreach (var datFile in datFiles)
                {
                    string name = Path.GetFileNameWithoutExtension(datFile);
                    if (!string.IsNullOrWhiteSpace(name) && name.Contains(type) && !panelsList.Contains(name))
                    {
                        AddPanel(name);
                        panelsList.Add(name);
                    }           
                }
            }
        }

        private void AddPanel(string file)
        {
            if (file.Contains("Speed"))
            {
                AddSpeedPanel(file);
            }

            if (file.Contains("Fuel"))
            {
                AddFuelPanel(file);
            }

            if (file.Contains("Pilot"))
            {
                AddPilotPanel(file);
            }

            if (file.Contains("Path"))
            {
                AddPathPanel(file);
            }

            if (file.Contains("Temperature"))
            {
                AddTemperaturePanel(file);
            }
        }

        private void  AddSpeedPanel(string file)
        {
            var panel = new SpeedPanel { Description = file };
            MonitorWrapPanel.Children.Add(panel);
            panel.StartReception();
        }

        private void AddFuelPanel(string file)
        {
            var panel = new FuelPanel { Description = file };
            MonitorWrapPanel.Children.Add(panel);
            panel.StartReception();
        }

        private void AddPilotPanel(string file)
        {
            var panel = new PilotPanel {Description = file};
            MonitorWrapPanel.Children.Add(panel);
            panel.StartReception();
        }

        private void AddPathPanel(string file)
        {
            var panel = new PathPanel { Description = file };
            MonitorWrapPanel.Children.Add(panel);
            panel.StartReception();
        }

        private void AddTemperaturePanel(string file)
        {
            var panel = new TemperaturePanel { Description = file };
            MonitorWrapPanel.Children.Add(panel);
            panel.StartReception();
        }

        private void SetMonitorType(SetMonitorTypeEventArgs args)
        {
            
        }

        private void CameNewData(DataPackEventArgs args)
        {
            
        }

        #region Binding

        private object _monitorsSet;
        public object MonitorsSet
        {
            get { return _monitorsSet; }
            set { SetProperty(ref _monitorsSet, value); }
        }

        #endregion
    }
}
