def activate_ngn(update, context):
    query = update.callback_query
    bot = context.bot

    # database functions
    binary_db = BinaryDB()

    # check if a deposit has been made into the the ngn account
    res = binary_db.db.users.find_one({"tgram_uid": query.message.chat_id},
                                      ['account'])
    if res['account']['ngn_balance'] <= 0:
        bot.edit_message_text(
            chat_id=query.message.chat_id,
            message_id=query.message.message_id,
            text=
            "You have to make a deposit before activating your NGN account",
            reply_markup=reply_markup)

        return THIRD

    res = binary_db.switch_acount(query.message.chat_id, 'ngn')
    print(res)
    context.user_data['default_acct'] = res['active_acct']

    reply_markup = InlineKeyboardMarkup(button_states['THIRD']['ACTIVATE_NGN'])

    #GET TRADE HISTORY OF THE USER AND RETURN
    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text=
        "Your NGN account is currently active and set to default trade account",
        reply_markup=reply_markup)

    return THIRD
def withdraw(update, context):
    query = update.callback_query
    chat_id = query.message.chat_id
    bot = context.bot
    bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)

    binary_db = BinaryDB()
    res = binary_db.db.users.find_one({"tgram_uid": query.message.chat_id},
                                      ['active_acct', 'account'])

    reply_markup = InlineKeyboardMarkup(button_states['SECOND']['WITHDRAW'])

    if res['account']['ngn_balance'] == None or res['account'][
            'ngn_balance'] == 0:
        bot.edit_message_text(
            chat_id=query.message.chat_id,
            message_id=query.message.message_id,
            text="Sorry you do not have any funds to withdraw at this time",
            reply_markup=reply_markup)

        return WITHDRAW_AMOUNT

    bot.edit_message_text(chat_id=query.message.chat_id,
                          message_id=query.message.message_id,
                          text="Reply with the amount you want to withdraw",
                          reply_markup=reply_markup)

    return WITHDRAW_AMOUNT
def balance(update, context):
    query = update.callback_query
    bot = context.bot
    user_id = query.message.chat_id
    bot.send_chat_action(chat_id=user_id, action=ChatAction.TYPING)

    # database functions
    binary_db = BinaryDB()
    res = binary_db.db.users.find_one({"tgram_uid": user_id}, ['account'])
    # print(res)
    demo_bal = res['account']['demo_balance']
    ngn_bal = res['account']['ngn_balance']

    context.user_data['demo_bal'] = demo_bal

    reply_markup = InlineKeyboardMarkup(button_states['SECOND']['BALANCE'])

    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text=
        "<b>YOUR ACCOUNT BALANCES:</b>\n\nDEMO BALANCE:  <b>{}</b>\n\nNGN BALANCE:  <b>{}</b>\n\n"
        .format(demo_bal, ngn_bal),
        reply_markup=reply_markup,
        parse_mode=ParseMode.HTML)

    return SECOND
def binary(update, context):
    user = update.message.from_user
    bot = context.bot
    bot.send_chat_action(chat_id=user.id, action=ChatAction.TYPING)
    binary_db = BinaryDB()
    res = binary_db.create_user(user.id, user.first_name, user.last_name)
    context.user_data['default_acct'] = res['active_acct']

    reply_markup = InlineKeyboardMarkup(button_states['BINARY_MAIN'])

    update.message.reply_text(
        "Welcome {} to our simple binary options trading platform\n\nYou will currently be using a DEMO account with 10000 credits to trade until you make a deposit, go to the deposit menu to see how you can deposit.\n\n"
        .format(user.first_name),
        reply_markup=reply_markup)

    return BINARY_MAIN
def deposit_amount(update, context):
    # query = update.callback_query
    bot = context.bot
    chat_id = update.message.chat_id
    bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)
    text = update.message.text
    print(text)

    binary_db = BinaryDB()
    res = binary_db.deposit(chat_id, text)
    print(res)

    reply_markup = InlineKeyboardMarkup(button_states['THIRD']['SHOW_ADDRESS'])

    bot.send_message(
        chat_id=chat_id,
        # message_id=query.message.message_id,
        text=
        "Follow the link to deposit your stipulated amount https://paystack.com/pay/oreo-binary",
        reply_markup=reply_markup)

    return LINK
def activate_demo(update, context):
    query = update.callback_query
    chat_id = query.message.chat_id
    bot = context.bot
    bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)

    # database functions
    binary_db = BinaryDB()
    res = binary_db.switch_acount(query.message.chat_id, 'demo')
    print(res)
    context.user_data['default_acct'] = res['active_acct']

    reply_markup = InlineKeyboardMarkup(
        button_states['THIRD']['ACTIVATE_DEMO'])

    if context.user_data['demo_bal'] <= 100:
        res = binary_db.db.users.find_one_and_update(
            {"tgram_uid"}, {'$set': {
                "account.demo_balance": 10000
            }})
        print(res)
        bot.edit_message_text(
            chat_id=query.message.chat_id,
            message_id=query.message.message_id,
            text=
            "Your DEMO account is currently active and REFRESHED, and set to default trade account",
            reply_markup=reply_markup)

        return THIRD

    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text=
        "Your DEMO account is currently active and set to default trade account",
        reply_markup=reply_markup)
    return THIRD
