package org.jebot.handler.dto;

import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.Update;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.jebot.constant.Constant;
import org.jebot.models.jebot.BotGroup;
import org.jebot.models.jebot.BotUser;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BotMessage {
    private Update update;
    private BotUser botUser;
    private BotGroup botGroup;
    private Message message;
    private TelegramBot telegramBot;

    public BotMessage(TelegramBot telegramBot, Message message, Update update) {
        this.telegramBot = telegramBot;
        this.message = message;
        this.update = update;
    }

    //用户认证
    public boolean isUserAuthenticated() {
        return this.botUser != null;
    }

    //群组认证
    public boolean isGroupAuthenticated() {
        return this.botGroup != null;
    }

    //是商户群组
    public boolean isGroupMch() {
        if (!this.isGroupAuthenticated()) {
            return false;
        }
        return Constant.DATA_TYPE_MERCHANT.equalsIgnoreCase(this.botGroup.getDataType());
    }

    //是通道群组
    public boolean isGroupChannel() {
        if (!this.isGroupAuthenticated()) {
            return false;
        }
        return Constant.DATA_TYPE_CHANNEL.equalsIgnoreCase(this.botGroup.getDataType());
    }

    //包含文字和图片
    public boolean isContainsTextAndPhoto(){
        // 判断是否包含文字
        boolean hasText = this.message.caption() != null;
        // 判断是否包含图片
        boolean hasPhoto = this.message.photo() != null && this.message.photo().length > 0;
        return hasText && hasPhoto;
    }

    // 来自群组的消息
    public boolean isGroupMessage(){
        return update.message().chat().type().name().equals("group") || update.message().chat().type().name().equals("supergroup");
    }

    public String getMessageText() {
        return this.message.text();
    }

    public boolean messageTextIsEmpty() {
        return Strings.isEmpty(this.getMessageText());
    }


}
