using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZionRobot.Tasks
{
    class Output
    {
        private bool running;
        private Handlers.Browser handler; 
        private int limitTime = 1200;
        private System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

        public bool Running { get => this.running; }

        public Output()
        {
            this.running = false;
        }

        public void Execute(Models.Source.Origin source, Models.Items.File file) //Models.Process.Workflow workflow)
        {
            running = true;
            stopwatch.Start();
            System.Windows.Application.Current.Dispatcher.Invoke((System.Action)delegate
            {
                this.handler = Handlers.Browser.Instance;
            });


            new System.Threading.Thread(() => {
                System.Threading.Thread t = new System.Threading.Thread(() =>
                {
                    try
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke((System.Action)delegate
                        {
                            map(file);

                            handler.Source = source;

                            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(Persistence.Basic.DoneFolder, source.Title));
                            handler.ExecuteAndMove(file, System.IO.Path.Combine(Persistence.Basic.DoneFolder, source.Title));
                        });
                    }
                    catch (Exception ex)
                    {

                        file.Status = Persistence.Basic.ERROR;
                    }
                });

                t.Start();

                new System.Threading.Thread(() => {

                    while (handler.Main == null || handler.Main.IsAlive)
                    {
                        if (stopwatch.Elapsed.TotalSeconds > limitTime)
                            break;

                        System.Threading.Thread.Sleep(1000);
                    }

                    stopwatch.Stop();
                    stopwatch.Reset();

                    System.Windows.Application.Current.Dispatcher.Invoke((System.Action)delegate
                    {
                        handler.Close();
                    });

                    running = false;

                }).Start();
            }).Start();
        }
        public void Execute(Models.Source.Origin source) //Models.Process.Workflow workflow)
        {
            running = true;
            stopwatch.Start();
            System.Windows.Application.Current.Dispatcher.Invoke((System.Action)delegate
            {
                this.handler = Handlers.Browser.Instance;
            });


            new System.Threading.Thread(() => {
                System.Threading.Thread t = new System.Threading.Thread(() =>
                {
                    try
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke((System.Action)delegate
                        {
                            handler.Source = source;

                            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(Persistence.Basic.DoneFolder, source.Title));
                            handler.ExecuteDemo();
                        });
                    }
                    catch (Exception ex)
                    {

                    }
                });

                t.Start();

                new System.Threading.Thread(() => {

                    while (handler.Main == null || handler.Main.IsAlive)
                    {
                        if (stopwatch.Elapsed.TotalSeconds > limitTime)
                            break;

                        System.Threading.Thread.Sleep(1000);
                    }

                    stopwatch.Stop();
                    stopwatch.Reset();

                    System.Windows.Application.Current.Dispatcher.Invoke((System.Action)delegate
                    {
                        handler.Close();
                    });

                    running = false;

                }).Start();
            }).Start();
        }

        private void map(Models.Items.File file)
        {
            file.METAObject.Pages.Select(p => p.SearchElements).ToList().ForEach(PageElements =>
            {
                PageElements.ToList().ForEach(f =>
                {
                    if (f.Type == @"Date")
                    {
                        f.Value = DateTime.Parse(f.Value).ToString(@"dd/MM/yyyy");
                    }
                    handler.DictionaryValues.Add(f.FullName, f.Value);
                });
            });
        }
    }
}
