import telegram
import jinja2

from leonard import Leonard
from modules import weather

INITIAL_WEATHER_OFFER = jinja2.Template("""You shouldn't write me to get help – you can subscribe to some notification messages.

⛅ Maybe weather? Morning forecasts or "Rain in next hour" reports?
""")
WEATHER_SETUP_RESULT = jinja2.Template(
    "👌 Not a problem, {% if result %}I subscribed you. It's ready " +
    "and will notificate you next time.{% else %}you can always change it later.{% endif %}"
)


def register(bot):
    bot.handlers['subscribes-setup'] = subscriptions_setup
    bot.handlers['subscribes-setup-result'] = subscriptions_setup_result
    bot.handlers['subscriptions-show'] = show_subscriptions
    bot.handlers['subscriptions-choose'] = choose_subscriptions
    bot.handlers['subscription-set'] = set_subscription


def subscriptions_setup(message, bot):
    bot.user_set(message.u_id, 'next_handler', 'subscribes-setup-result')
    bot.telegram.send_message(
        chat_id=message.u_id,
        text=INITIAL_WEATHER_OFFER.render(),
        reply_markup=telegram.ReplyKeyboardMarkup(
            [['Yeah, send me morning forecasts 🌄'],
             ['Notificate me about upcoming rain ☔️'],
             ['No, thanks, I will setup it later 🚫']]
        )
    )


def subscriptions_setup_result(message, bot):
    base_key = 'notifications:{}:{}'.format(weather.NAME, '{}')
    if '🌄' in message.text:
        key = base_key.format('morning')
    elif '☔️' in message.text:
        key = base_key.format('rain')
    else:
        key = None
    bot.telegram.send_message(
        chat_id=message.u_id,
        text=WEATHER_SETUP_RESULT.render(result=key),
        reply_markup=telegram.ReplyKeyboardHide()
    )
    if key:
        bot.user_set(message.u_id, key, 1)
        bot.redis.expire('user:{}:{}'.format(message.u_id, key), 24 * 60 * 60)
    bot.call_handler(message, 'welcome-location-setup')


def show_subscriptions(message, bot: Leonard):
    bot.user_set(message.u_id, 'next_handler', 'subscriptions-choose')
    reply_markup = telegram.ReplyKeyboardMarkup(
        [[telegram.KeyboardButton(subscription)] for subscription in bot.available_subscriptions.keys()]
    )
    reply_markup.keyboard.append([telegram.KeyboardButton(bot.MENU_BUTTON)])
    bot.telegram.send_message(message.u_id, 'There are {} subscribe sources'.format(len(bot.available_subscriptions)),
                              reply_markup=reply_markup, parse_mode=telegram.ParseMode.MARKDOWN)
    pass


def choose_subscriptions(message, bot: Leonard):
    if message.text in bot.available_subscriptions:
        bot.user_set(message.u_id, 'next_handler', 'subscription-set')
        chosen_subscriptions = bot.available_subscriptions[message.text]
        reply_markup = telegram.ReplyKeyboardMarkup(
            [[telegram.KeyboardButton('{} - {}'.format(message.text, subscription))] for subscription in
             chosen_subscriptions]
        )
        reply_markup.keyboard.append([telegram.KeyboardButton(bot.MENU_BUTTON)])
        bot.telegram.send_message(message.u_id, 'There are {} subscription types'.format(len(chosen_subscriptions)),
                                  reply_markup=reply_markup, parse_mode=telegram.ParseMode.MARKDOWN)
    else:
        bot.call_handler(message, 'main-menu')


def set_subscription(message, bot: Leonard):
    if len(message.text.split(' - ')) <= 1:
        bot.call_handler(message, 'main-menu')
        return
    plugin, subscription = message.text.split(' - ')
    if plugin not in bot.available_subscriptions:
        bot.call_handler(message, 'main-menu')
        return

    if subscription not in bot.available_subscriptions[plugin]:
        bot.call_handler(message, 'main-menu')
        return

    subscription = bot.available_subscriptions[plugin][subscription]
    bot.user_set(message.u_id, 'notifications:{}:{}'.format(plugin, subscription), 1)
    bot.telegram.send_message(message.u_id, 'You have been successfully subscribed to "{}"'.format(message.text),
                              reply_markup=telegram.ReplyKeyboardHide(), parse_mode=telegram.ParseMode.MARKDOWN)
    bot.call_handler(message, 'main-menu')

@@ -705,72 +705,8 @@
 ons%0A
-    bot.handlers%5B'subscriptions-choose'%5D = choose_subscriptions%0A
     
@@ -1959,16 +1959,12 @@
 tion
-s-choo
+-
 se
+t
 ')%0A 
@@ -2049,804 +2049,126 @@
 ton(
-subscription)%5D for subscription in bot.available_subscriptions.keys()%5D%0A    )%0A    reply_markup.keyboard.append(%5Btelegram.KeyboardButton(bot.MENU_BUTTON)%5D)%0A    bot.telegram.send_message(message.u_id, 'There are %7B%7D subscribe sources'.format(len(bot.available_subscriptions)),%0A                              reply_markup=reply_markup, parse_mode=telegram.ParseMode.MARKDOWN)%0A    pass%0A%0A%0Adef choose_subscriptions(message, bot: Leonard):%0A    if message.text in bot.available_subscriptions:%0A        bot.user_set(message.u_id, 'next_handler', 'subscription-set')%0A        chosen_subscriptions = bot.available_subscriptions%5Bmessage.text%5D%0A        reply_markup = telegram.ReplyKeyboardMarkup(%0A            %5B%5Btelegram.KeyboardButton('%7B%7D - %7B%7D'.format(message.text, subscription))%5D for subscription in%0A            
+'%7B%7D - %7B%7D'.format(name, sub))%5D for name, chosen_subscription in%0A         bot.available_subscriptions.items() for sub in
  cho
@@ -2187,29 +2187,15 @@
 tion
-s%5D%0A        )%0A  
+%5D)%0A
     
-  
 repl
@@ -2263,20 +2263,16 @@
 TTON)%5D)%0A
-    
     bot.
@@ -2285,32 +2285,41 @@
 am.send_message(
+%0A        
 message.u_id, 'T
@@ -2315,16 +2315,24 @@
 ge.u_id,
+%0A       
  'There 
@@ -2354,77 +2354,67 @@
 tion
- types'.format(len(chosen_subscriptions)),%0A                          
+s available'.format(sum(map(len, reply_markup.keyboard))),%0A
     
@@ -2443,16 +2443,24 @@
 _markup,
+%0A       
  parse_m
@@ -2494,65 +2494,13 @@
 DOWN
-)
 %0A    
-else:%0A        bot.call_handler(message, 'main-menu'
 )%0A%0A%0A

Remove subscription buttons nesting