""" Handler module for parsing the wizzards website for metadata
on magic cards """

import os

from urllib.request import urlopen
from bs4 import BeautifulSoup
import html5lib


def make_mana_dict():
    mana_dict = dict()
    mana_colors_list = [
        'Green', 'Red', 'Blue', 'Black', 'White',
        'Phyrexian Blue', 'Phyrexian Red', 'Phyrexian Green',
        'Phyrexian Black', 'Phyrexian White',
        '1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
        '11', '12', '13', '14', '15', '16', '1000000',
        'Black or Green', 'Red or White', 'Green or Blue',
        'Blue or Red', 'White or Black', 'White or Blue',
        'Blue or Black', 'Black or Red', 'Red or Green',
        'Green or White', 'Two or White', 'Two or Green',
        'Two or Black', 'Two or Red', 'Two or Blue'
    ]

    config_file = open(os.path.dirname(
        os.path.abspath(__file__)) + '/' + 'mana_config.txt', "r")
    counter = 0

    for mana_emoji_id in config_file.readlines():
        mana_dict[mana_colors_list[counter]] = mana_emoji_id
        counter += 1

    config_file.close()
    return mana_dict


MANA_DICT = make_mana_dict()
WIZZARDS_BASE_URL = 'http://gatherer.wizards.com/'


def parse_cardtextbox(cardtextbox_div):
    # print("parse_cardtextbox:", cardtextbox_div)
    def extract_content(content):
        content_string = ""
        try:
            icon_content = content['alt']
            icon_string = MANA_DICT[icon_content].strip()
            return " " + icon_string + " "
        except (TypeError, KeyError):
            pass

        try:
            for content_prt in content.contents:
                content_string = content_string + extract_content(content_prt)
        except AttributeError:
            return content
        # print("parse_cardtextbox output:")
        return content_string

    content_string = extract_content(cardtextbox_div)

    # cleaning content_string for random newlines
    content_string = content_string.lstrip("\\n").strip()
    return content_string


def parse_base_value(value_div):
    # print("parse_base_value:",value_div)
    value_string = value_div.string.strip()
    # print("parse_base_value output:", value_string)
    import re
    value_string = re.sub('â€”', '--- ', value_string)
    return value_string


def parse_mana(mana_div):
    mana_string = ""
    for mana_content in mana_div.children:
        try:
            mana_string = mana_string + \
                MANA_DICT[mana_content['alt']].strip() + " "
        except TypeError:
            pass
    # print("parse_mana output:", mana_string)
    return mana_string


def parse_image(image_div):
    # print("parse_image:", image_div)
    image_handler = image_div.img['src'].lstrip('../../')
    # print("parse_image output:", image_handler)
    return image_handler


def parse_rarity(rarity_div):
    # print("parse_rarity:", rarity_div)
    rarity = rarity_div.span.string
    # print("parse_rarity output:", rarity)
    return rarity


def parse_artist(artist_div):
    # print("parse_artist:", artist_div)
    artist = artist_div.a.string
    # print("artist_div output:", artist)
    return artist


def scrape_wizzards(url='http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=74626'):
    '''
    ARGS:

    RETURNS:
    '''
    html = urlopen(url)
    soup = BeautifulSoup(html, 'html5lib')

    label_dict = {
        'Community Rating': 'skip',
        'Card Name': parse_base_value,
        'Mana Cost': parse_mana,
        'Converted Mana Cost': parse_base_value,
        'Types': parse_base_value,
        'Card Text': parse_cardtextbox,
        'P/T': parse_base_value,
        'Expansion': 'skip',
        'Rarity': parse_rarity,
        'All Sets': 'skip',
        'Card Number': parse_base_value,
        'Artist': parse_artist,
        'Flavor Text': parse_cardtextbox
    }

    card_data = dict()
    label_list = soup.find_all('div', class_='label')
    for label_div in label_list:
        for string in label_div.stripped_strings:
            label_name = string.strip(':')
            try:
                if label_dict[label_name] != 'skip':
                    card_data[label_name] = \
                        label_dict[label_name](label_div.find_next_sibling('div'))
            except KeyError:
                print("unrecognised label:", label_name)
                pass

    card_image_div = soup.find('div', class_='cardImage')
    card_data['image_url'] = WIZZARDS_BASE_URL + parse_image(card_image_div)
    return card_data


def card_data2string(card_data):

    element_list = ['Card Name', 'Mana Cost', 'Types', 'Rarity', 'Card Text',
                    'Flavor Text', 'P/T', 'Artist', 'image_url']
    out_string = ''
    for element in element_list:
        if element in card_data:
            if element == 'image_url':
                out_string = out_string + card_data[element]
            else:
                out_string = out_string + element + ": " + card_data[element] + '\n'

    return out_string


if __name__ == "__main__":
    card_data2string(scrape_wizzards('http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=383242'))
    card_data2string(scrape_wizzards('http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=212578'))
    card_data2string(scrape_wizzards('http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=220517'))
