#!/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 PyInquirer import style_from_dict, Token, prompt, Separator
from PyInquirer import Validator, ValidationError
profile=getProfile()
myMarket=profile.market
myColor=profile.market.exports[0]

markets=Market.all()
marketNames=[m['name'] for m in markets if m['name'] != myMarket.name]


parser = argparse.ArgumentParser(description='Utility used to send out a convoy to a market.')
parser.add_argument('market',choices=marketNames, type=str, help="The name of the market you want to transact in.")
parser.add_argument('quantity', type=int, help="The amount of {} pigment you want trade.".format(myColor))
parser.add_argument('money', type=int, help="The amount of money you are willing to give the convoy to buy other pigments.")
parser.add_argument('horses', type=int, help="The number of horses you want on your convoy. The more horses the faster you get there but the more the convoy costs.")

horses=None
money=None
quantity=None
market=None
if len(sys.argv)==1:
    questions=[
        {
            'type': 'list',
            'name': 'market',
            'message': 'Which market would you like to send your convoy to? The market contains other players like you with their own prices for their pigments. If your buy and sell price is set correctly your convoy will make a transaction automatically for you.',
            'choices': marketNames,
        },
        {
            'type': 'input',
            'name': 'quantity',
            'message': 'How many {} pigments do you want load into the convoy to sell?'.format(myColor),
        },
        {
            'type': 'input',
            'name': 'horses',
            'message': 'How many horses do you want to carry your convoy? The more horses the faster you get to your destination but the more expensive the convoy.',
        },
        {
            'type': 'input',
            'name': 'money',
            #Try to make this a yes or no question.
            'message': 'While you are there you can take the time to buy other pigment from the players in the other market. How much money do you want to give your convoy to buy other pigments (enter 0 if you don\'t want to buy pigments)? The more money you give the convoy the more pigments it can purchase. Be sure to set your buy price, using the update command, prior to sending out a convoy. To cancel hit control-C.',
        },
    ]
    answers=prompt(questions)
    horses=int(answers['horses'])
    quantity=int(answers['quantity'])
    market=answers['market']
    money=float(answers['money'])
    print(answers)
else:
    args=parser.parse_args()
    horses=int(args.horses)
    quantity=int(args.quantity)
    market=args.market
    money=float(args.money)
    print(horses,quantity,market,money)

convoy=profile.createConvoy(horses,money,quantity)
dest=Market.load(market)
businesses=[(b,100)for b in dest.businesses]
convoy.gotoMarket(dest,businesses)


