/*
 * The MIT License
 *
 * Copyright (c) 2016 Marcelo "Ataxexe" Guimarães <ataxexe@devnull.tools>
 *
 * Permission  is hereby granted, free of charge, to any person obtaining
 * a  copy  of  this  software  and  associated  documentation files (the
 * "Software"),  to  deal  in the Software without restriction, including
 * without  limitation  the  rights to use, copy, modify, merge, publish,
 * distribute,  sublicense,  and/or  sell  copies of the Software, and to
 * permit  persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * The  above  copyright  notice  and  this  permission  notice  shall be
 * included  in  all  copies  or  substantial  portions  of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY OF ANY KIND,
 * EXPRESS  OR  IMPLIED,  INCLUDING  BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN  NO  EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM,  DAMAGES  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT  OR  OTHERWISE,  ARISING  FROM,  OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE   OR   THE   USE   OR   OTHER   DEALINGS  IN  THE  SOFTWARE.
 */

package tools.devnull.boteco.plugins.karma;

import tools.devnull.boteco.ContentFormatter;
import tools.devnull.boteco.ServiceLocator;
import tools.devnull.boteco.message.IncomeMessage;
import tools.devnull.boteco.message.MessageProcessor;

import java.util.Properties;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KarmaMessageProcessor implements MessageProcessor, ServiceLocator {

  private final Pattern pattern =
      Pattern.compile("(?<term>\\S+)(?<operation>\\+\\+|\\-\\-)(\\s|$)");

  private final KarmaRepository repository;
  private final Properties properties;

  public KarmaMessageProcessor(KarmaRepository repository, Properties properties) {
    this.repository = repository;
    this.properties = properties;
  }

  @Override
  public String id() {
    return "karma";
  }

  @Override
  public boolean canProcess(IncomeMessage message) {
    return message.isGroup() && pattern.matcher(message.content()).find();
  }

  @Override
  public void process(IncomeMessage message) {
    ContentFormatter f = message.channel().formatter();
    Matcher matcher = pattern.matcher(message.content());
    while (matcher.find()) {
      String term = matcher.group("term");
      String operation = matcher.group("operation");
      int value = update(term, operation, f);
      reply(message, term, value, f);
    }
  }

  private int update(String term, String operation, ContentFormatter f) {
    int updatedValue = 0;
    switch (operation) {
      case "++":
        updatedValue = operateKarma(term, Karma::raise);
        break;
      case "--":
        updatedValue = operateKarma(term, Karma::lower);
        break;
    }
    return updatedValue;
  }

  private void reply(IncomeMessage message, String term, int value, ContentFormatter f) {
    String content = properties.getProperty(term, "%t has now %n %u of %k");
    content = content.replace("%t", f.accent(properties.getProperty(term + ".term", term)));
    content = content.replace("%n", f.number(value));
    content = content.replace("%u", properties.getProperty(term + ".unit", "points"));
    content = content.replace("%k", properties.getProperty(term + ".karma", "karma"));
    message.reply(content);
  }

  private int operateKarma(String term, Consumer<Karma> operation) {
    term = term.toLowerCase();
    Karma karma = repository.find(term);
    operation.accept(karma);
    repository.update(karma);
    return karma.value();
  }

}

@@ -3067,24 +3067,61 @@
 matter f) %7B%0A
+    String key = term.toLowerCase();%0A
     String c
@@ -3152,20 +3152,19 @@
 roperty(
-term
+key
 , %22%25t ha
@@ -3248,28 +3248,27 @@
 getProperty(
-term
+key
  + %22.term%22, 
@@ -3385,28 +3385,27 @@
 getProperty(
-term
+key
  + %22.unit%22, 
@@ -3475,20 +3475,19 @@
 roperty(
-term
+key
  + %22.kar

fix the key to get custom karma properties