
from YoConfig import YoConfig
import redis

from random import randint
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
from telegram import InlineKeyboardMarkup, InlineKeyboardButton
import telegram
import datetime

""" import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                     level=logging.INFO) """

""" def start(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!") """

r = redis.Redis(host=YoConfig['db']['choozmo_redis']['REDIS_HOST'], port=YoConfig['db']['choozmo_redis']['REDIS_PORT'], password=YoConfig['db']['choozmo_redis']['REDIS_PASSWORD'], db=1)

def start(update, bot):
    a, b = randint(1, 100), randint(1, 100)
    update.message.reply_text('{} + {} = ?'.format(a, b),
        reply_markup = InlineKeyboardMarkup([[
                InlineKeyboardButton(str(s), callback_data = '{} {} {}'.format(a, b, s)) for s in range(a + b - randint(1, 3), a + b + randint(1, 3))
            ]]))


def answer(update, bot):
    a, b, s = [int(x) for x in update.callback_query.data.split()]
    if a + b == s:
        update.callback_query.edit_message_text('你答對了！')
    else:
        update.callback_query.edit_message_text('你答錯囉！')

updater = Updater('1773608337:AAF0TVRKXQu4vB3ii7JkM5kuV2vsXkvX7ss', use_context=True)
j = updater.job_queue
dispatcher = updater.dispatcher

""" dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(answer)) """

def echo(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)

echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dispatcher.add_handler(echo_handler)

def caps(update, context):
    text_caps = ' '.join(context.args).upper()
    context.bot.send_message(chat_id=update.effective_chat.id, text=text_caps)

caps_handler = CommandHandler('caps', caps)
dispatcher.add_handler(caps_handler)


def callback_alarm(context):
    context.bot.send_message(chat_id=context.job.context, text='BEEP')

def callback_timer(update, context):
    context.bot.send_message(chat_id=update.message.chat_id,
                             text='Setting a timer for 1 minute!')

    context.job_queue.run_repeating(callback_alarm, 60, context=update.message.chat_id)

timer_handler = CommandHandler('timer', callback_timer)
dispatcher.add_handler(timer_handler)

def callback_minute(context: telegram.ext.CallbackContext):
    context.bot.send_message(chat_id='1053416326', 
                             text='Im online.')
    context.bot.send_message(chat_id='-1001177266889', 
                             text='Im online.')    
updater.job_queue.run_once(callback_minute, 3)

def hhh_space_monitor(context: telegram.ext.CallbackContext):
    context.bot.send_message(chat_id='-1001177266889', 
                             text='this is space monitor. space_hhhapi: \n' + str(r.get('space_hhhapi').decode('utf-8')))    
updater.job_queue.run_daily(hhh_space_monitor, datetime.time(1, 00, 00))
updater.job_queue.run_once(hhh_space_monitor, 3)

updater.start_polling()
updater.idle()
