const _ = require('lodash');
const api = require('./api');
const session = require('./session');
const responders = require('./responders');

module.exports = {

  // ## /kirjaudu
  // Link telegram id with piikki username
  login: async (ctx) => {
    // Check if there is already a link
    if (ctx.session.username) {
      ctx.reply(`Olet jo kirjautunut tunnuksella ${ctx.session.username}`);
      return;
    }

    // Start the login process
    await session.updateSession(ctx.from.id, session.constants.states.loginAskUsername());
    ctx.reply('Syötä Piikki-tunnuksesi');
  },

  // ## /saldo
  // Lists saldos in each group
  saldo: async (ctx) => {
    const user = await api.getUser(ctx.session.username);

    if (_.isEmpty(user.saldos)) {
      ctx.reply('Et ole liittynyt vielä mihinkään ryhmään.');
      return;
    }

    const saldos = _.map(user.saldos, (saldo, group) => `*${group}*: ${saldo}`);
    ctx.replyWithMarkdown(`Ryhmiesi saldot:\n${_.join(saldos, '\n')}`);
  },

  // ## /lisaa [amount]
  // Add credit by certain amount, default is 1
  add: async (ctx) => {
    const amount = (_.isEmpty(ctx.state.command.args)) ? 1 :
      _.chain(ctx.state.command.splitArgs)
      .first()
      .toLength()
      .value();

    if (amount > 0) {
      if (ctx.session.defaultGroup) {
        const res = await api.makeTransaction(ctx.session.defaultGroup, ctx.session.username, amount);
        if (res) {
          ctx.reply(`Saldosi ryhmässä ${ctx.session.defaultGroup}: ${_.first(res).saldo}`);
        }
      }
    } else {
      ctx.reply(`"${_.first(ctx.state.command.splitArgs)}" ei ollut positiivinen kokonaisluku`);
    }
  },

  // ## /viiva [amount]
  // Takes credit by certain amount
  subtract: async (ctx) => {
    const amount = (_.isEmpty(ctx.state.command.args)) ? 1 :
      _.chain(ctx.state.command.splitArgs)
      .first()
      .toLength()
      .value();

    if (amount > 0) {
      if (ctx.session.defaultGroup) {
        const res = await api.makeTransaction(ctx.session.defaultGroup, ctx.session.username, -amount);
        if (res) {
          ctx.reply(`Saldosi ryhmässä ${ctx.session.defaultGroup}: ${_.first(res).saldo}`);
        }
      }
    } else {
      ctx.reply(`"${_.first(ctx.state.command.splitArgs)}" ei ollut positiivinen kokonaisluku`);
    }
  },

  // Process messages without a command
  message: (ctx) => {
    // For now we don't process non-message events,
    // such as edit events
    if (ctx.updateType !== 'message') return;

    // If message is empty or command, ignore it
    if (_.isEmpty(ctx.message.text) || (ctx.message.text[0] === '/')) return;

    // There is no events to react
    if (!ctx.session.state) return;

    // Process event in responder specified by the state type
    responders(ctx);
  },
};

@@ -891,17 +891,19 @@
 =%3E %60
-*
+%3Cb%3E
 $%7Bgroup%7D
 *: $
@@ -902,9 +902,12 @@
 oup%7D
-*
+%3C/b%3E
 : $%7B
@@ -937,33 +937,21 @@
 With
-Markdown(%60Ryhmiesi s
+HTML(%60S
 aldo
-t
+si
 :%5Cn$

Make response text more clear when executing from group