package com.red.circle.order.app.service;

import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.*;
import com.pengrad.telegrambot.request.AnswerPreCheckoutQuery;
import com.red.circle.order.app.command.pay.web.strategy.TelegramPurchaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * Description:
 *
 * @author 金泽强
 * @date 2024/12/10 16:43
 */
//@Service
@RequiredArgsConstructor
public class TelegramService {

    private final TelegramBot telegramBot;
    private final TelegramPurchaseService telegramPurchaseService;

    public void dealWithUpdate(Update update) {
        CallbackQuery callbackQuery = update.callbackQuery();
        Message message = update.message();
        PreCheckoutQuery preCheckoutQuery = update.preCheckoutQuery();
        if (preCheckoutQuery != null) {
            telegramPurchaseService.pay(preCheckoutQuery);
            AnswerPreCheckoutQuery answerPreCheckoutQuery = new AnswerPreCheckoutQuery(preCheckoutQuery.id());
            telegramBot.execute(answerPreCheckoutQuery);
            return;
        } else if (message != null) {
            SuccessfulPayment successfulPayment = message.successfulPayment();
            if (successfulPayment != null) {
                telegramPurchaseService.paySuccess(successfulPayment);
            }
        }
    }

}