def trade_time(update, context):
    query = update.callback_query
    chat_id = query.message.chat_id
    bot = context.bot
    bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)

    binary_db = BinaryDB()
    res = binary_db.db.users.find_one({"tgram_uid": chat_id})
    total_bal = res['account']['demo_balance'] if res[
        'active_acct'] == 'demo' else res['account']['ngn_balance']
    context.user_data['total_bal'] = total_bal

    reply_markup = InlineKeyboardMarkup(
        button_states['SECOND']['TIME_SETTINGS'])

    bot.edit_message_text(chat_id=query.message.chat_id,
                          message_id=query.message.message_id,
                          text="PICK A DURATION FOR THE TRADE",
                          reply_markup=reply_markup)

    return SECOND
def init_trade(update, context):
    query = update.callback_query
    bot = context.bot
    chat = update.effective_chat
    asset = context.user_data['asset']
    currency = context.user_data['currency']
    trade_time = context.user_data['trade_time']
    trade_amount = context.user_data['trade_amount']
    total_bal = context.user_data['total_bal']
    default_acct = context.user_data['default_acct']

    reply_markup = InlineKeyboardMarkup(button_states['FOURTH']['INIT_TRADE'])

    if query.data == 'call':
        t = Trade(60 * trade_time, trade_amount, total_bal, 'CALL')
        bet_quote = t.get_price(currency)
        first = "SPOT QUOTE: <b>$" + str(round(
            bet_quote, 4)) + "</b>\nTRADE TYPE: <b>" + t.trade_type + "</b>"
        result = None

        bot.edit_message_text(
            chat_id=query.message.chat_id,
            message_id=query.message.message_id,
            text=first,
            # reply_markup=reply_markup,
            parse_mode=ParseMode.HTML)

        last_quote = bet_quote

        for i in range(0, t.duration, 5):
            current_quote = t.get_price(currency)
            first = "SPOT QUOTE: <b>$" + str(round(
                bet_quote, 4)) + "</b>\nCURRENT QUOTE: <b>$" + str(
                    round(current_quote,
                          4)) + "</b>\nTRADE TYPE: <b>" + t.trade_type + "</b>"
            if (i >= t.duration):
                break
            result = t.call(bet_quote, current_quote, 80)

            if current_quote != last_quote:
                bot.send_message(
                    chat_id=query.message.chat_id,
                    message_id=query.message.message_id,
                    text=first + "\n\n" + result['state'],
                    # reply_markup=reply_markup,
                    parse_mode=ParseMode.HTML)
                last_quote = current_quote

            time.sleep(5)
            i += 5

        bot.send_message(chat_id=query.message.chat_id,
                         message_id=query.message.message_id,
                         text="<b>TRADE IS DONE!!!</b>" + "\n\n" +
                         result['state'],
                         reply_markup=reply_markup,
                         parse_mode=ParseMode.HTML)
        binary_db = BinaryDB()
        res = binary_db.db.users.update_one(
            {"tgram_uid": query.message.chat_id},
            {'$set': {
                "account.demo_balance": result['new_balance']
            }}) if default_acct == 'demo' else binary_db.db.users.update_one(
                {"tgram_uid": query.message.chat_id},
                {'$set': {
                    "account.ngn_balance": result['new_balance']
                }})
        print(res)

    else:
        t = Trade(60 * trade_time, trade_amount, total_bal, 'PUT')
        bet_quote = t.get_price(currency)
        first = "SPOT QUOTE: <b>$" + str(round(
            bet_quote, 4)) + "</b>\nTRADE TYPE: <b>" + t.trade_type + "</b>"
        result = None

        bot.edit_message_text(
            chat_id=query.message.chat_id,
            message_id=query.message.message_id,
            text=first,
            # reply_markup=reply_markup,
            parse_mode=ParseMode.HTML)
        last_quote = bet_quote

        for i in range(0, t.duration, 5):
            current_quote = t.get_price(currency)
            first = "SPOT QUOTE: <b>$" + str(round(
                bet_quote, 4)) + "</b>\nCURRENT QUOTE: <b>$" + str(
                    round(current_quote,
                          4)) + "</b>\nTRADE TYPE: <b>" + t.trade_type + "</b>"
            if (i >= t.duration):
                break
            result = t.put(bet_quote, current_quote, 80)

            if current_quote != last_quote:
                bot.send_message(
                    chat_id=query.message.chat_id,
                    message_id=query.message.message_id,
                    text=first + "\n\n" + result['state'],
                    # reply_markup=reply_markup,
                    parse_mode=ParseMode.HTML)
                last_quote = current_quote

            time.sleep(5)
            i += 5

        bot.send_message(chat_id=query.message.chat_id,
                         message_id=query.message.message_id,
                         text="<b>TRADE IS DONE!!!</b>" + "\n\n" +
                         result['state'],
                         reply_markup=reply_markup,
                         parse_mode=ParseMode.HTML)
        binary_db = BinaryDB()
        res = binary_db.db.users.update_one(
            {"tgram_uid": query.message.chat_id},
            {'$set': {
                "account.demo_balance": result['new_balance']
            }}) if default_acct == 'demo' else binary_db.db.users.update_one(
                {"tgram_uid": query.message.chat_id},
                {'$set': {
                    "account.ngn_balance": result['new_balance']
                }})
        print(res)

    return FOURTH