#!/usr/bin/python

import re
import requests
from bs4 import BeautifulSoup
from dataclasses import dataclass, field

def get_price_in_gbp(price):
    price_str = re.search(r'\d?,?\d+\.\d+', price).group()
    price_str_no_comma = re.sub(",", "", price_str)
    price_usd = float(price_str_no_comma)
    price_gbp = usd_to_gbp(price_usd)
    return price_gbp

def usd_to_gbp(dollars):
    return round(dollars * 0.8, 2)

def get_dawnglare_html():
    response = requests.get("https://mtg.dawnglare.com/?p=sets&pack=1")

    if response.status_code != 200:
        print("Request to Dawnglare failed with status code: " + response.status_code)
        os.exit(1)

    return response.text

@dataclass(order=True)
class ExpectedValue():
    setName: str = field(compare=False)
    boosterPackEV: float = field(compare=True)

    @property
    def prereleasePackEV(self):
        return self.boosterPackEV * 6

def get_expected_values(soup):
    prices = soup.find_all(class_="cP")
    names = soup.find_all(class_="cN")

    evs = []
    for i in range(0, len(prices)):
        setName = names[i].text
        boosterPackEV = get_price_in_gbp(prices[i].text)
        ev = ExpectedValue(setName, boosterPackEV)
        evs.append(ev)

    return evs


def main():
    html = get_dawnglare_html()
    soup = BeautifulSoup(html, features="lxml")
    evs = get_expected_values(soup)
    sorted_by_ev = sorted(evs, reverse=True)

    print(f"{'Set Name':<40} | Prerelease Kit EV")
    print("-" * 60)
    for setEV in sorted_by_ev:
        print(f'{setEV.setName:<40} | #{setEV.prereleasePackEV:,.2f}')

if __name__ == "__main__":
    main()
