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

namespace Post
{
    class Post_parser
    {
        HtmlDocument HTML;
        public int count_unreal_user = 0;
        List<HtmlNode> HTML_LIST = new List<HtmlNode>();
        public Post_parser(string html_data, ref List<user> userarr)
        {
            HtmlNode post;
            HTML = new HtmlDocument();
            HTML.Load(html_data, true);
            if ((post = HTML.DocumentNode.SelectSingleNode("//*[@id='postrussia']")) == null)
            {
                Console.WriteLine("Not Found 'POST_BLOCKS'");
                return;
            }
            var table = post.SelectNodes(".//div");
            foreach (var table_element in table)
            {
                if (table_element.Id != null)
                {
                    if (table_element.Id.Contains("delivery_block_"))
                    {
                        HTML_LIST.Add(table_element);
                        userarr.Add(create_user(get_user_param(table_element)));
                    }
                }
                else
                {
                    continue;
                }
            }
            count_unreal_user = HTML_LIST.Count;
        }

        private user create_user(object[] v)
        {
            user u = new user();
            u.name = (string)(v[0]);
            u.id = (string)(v[1]);
            u.org_note = (string)(v[2]);
            u.Address = (string)(v[3]);
            u.ocenka = (string)(v[4]);
            return u;
        }

        private object[] get_user_param(HtmlNode table_element)
        {
            if (table_element == null) return null;
            HtmlNode user_block = table_element.SelectSingleNode(".//span[@class='username']"); // -------------UserName
            HtmlNode user_name = user_block.SelectSingleNode("img[@class='cptcb']");
            string user_name_str = user_name.Attributes["data-clipboard-text"].Value;           // -------------UserName


            HtmlNode org_notes = table_element.SelectSingleNode("span[@class='orgnote']"); // ----------OrgNotes
            string org_notes_str = org_notes.InnerText;                                    // ----------OrgNotes


            HtmlNode adress_note = table_element.SelectSingleNode("div[@class='address']"); // ----------AddrNote
            string adress_note_str = adress_note.InnerText;                                 // ----------AddrNote


            HtmlNode ocenka_note = table_element.SelectSingleNode("span[@class = 'title']"); // ----------OcenkaNote
            string ocenka_not_str = ocenka_note.InnerText;                                   // ----------OcenkaNote
            

            HtmlNode user_id = table_element.SelectSingleNode("span[@class = 'ftitle']"); // ----------id
            string user_id_str = user_id.Id;                                              // ----------id

            return new object[] { user_name_str, user_id_str, org_notes_str, adress_note_str, ocenka_not_str };
        }

        void get_delivery_blocks()
        {
            //userarr.Sort(delegate (user x, user y)
            //{
            //    if (x.name == null && y.name == null) return 0;
            //    else if (x.name == null) return -1;
            //    else if (y.name == null) return 1;
            //    else return x.name.CompareTo(y.name);
            //}); }
        }
    }
}

