__author__ = 'Helium'

# Here, we are importing the auxiliary functions to clean or convert data
from Forums.Utilities.utilities import *
from datetime import date
from datetime import timedelta
import re

# Here, we are importing BeautifulSoup to search through the HTML tree
from bs4 import BeautifulSoup, ResultSet, Tag

# This is the method to parse the Description Pages (one page to each topic in the Listing Pages)


def procrax_description_parser(soup: Tag):

    # Fields to be parsed

    topic = "-1"           # topic name
    user = []              # all users of each post
    addDate = []           # all dated of each post
    feedback = []          # all feedbacks of each vendor (this was found in just one Forum and with a number format)
    status = []            # all user's authority in each post such as (adm, member, dangerous)
    reputation = []        # all user's karma in each post (usually found as a number)
    sign = []              # all user's signature in each post (usually a standard message after the content of the post)
    post = []              # all messages of each post
    interest = []          # all user's interest in each post

    # Finding the topic (should be just one coming from the Listing Page)

    li = soup.find("h1", {"class": "p-title-value"})
    topic = li.text
    
    thread: ResultSet[Tag] = soup.find("div", {"class": "block-body js-replyNewMessageContainer"}).find_all("article", {"data-author": True})

    for ipost in thread:
        username = ipost.find("h4", {"class": "message-name"}).text
        user.append(cleanString(username.strip()))
        
        date_posted = ipost.find("ul", {"class": "message-attribution-main listInline"}).find("time").get("datetime")
        datetime_obj = datetime.strptime(date_posted, "%Y-%m-%dT%H:%M:%S%z")
        addDate.append(datetime_obj)
        
        
        feedback.append("-1")
        
        user_status = ipost.find("h5", {"class": "userTitle message-userTitle"}).text
        status.append(cleanString(user_status.strip()))
        
        user_lvl = ipost.find("div", {"class": "afAwardLevel"}).text
        reputation.append(cleanString(user_lvl.strip()))
        
        sign.append("-1")
        
        user_post = ipost.find("article", {"class": "message-body js-selectToQuote"}).text
        post.append(cleanString(user_post.strip()))
        
        interest.append("-1")
        
        

    # Populate the final variable (this should be a list with all fields scraped)

    row = (topic, user, status, reputation, interest, sign, post, feedback, addDate)

    # Sending the results

    return row

# This is the method to parse the Listing Pages (one page with many posts)

def procrax_listing_parser(soup: Tag):

    board = "-1"       # board name (the previous level of the topic in the Forum categorization tree.
                       # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)

    nm = 0             # this variable should receive the number of topics
    topic = []         # all topics
    author = []        # all authors of each topic
    views = []         # number of views of each topic
    posts = []         # number of posts of each topic
    addDate = []       # when the topic was created (difficult to find)
    href = []          # this variable should receive all cleaned urls (we will use this to do the marge between
                       # Listing and Description pages)

    # Finding the board (should be just one)
    li = soup.find("h1", {"class": "p-title-value"})
    board = cleanString(li.text.strip())

    threads_list: ResultSet[Tag] = soup.find("div", {"class": "structItemContainer-group js-threadList"}).find_all("div", {"data-author": True})
    
    nm = len(threads_list)
    
    for thread in threads_list:
        thread_title = thread.find("div", {"class": "structItem-title"}).text
        topic.append(cleanString(thread_title.strip()))
        
        thread_author = thread.get("data-author")
        author.append(cleanString(thread_author))
        
        thread_views = thread.find("dl", {"class": "pairs pairs--justified structItem-minor"}).find('dd').text
        views.append(cleanString(thread_views.strip()))
        
        thread_replies = thread.find("dl", {"class": "pairs pairs--justified"}).find('dd').text
        # All threads contain one topic post and reply posts
        thread_total_posts = str(1 + int(thread_replies))
        posts.append(thread_total_posts)
        
        thread_date = thread.find("li", {"class": "structItem-startDate"}).find("time").get("datetime")
        datetime_obj = datetime.strptime(thread_date, "%Y-%m-%dT%H:%M:%S%z")
        addDate.append(datetime_obj)
        
        thread_link: str = thread.find("div", {"class": "structItem-title"}).find('a').get('href')
        href.append(thread_link)
    
    
    return organizeTopics(
        forum="Procrax",
        nm=nm,
        board=board,
        author=author,
        topic=topic,
        views=views,
        posts=posts,
        addDate=addDate,
        href=href
    )


def procrax_links_parser(soup):

    # Returning all links that should be visited by the Crawler

    href = []

    listing = soup.find_all('div', {"class": "structItem-title"})

    for a in listing:
        link = a.find('a', {'class': ''}).get('href')

        href.append(link)

    return href