///-------------------------------------------------------------------------------------------------
        /// <summary>   Добавление буллетина </summary>
        ///
        /// <remarks>   SV Milovanov, 05.02.2018. </remarks>
        ///
        /// <param name="signature">    Сигнатура группы </param>
        /// <param name="fields">       Словарь полей </param>
        ///-------------------------------------------------------------------------------------------------

        public void AddBulletin(GroupSignature signature, Dictionary <string, string> fields)
        {
            DCT.Execute(data =>
            {
                WebWorker.DownloadPage("https://www.avito.ru/additem", (doc) =>
                {
                    //1
                    if (!string.IsNullOrEmpty(signature.Category1))
                    {
                        var categoryRadio = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                            .FirstOrDefault(q => q.GetAttribute("type") == "radio" && q.GetAttribute("title") == signature.Category1);
                        if (categoryRadio == null)
                        {
                            return;
                        }
                        categoryRadio.InvokeMember("click");
                        Thread.Sleep(1000);
                    }
                    //2
                    if (!string.IsNullOrEmpty(signature.Category2))
                    {
                        var serviceRadio = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                           .FirstOrDefault(q => q.GetAttribute("type") == "radio" && q.GetAttribute("title") == signature.Category2);
                        if (serviceRadio == null)
                        {
                            return;
                        }
                        serviceRadio.InvokeMember("click");
                        Thread.Sleep(1000);
                    }
                    //3
                    if (!string.IsNullOrEmpty(signature.Category3))
                    {
                        var serviceTypeRadio = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                               .FirstOrDefault(q => q.GetAttribute("type") == "radio" && q.GetAttribute("title") == signature.Category3);
                        if (serviceTypeRadio == null)
                        {
                            return;
                        }
                        serviceTypeRadio.InvokeMember("click");
                        Thread.Sleep(1000);
                    }
                    //4
                    if (!string.IsNullOrEmpty(signature.Category4))
                    {
                        var serviceTypeRadio2 = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                                .FirstOrDefault(q => q.GetAttribute("type") == "radio" && q.GetAttribute("title") == signature.Category4);
                        if (serviceTypeRadio2 == null)
                        {
                            return;
                        }
                        serviceTypeRadio2.InvokeMember("click");
                        Thread.Sleep(1000);
                    }
                    //5
                    if (!string.IsNullOrEmpty(signature.Category5))
                    {
                        var serviceTypeRadio3 = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                                .FirstOrDefault(q => q.GetAttribute("type") == "radio" && q.GetAttribute("title") == signature.Category5);
                        if (serviceTypeRadio3 == null)
                        {
                            return;
                        }
                        serviceTypeRadio3.InvokeMember("click");
                        Thread.Sleep(1000);
                    }

                    var fieldSetter = FieldContainerList.Get(Uid);
                    fieldSetter.LoadFieldsFromGroup(signature);

                    foreach (var pair in fields)
                    {
                        var template = fieldSetter.Fields.FirstOrDefault(q => q.Key == pair.Key);
                        fieldSetter.SetFieldValue(template.Key, pair.Value);
                    }

                    //Продолжить с пакетом «Обычная продажа»
                    var radioButton = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                      .FirstOrDefault(q => q.GetAttribute("id") == "pack1");
                    if (radioButton != null)
                    {
                        radioButton.InvokeMember("click");
                    }


                    var buttons = WebWorker.WebDocument.GetElementsByTagName("button").Cast <HtmlElement>();
                    var pack    = "Продолжить с пакетом «Обычная продажа»";
                    var button  = buttons.FirstOrDefault(btn => btn.InnerText == pack);
                    if (button != null)
                    {
                        button.InvokeMember("click");
                    }

                    Thread.Sleep(1000);
                });
                WebWorker.DownloadPage("https://www.avito.ru/additem/confirm", (doc) =>
                {
                    ////Снимаем галочки
                    var servicePremium = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                         .FirstOrDefault(q => q.GetAttribute("id") == "service-premium");
                    if (servicePremium != null)
                    {
                        servicePremium.InvokeMember("click");
                    }
                    var serviceVip = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                     .FirstOrDefault(q => q.GetAttribute("id") == "service-vip");
                    if (serviceVip != null)
                    {
                        serviceVip.InvokeMember("click");
                    }
                    var serviceHighlight = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                           .FirstOrDefault(q => q.GetAttribute("id") == "service-highlight");
                    if (serviceHighlight != null)
                    {
                        serviceHighlight.InvokeMember("click");
                    }

                    ////Подтверждаем
                    var text           = "Продолжить";
                    var buttonContinue = WebWorker.WebDocument.GetElementsByTagName("button").Cast <HtmlElement>().FirstOrDefault(btn => btn.InnerText == text);
                    if (buttonContinue != null)
                    {
                        buttonContinue.InvokeMember("click");
                    }
                });
            });
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>   Отредактировать буллетин </summary>
        ///
        /// <remarks>   SV Milovanov, 05.02.2018. </remarks>
        ///
        /// <param name="bulletinId">   Идентификатор буллетина </param>
        /// <param name="fields">       Словарь полей </param>
        ///-------------------------------------------------------------------------------------------------

        public void EditBulletin(Guid bulletinId, Dictionary <string, string> fields)
        {
            DCT.Execute(data =>
            {
                var dbBulletinInstance = data.Db1.BulletinInstances.FirstOrDefault(q => q.Id == bulletinId);
                if (dbBulletinInstance != null)
                {
                    var dbGroup        = data.Db1.Groups.FirstOrDefault(q => q.Id == dbBulletinInstance.GroupId);
                    var groupContainer = GroupContainerList.Get(Uid);
                    var groupPackage   = groupContainer.GetGroupPackage(dbGroup.Hash);

                    WebWorker.Execute(() =>
                    {
                        DCT.Execute(d =>
                        {
                            WebWorker.NavigatePage(Path.Combine(dbBulletinInstance.Url, "edit"));
                            Thread.Sleep(1000);

                            var fieldSetter = FieldContainerList.Get(Uid);
                            fieldSetter.LoadFieldsFromGroup(new GroupSignature(groupPackage.Categories));

                            foreach (var pair in fields)
                            {
                                var template = fieldSetter.Fields.FirstOrDefault(q => q.Key == pair.Key);
                                fieldSetter.SetFieldValue(template.Value.Id, pair.Value);
                            }

                            if (EnumHelper.GetValue <BulletinInstanceState>(dbBulletinInstance.State) != BulletinInstanceState.Publicated)
                            {
                                //Продолжить с пакетом «Обычная продажа»
                                var radioButton = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>()
                                                  .FirstOrDefault(q => q.GetAttribute("id") == "pack1");
                                if (radioButton != null)
                                {
                                    radioButton.InvokeMember("click");
                                }


                                var buttons = WebWorker.WebDocument.GetElementsByTagName("button").Cast <HtmlElement>();
                                var pack    = "Продолжить без пакета";
                                var button  = buttons.FirstOrDefault(btn => btn.InnerText == pack);
                                if (button != null)
                                {
                                    button.InvokeMember("click");
                                }

                                Thread.Sleep(1000);
                            }
                            else
                            {
                                var button = WebWorker.WebDocument.GetElementsByTagName("button").Cast <HtmlElement>()
                                             .FirstOrDefault(q => q.GetAttribute("type") == "submit" && q.InnerText == "Продолжить");
                                if (button != null)
                                {
                                    button.InvokeMember("click");
                                }
                                Thread.Sleep(1000);
                            }
                        });
                    });
                }
            });
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>   Получаем список буллетинов учетки и сохраняем новые в БД </summary>
        ///
        /// <remarks>   SV Milovanov, 30.01.2018. </remarks>
        ///-------------------------------------------------------------------------------------------------

        public override void GetBulletins()
        {
            DCT.Execute(data =>
            {
                Exit();
                Auth();
                var context   = DCT.Context;
                var bulletins = new List <BulletinPackage>();
                var tabStates = new List <TabState>();
                WebWorker.Execute(() =>
                {
                    WebWorker.NavigatePage(ProfileUrl);

                    var tabs = WebWorker.WebDocument.GetElementsByTagName("li").Cast <HtmlElement>()
                               .Where(q => q.GetAttribute("className").Contains("tabs-item")).ToArray();

                    tabStates.Add(new TabState
                    {
                        Title = "Активные",
                        Href  = ProfileUrl,
                    });

                    foreach (var tab in tabs)
                    {
                        if (!tab.CanHaveChildren)
                        {
                            continue;
                        }

                        foreach (HtmlElement ch in tab.Children)
                        {
                            if (ch.TagName.ToLower() == "a")
                            {
                                var tabUrl   = ch.GetAttribute("href");
                                var tabState = ch.InnerText;

                                tabStates.Add(new TabState
                                {
                                    Title = tabState,
                                    Href  = tabUrl,
                                });
                            }
                        }
                    }
                });
                WebWorker.Execute(() =>
                {
                    foreach (var tabState in tabStates)
                    {
                        WebWorker.NavigatePage(tabState.Href);

                        var nextPages = new List <string>
                        {
                            tabState.Href
                        };

                        var hasNextPage = true;
                        do
                        {
                            var result = GetBulletinPages(tabState.Title);
                            bulletins.AddRange(result);

                            var nextPage = WebWorker.WebDocument.GetElementsByTagName("a").Cast <HtmlElement>()
                                           .FirstOrDefault(q => q.GetAttribute("className").Contains("js-pagination-next"));
                            if (nextPage == null)
                            {
                                hasNextPage = false;
                            }
                            else
                            {
                                var nextPageHref = nextPage.GetAttribute("href");
                                nextPages.Add(nextPageHref);
                                hasNextPage = true;
                                nextPage.InvokeMember("click");
                                Thread.Sleep(1000);
                            }
                        } while (hasNextPage);
                    }
                });

                WebWorker.Execute(() =>
                {
                    var groupContainer = GroupContainerList.Get(Uid);
                    foreach (var bulletin in bulletins)
                    {
                        var url = Path.Combine(bulletin.Url, "edit");
                        WebWorker.NavigatePage(url);
                        Thread.Sleep(1500);

                        var groupElement = WebWorker.WebDocument.GetElementsByTagName("div").Cast <HtmlElement>()
                                           .FirstOrDefault(q => q.GetAttribute("className").Contains("form-category-path"));

                        if (groupElement == null)
                        {
                            continue;
                        }

                        var categories     = groupElement.InnerText.Split('/').Select(q => q.Trim()).ToArray();
                        bulletin.Signature = new GroupSignature(categories);

                        var fieldSetter = FieldContainerList.Get(Uid);
                        fieldSetter.LoadFieldsFromGroup(bulletin.Signature);

                        var dictionary = new Dictionary <string, string>();
                        foreach (var field in fieldSetter.Fields)
                        {
                            var value = fieldSetter.GetFieldValue(field.Value.Id);
                            dictionary.Add(field.Key, value);
                        }
                        bulletin.Fields = dictionary;
                    }
                });

                WebWorker.Execute(() =>
                {
                    DCT.Execute(d =>
                    {
                        foreach (var bulletin in bulletins)
                        {
                            var url = bulletin.Url;
                            var bs  = d.Db1.BulletinInstances.ToArray();
                            var dbBulletinInstance = d.Db1.BulletinInstances.FirstOrDefault(q => q.Url == url);
                            if (dbBulletinInstance == null)
                            {
                                var dbBulletin = new Bulletin
                                {
                                    UserId = d.Objects.CurrentUser.Id,
                                };
                                d.Db1.Bulletins.Add(dbBulletin);
                                d.Db1.SaveChanges();

                                var groupId = Guid.Empty;
                                if (bulletin.Signature != null)
                                {
                                    var groupHash = bulletin.Signature.GetHash();

                                    var dbGroup = d.Db1.Groups.FirstOrDefault(q => q.Hash == groupHash);
                                    groupId     = dbGroup.Id;
                                }

                                dbBulletinInstance = new BulletinInstance
                                {
                                    GroupId      = groupId,
                                    AccessId     = d.Objects.CurrentAccess.Id,
                                    BoardId      = Uid,
                                    BulletinId   = dbBulletin.Id,
                                    HasRemoved   = false,
                                    LastChangeId = null,
                                    Url          = bulletin.Url,
                                    State        = 0,
                                };
                                d.Db1.BulletinInstances.Add(dbBulletinInstance);
                                d.Db1.SaveChanges();

                                if (bulletin.Fields == null)
                                {
                                    continue;
                                }

                                foreach (var field in bulletin.Fields)
                                {
                                    var dbFieldTemplate = d.Db1.FieldTemplates.FirstOrDefault(q => q.Name == field.Key);
                                    if (dbFieldTemplate != null)
                                    {
                                        var dbBulletinField = d.Db1.BulletinFields.FirstOrDefault(q => q.BulletinInstanceId == dbBulletinInstance.Id && q.FieldId == dbFieldTemplate.Id);
                                        if (dbBulletinField == null)
                                        {
                                            dbBulletinField = new BulletinField
                                            {
                                                BulletinInstanceId = dbBulletinInstance.Id,
                                                FieldId            = dbFieldTemplate.Id,
                                                Value = field.Value,
                                            };
                                            d.Db1.BulletinFields.Add(dbBulletinField);
                                            d.Db1.SaveChanges();
                                        }
                                    }
                                }
                            }
                        }
                    });
                });
                //WebWorker.Execute(() =>
                //{
                //    var groupContainer = GroupContainerList.Get(Uid);
                //    foreach (var bulletin in bulletins)
                //    {
                //        var url = Path.Combine(bulletin.Url, "edit");
                //        WebWorker.NavigatePage(url);
                //        Thread.Sleep(1500);

                //        var groupElement = WebWorker.WebDocument.GetElementsByTagName("div").Cast<HtmlElement>()
                //            .FirstOrDefault(q => q.GetAttribute("className").Contains("form-category-path"));

                //        if (groupElement == null) continue;

                //        var categories = groupElement.InnerText.Split('/').Select(q => q.Trim()).ToArray();
                //        bulletin.Signature = new GroupSignature(categories);

                //        var group = groupContainer.Get(bulletin.Signature.GetHash());

                //        if (group == null) continue;

                //        var fieldSetter = FieldContainerList.Get(Uid);
                //        fieldSetter.LoadFieldsFromGroup(bulletin.Signature);

                //        var dictionary = new Dictionary<string, string>();
                //        foreach (var field in group.Fields)
                //        {
                //            var value = fieldSetter.GetField(field.Key);
                //            dictionary.Add(field.Key, value);
                //        }
                //        bulletin.Fields = dictionary;
                //    }


                //});

                //WebWorker.Execute(() =>
                //{
                //    var groups = bulletins.Where(q => q.Fields != null && q.Fields.Count > 0).GroupBy(q => q.Signature.ToString());
                //    foreach (var groupedBulletins in groups)
                //    {
                //        var xls = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), $"edit_bulletins[{groupedBulletins.Key}].xlsx"));
                //        if (xls.Exists)
                //            xls.Delete();
                //        var firstBulletin = groupedBulletins.FirstOrDefault();
                //        using (var package = new ExcelPackage(xls))
                //        {
                //            var worksheet = package.Workbook.Worksheets.Add("Мои объявления");
                //            var keys = firstBulletin.Fields.Keys;
                //            var count = 0;
                //            foreach (var k in keys)
                //            {
                //                var cell = worksheet.Cells[1, count + 1];
                //                cell.Style.Font.Size = 14;
                //                cell.Value = k;
                //                cell.AutoFitColumns();
                //                count++;
                //            }
                //            var row = 2;
                //            foreach (var bulletin in groupedBulletins)
                //            {
                //                var column = 0;
                //                foreach (var k in keys)
                //                {
                //                    var cell = worksheet.Cells[row, column + 1];
                //                    var field = bulletin.Fields[k];
                //                    cell.Value = field;
                //                    column++;
                //                }

                //                worksheet.Column(column + 1).Hidden = true;
                //                var urlCell = worksheet.Cells[row, column + 1];
                //                urlCell.Value = bulletin.Url;

                //                worksheet.Column(column + 2).Hidden = true;
                //                var stateCell = worksheet.Cells[row, column + 2];
                //                stateCell.Value = bulletin.State;
                //                row++;
                //            }

                //            package.Save();
                //        }

                //    }
                //});
            });
        }