#!/usr/bin/env python
import sys
sys.path.insert(0,'../')

try:
    from Market import Market
    from Business import Business
    from db import getProfile
except:
    from mural.Market import Market
    from mural.Business import Business
    from mural.db import getProfile

import argparse
from pymongo import MongoClient
from getpass import getpass
from PyInquirer import style_from_dict, Token, prompt, Separator,Validator, ValidationError
parser = argparse.ArgumentParser(description='Utility to update the sell or buy price on your account.\n The buy price is the amount you are willing to buy a pigment at.\nThe sell price is the amount you are willing to sell a pigment at.')
parser.add_argument('type', type=str, choices=['buy','sell'],help="Are you updating the sell price or the bid price.")
parser.add_argument('pigment', choices=['red','green','blue'],type=str, help="Which commodity are you trying to update?")
parser.add_argument('price', type=float, nargs=1, help="The new value you want to set.")
print('Displaying your profile:')
profile=getProfile()
profile.display()
if len(sys.argv) == 1:
    questions=[
        {
            'type': 'list',
            'name': 'pigment',
            'message': 'Which color are you trying to set the price for?',
            'choices': ['Red','Green','Blue'],
        },
        {
            'type': 'list',
            'name': 'tradeType',
            'message': 'Do you want to set the buy price or the sell price? ',
            'choices': ['Buy','Sell'],
        },
        {
            'type': 'input',
            'name': 'price',
            'message': 'Please enter the new price:',
        },

    ]
    answers=prompt(questions)
    pigment=answers['pigment']
    tradeType=answers['tradeType']
    price=float(answers['price'])
else:
    args=parser.parse_args()
    tradeType=args.type
    pigment=args.pigment.title()
    price=args.price[0]
    
print("Updating your configuration.")
if tradeType.lower() == 'buy':
    profile.stock[pigment].buyPrice=price
elif tradeType.lower() == 'sell':
    profile.stock[pigment].sellPrice=price
profile.save()
profile.display(pigment)
