package com.github.masahitojp.botan;

import com.github.masahitojp.botan.brain.BotanBrain;
import com.github.masahitojp.botan.exception.BotanException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.muc.MultiUserChat;

import java.util.Optional;
import java.util.regex.Matcher;

public final class BotanMessage {
    private Chat chat;
    private MultiUserChat muc;
    private final Message message;
    private final Botan botan;
    private final Matcher matcher;

    public BotanMessage(final Botan botan, final Chat chat, final Message message, final Matcher matcher) {
        this.botan = botan;
        this.chat = chat;
        this.message = message;
        this.matcher = matcher;
    }

    public BotanMessage(final Botan botan, final MultiUserChat muc, final Message message, final Matcher matcher) {
        this.botan = botan;
        this.muc = muc;
        this.message = message;
        this.matcher = matcher;
    }

    public final String getRobotName() {
        return this.botan.getName();
    }

    public final void reply(final String body) throws BotanException {
        try {
            // Todo : コンストラクタでnullが割り当てられないようにする
            if (chat != null) {
                this.chat.sendMessage(body);
            } else if (muc != null) {
                // 自分からのメッセージは受け取らないように変更
                final Optional<String> messageFromOpt = Optional.of(message.getFrom());
                final String[] s = messageFromOpt.orElse("").split("/");
                if (s.length == 2 && !s[1].equals(botan.getName())) {
                    this.muc.sendMessage(body);
                }
            }

        } catch (SmackException.NotConnectedException e) {
            throw new BotanException(e);
        }
    }

    public final Matcher getMatcher() {
        return this.matcher;
    }

    public final String getBody() {
        return message.getBody();
    }

    public final String getFrom() {
        final String from = message.getFrom();
        final String[] elems = from.split("/");
        if (elems.length > 0) {
            return elems[elems.length -1];
        } else {
            final String[] strs = from.split("@");
            return strs[0];
        }
    }

    public final BotanBrain getBrain() {
        return this.botan.brain;
    }
}

@@ -427,62 +427,8 @@
 e %7B%0A
-    private Chat chat;%0A    private MultiUserChat muc;%0A
     
@@ -523,16 +523,70 @@
 matcher;
+%0A    private Chat chat;%0A    private MultiUserChat muc;
 %0A%0A    pu
@@ -1043,32 +1043,64 @@
 matcher;%0A    %7D%0A%0A
+    @SuppressWarnings(%22unused%22)%0A
     public final
@@ -1160,32 +1160,64 @@
 tName();%0A    %7D%0A%0A
+    @SuppressWarnings(%22unused%22)%0A
     public final
@@ -1283,62 +1283,101 @@
     
-try %7B%0A            // Todo : %E3%82%B3%E3%83%B3%E3%82%B9%E3%83%88%E3%83%A9%E3%82%AF%E3%82%BF%E3%81%A7null%E3%81%8C%E5%89%B2%E3%82%8A%E5%BD%93%E3%81%A6%E3%82%89%E3%82%8C%E3%81%AA%E3%81%84%E3%82%88%E3%81%86%E3%81%AB%E3%81%99%E3%82%8B
+if (body == null) %7B%0A            throw new IllegalArgumentException();%0A        %7D%0A        try %7B
 %0A   
@@ -1951,32 +1951,64 @@
        %7D%0A    %7D%0A%0A
+    @SuppressWarnings(%22unused%22)%0A
     public final
@@ -2059,32 +2059,64 @@
 matcher;%0A    %7D%0A%0A
+    @SuppressWarnings(%22unused%22)%0A
     public final
@@ -2168,32 +2168,64 @@
 tBody();%0A    %7D%0A%0A
+    @SuppressWarnings(%22unused%22)%0A
     public final
@@ -2366,17 +2366,17 @@
 ength %3E 
-0
+1
 ) %7B%0A    
@@ -2410,16 +2410,17 @@
 length -
+ 
 1%5D;%0A    
@@ -2520,32 +2520,64 @@
        %7D%0A    %7D%0A%0A
+    @SuppressWarnings(%22unused%22)%0A
     public final

change message#getFrom format