package ru.kayashov.bar.bot.domain.command.api;

import org.springframework.transaction.annotation.Transactional;
import ru.kayashov.bar.bot.domain.methods.AbstractMethod;
import ru.kayashov.bar.bot.domain.model.AbstractCallbackQuery;
import ru.kayashov.bar.bot.domain.model.AbstractMessage;
import ru.kayashov.bar.bot.domain.model.AbstractUpdate;

import java.util.List;

public interface Command {

    @Transactional
    List<? extends AbstractMethod> execute (AbstractUpdate abstractUpdate);

    default Long getChatId(AbstractUpdate abstractUpdate) {
        AbstractMessage message = abstractUpdate.getMessage();
        AbstractCallbackQuery query = abstractUpdate.getCallbackQuery();

        if(message != null) {
            return message.getChat().getId();
        }

        if (query != null) {
            return query.getMessage().getChatId();
        }

        throw new RuntimeException("Не возможно найти id чата");
    }

    default Integer getMessageId(AbstractUpdate abstractUpdate) {
        AbstractMessage message = abstractUpdate.getMessage();
        AbstractCallbackQuery query = abstractUpdate.getCallbackQuery();

        if(message != null) {
            return message.getMessageId();
        }

        if (query != null) {
            return query.getMessage().getMessageId();
        }

        throw new RuntimeException("Не возможно найти id сообщения");
    }
}
