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

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

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

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

        public void Execute(Models.Source.Origin source, Models.ZFlow.Activity activity)
        {
            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;


                            if (activity.Action.ToUpper() == @"UPDATE" && !string.IsNullOrEmpty(activity.DataJuri))
                            {
                                handler.UpdateActivity(activity);

                            }else if (activity.Action.ToUpper() == @"CREATE")
                            {
                                handler.CreateSubActivity(activity);
                            } else if (activity.Action.ToUpper() == @"CREATECASE")
                            {
                                handler.CreateCase(activity);
                            } else if (activity.Action.ToUpper() == @"CLOSE")
                            {
                                handler.CloseCase(activity);
                            } else if (activity.Action.ToUpper() == @"SHAREPOINT")
                            {
                                handler.UploadAttachments(activity);
                            } 
                            //else
                            //{
                            //    activity.UpdateActivitySycn(5);
                            //}

                        });
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                });

                t.Start();

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

                    while (handler.Main == null || handler.Main.IsAlive)
                    {
                        if (stopwatch.Elapsed.TotalSeconds > limitTime)
                        {
                            activity.UpdateActivitySycn(3);
                            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.ZFlow.Lawsuit lawsuit)
        {
            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;


                             if (lawsuit.SyncAction.ToUpper() == @"UPDATE")
                             {
                                 handler.UpdateLawsuit(lawsuit);
                             }

                            if (lawsuit.SyncAction.ToUpper() == @"CREATE")
                            {
                                handler.CreateLawsuit(lawsuit);
                            }

                        });
                    }
                    catch (Exception ex)
                    {
                        throw 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();
        }
        
        public void Execute(Models.Source.Origin source, Models.ZFlow.LawsuitPhase lawsuitPhase)
        {
            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;


                            // if (lawsuit.Action.ToUpper() == @"UPDATE" )

                            if (lawsuitPhase.SyncAction.ToUpper() == @"CREATE")
                            {
                                handler.CreateLawsuitPhase(lawsuitPhase);
                            }

                        });
                    }
                    catch (Exception ex)
                    {
                        throw 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();
        }
        
        public void Execute(Models.Source.Origin source, Models.ZFlow.LawsuitPhaseProgress lawsuitPhaseProgress)
        {
            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;


                            // if (lawsuit.Action.ToUpper() == @"UPDATE" )

                            if (lawsuitPhaseProgress.SyncAction.ToUpper() == @"CREATE")
                            {
                                handler.CreateLawsuitPhaseProgress(lawsuitPhaseProgress);
                            }

                        });
                    }
                    catch (Exception ex)
                    {
                        throw 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();
        }
        public void Execute(Models.Source.Origin source, Models.ZFlow.LawsuitDemand lawsuitDemand)
        {
            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;


                            // if (lawsuit.Action.ToUpper() == @"UPDATE" )

                            if (lawsuitDemand.SyncAction.ToUpper() == @"CREATE")
                            {
                                handler.CreateLawsuitDemand(lawsuitDemand);
                            }

                        });
                    }
                    catch (Exception ex)
                    {
                        throw 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();
        }


        public void SyncFromLocal(Models.Source.Origin source, string json, string type)
        {
            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;
                            Models.Source.BlockElement blockElement = null;

                            // if (lawsuit.Action.ToUpper() == @"UPDATE" )
                            if (type.ToUpper() == "ANDAMENTOS")
                            {
                                blockElement = source.BlockElements.Where(be => be.Name == @"CreateLawsuitPhaseProgressLocal").FirstOrDefault();
                            }
                            
                            if(type.ToUpper() == "DEPOSITOS")
                            {
                                blockElement = source.BlockElements.Where(be => be.Name == @"CreateLawsuitDepositLocal").FirstOrDefault();
                            }

                            if (type.ToUpper() == "PEDIDOS")
                            {
                                blockElement = source.BlockElements.Where(be => be.Name == @"CreateLawsuitDemandLocal").FirstOrDefault();
                            }

                            if(type.ToUpper() == "PROCESSOS")
                            {
                                blockElement = source.BlockElements.Where(be => be.Name == @"CreateLawsuitLocal").FirstOrDefault();
                            }

                            handler.CreateWithLocal(json, blockElement);


                        });
                    }
                    catch (Exception ex)
                    {
                        throw 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 string convertDate(string value)
        {
            return DateTime.Parse(value, new System.Globalization.CultureInfo("pt-BR")).ToString(@"dd/MM/yyyy");
        }
    }
}
