/*
 * 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 com.google.gson.Gson;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
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 MongoCollection<Document> karmas;
  private final Gson gson;
  private final Properties properties;

  public KarmaMessageProcessor(MongoDatabase database, Properties properties) {
    this.properties = properties;
    this.karmas = database.getCollection("karmas");
    this.gson = new Gson();
  }

  @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");
      String value = update(term, operation, f);
      reply(message, term, value, f);
    }
  }

  private String 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 f.number(updatedValue);
  }

  private void reply(IncomeMessage message, String term, String value, ContentFormatter f) {
    String karma = properties.getProperty(term + ".karma", "karma");
    term = properties.getProperty(term + ".term", term);
    message.reply("%s has now %s points of %s", f.accent(term), value, karma);
  }

  private Karma findKarma(String term) {
    BasicDBObject query = new BasicDBObject();
    query.put("_id", term);
    Document result = karmas.find(query).first();
    if (result == null) {
      Karma newKarma = new Karma(term);
      karmas.insertOne(Document.parse(gson.toJson(newKarma)));
      return newKarma;
    }
    return gson.fromJson(result.toJson(), Karma.class);
  }

  private void updateKarma(Karma karma) {
    BasicDBObject query = new BasicDBObject();
    query.put("_id", karma.name());
    if (karma.value() != 0) {
      karmas.updateOne(query, new Document("$set", new Document().append("value", karma.value())));
    } else {
      // remove karma if it's value is zero (something like a garbage collector)
      karmas.findOneAndDelete(query);
    }

  }

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

}

@@ -2801,22 +2801,19 @@
 ;%0A      
-Str
 in
-g
+t
  value =
@@ -2900,22 +2900,19 @@
 private 
-Str
 in
-g
+t
  update(
@@ -3214,17 +3214,8 @@
 urn 
-f.number(
 upda
@@ -3222,17 +3222,16 @@
 tedValue
-)
 ;%0A  %7D%0A%0A 
@@ -3286,22 +3286,19 @@
 g term, 
-Str
 in
-g
+t
  value, 
@@ -3330,21 +3330,23 @@
  String 
-karma
+content
  = prope
@@ -3371,42 +3371,82 @@
 term
- + %22.karma%22, %22karma%22);%0A    term = 
+, %22%25t has now %25n %25u of %25k%22);%0A    content = content.replace(%22%25t%22, f.accent(
 prop
@@ -3489,86 +3489,265 @@
 erm)
+))
 ;%0A    
-message.reply(%22%25s has now %25s points of %25s%22, f.accent(term), value
+content = content.replace(%22%25n%22, f.number(value));%0A    content = content.replace(%22%25u%22, properties.getProperty(term + %22.unit%22, %22points%22));%0A    content = content.replace(%22%25k%22, properties.getProperty(term + %22.karma%22
 , 
+%22
 karma
+%22));%0A    message.reply(content
 );%0A 

add options to customize even more the karma messages