#! /usr/bin/env python

import click
from datetime import datetime
from os import environ
import dateparser

from optionstracker.value import value
from optionstracker.price import get_price
from optionstracker.vesting import vested

mirriad_price_url = (
    'https://www.londonstockexchange.com/'
    'stock/MIRI/mirriad-advertising-plc/company-page')

@click.command()
@click.option('--grant-date', default='2020-05-18')
@click.option('--lse-price-url', default=mirriad_price_url,
    help='default: {}'.format(mirriad_price_url))
@click.option('--vesting-rate', type=float, default=0.0277,
    help='default: 0.0277')
@click.option('--vesting-interval', default='m',
    help='default: m. m=monthly y=annually')
@click.argument('grant-quantity', type=int)
@click.argument('grant-price-pence', type=int)
def track_options(
    grant_quantity, grant_price_pence, vesting_rate, lse_price_url, grant_date,
    vesting_interval):
  print('Getting market prices...')
  bid, offer = get_price(lse_price_url)
  print('Market Bid: {}p'.format(bid))
  grant_date = dateparser.parse(grant_date)
  print('Options: {:,d} @ {}p granted on {}'.format(
      grant_quantity, grant_price_pence, grant_date.strftime('%x')))
  vested_quantity, _ = vested(
      grant_quantity, datetime.now(), grant_date, vesting_rate,
      vesting_interval)
  print('Vested Quantity:', vested_quantity)
  print('Total Value: £{:,.2f}'.format(
      value(grant_quantity, grant_price_pence, float(bid)) / 100))
  print('Vested Value: £{:,.2f}'.format(
      value(vested_quantity, grant_price_pence, float(bid)) / 100))


if __name__ == '__main__':
  track_options()
