package org.jebot.handler.impl.calculator;

import com.pengrad.telegrambot.model.request.ReplyParameters;
import com.pengrad.telegrambot.request.SendMessage;
import org.jebot.handler.AbstractHandler;
import org.jebot.handler.dto.BotMessage;

import java.util.regex.Pattern;

public class CalculatorHandler extends AbstractHandler {

    private static final Pattern REGEX_ADD = Pattern.compile("^-?\\d+(\\.\\d+)?\\s*\\+\\s*-?\\d+(\\.\\d+)?$");
    private static final Pattern REGEX_SUB = Pattern.compile("^-?\\d+(\\.\\d+)?\\s*-\\s*-?\\d+(\\.\\d+)?$");
    private static final Pattern REGEX_MUL = Pattern.compile("^-?\\d+(\\.\\d+)?\\s*\\*\\s*-?\\d+(\\.\\d+)?$");
    private static final Pattern REGEX_DIV = Pattern.compile("^-?\\d+(\\.\\d+)?\\s*/\\s*-?\\d+(\\.\\d+)?$");


    @Override
    public boolean msgHandler(BotMessage message) {
        if (message.messageTextIsEmpty()) {
            return false;
        }
        String text = message.getMessageText();
        if (REGEX_ADD.matcher(text).matches()) {
            String[] parts = text.split("\\+");
            if (parts.length != 2) {
                message.getTelegramBot().execute(new SendMessage(
                        message.getMessage().chat().id(), "输入格式错误").
                        replyParameters(new ReplyParameters(message.getMessage().messageId()))
                );
                return false;
            }
            double result = Double.parseDouble(parts[0].trim()) + Double.parseDouble(parts[1].trim());
            message.getTelegramBot().execute(
                    new SendMessage(message.getMessage().chat().id(), String.valueOf(result)).
                            replyParameters(new ReplyParameters(message.getMessage().messageId()))
            );
            return true;
        } else if (REGEX_SUB.matcher(text).matches()) {
            String[] parts = text.split("-");
            if (parts.length != 2) {
                message.getTelegramBot().execute(
                        new SendMessage(message.getMessage().chat().id(), "输入格式错误").
                                replyParameters(new ReplyParameters(message.getMessage().messageId()))
                );
                return false;
            }
            double result = Double.parseDouble(parts[0].trim()) - Double.parseDouble(parts[1].trim());
            message.getTelegramBot().execute(
                    new SendMessage(message.getMessage().chat().id(), String.valueOf(result)).
                            replyParameters(new ReplyParameters(message.getMessage().messageId()))
            );
            return true;
        } else if (REGEX_MUL.matcher(text).matches()) {
            String[] parts = text.split("\\*");
            if (parts.length != 2) {
                message.getTelegramBot().execute(
                        new SendMessage(message.getMessage().chat().id(), "输入格式错误").
                                replyParameters(new ReplyParameters(message.getMessage().messageId()))
                );
                return false;
            }
            double result = Double.parseDouble(parts[0].trim()) * Double.parseDouble(parts[1].trim());
            message.getTelegramBot().execute(
                    new SendMessage(message.getMessage().chat().id(), String.valueOf(result)).
                            replyParameters(new ReplyParameters(message.getMessage().messageId()))
            );
            return true;
        } else if (REGEX_DIV.matcher(text).matches()) {
            String[] parts = text.split("/");
            if (parts.length != 2) {
                message.getTelegramBot().execute(
                        new SendMessage(message.getMessage().chat().id(), "输入格式错误").
                                replyParameters(new ReplyParameters(message.getMessage().messageId()))
                );
                return false;
            }
            double result = Double.parseDouble(parts[0].trim()) / Double.parseDouble(parts[1].trim());
            message.getTelegramBot().execute(
                    new SendMessage(message.getMessage().chat().id(), String.valueOf(result)).
                            replyParameters(new ReplyParameters(message.getMessage().messageId()))
            );
            return true;
        }

        return false;
    }
}
