package com.red.circle.other.app.manager;

import com.alibaba.fastjson.JSON;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.external.inner.endpoint.message.ImGroupClient;
import com.red.circle.external.inner.endpoint.message.OfficialNoticeClient;
import com.red.circle.external.inner.model.cmd.message.BroadcastGroupMsgBodyCmd;
import com.red.circle.external.inner.model.cmd.message.CustomGroupMsgBodyCmd;
import com.red.circle.external.inner.model.cmd.message.notice.official.NoticeExtTemplateTypeCmd;
import com.red.circle.external.inner.model.enums.message.GroupMessageTypeEnum;
import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum;
import com.red.circle.other.app.dto.clientobject.RocketStatusCO;
import com.red.circle.other.app.util.OfficialNoticeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 火箭IM消息推送管理器
 *
 * @author system
 * @date 2025-01-15
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RocketImPushManager {

     private final ImGroupClient imGroupClient;

    /**
     * 推送能量更新消息
     */
    public void pushEnergyUpdate(String roomAccount, RocketStatusCO status) {
        try {
            imGroupClient.sendCustomMessage(roomAccount,
                    CustomGroupMsgBodyCmd.builder()
                            .type(GroupMessageTypeEnum.ROCKET_ENERGY_UPDATE)
                            .data(status)
                            .build());

            log.info("推送火箭能量更新消息成功: roomId={}, level={}, energy={}/{}",
                    roomAccount, status.getLevel(), status.getCurrentEnergy(), status.getMaxEnergy());

        } catch (Exception e) {
            log.error("推送火箭能量更新消息失败: roomId={}", roomAccount, e);
        }
    }

    /**
     * 推送火箭发射消息
     */
    public void pushRocketLaunch(RocketStatusCO status) {
        try {
            imGroupClient.sendMessageBroadcast(
                    BroadcastGroupMsgBodyCmd.builder()
                            .toPlatform(SysOriginPlatformEnum.LIKEI)
//                            .type(GroupMessageTypeEnum.ROCKET_ENERGY_LAUNCH)
                            .data(status)
                            .build()
            );

            log.info("推送火箭发射消息成功: roomId={}, level={}", status.getRoomId(), status.getLevel());

        } catch (Exception e) {
            log.error("推送火箭发射消息失败: roomId={}", status.getRoomId(), e);
        }
    }

    /**
     * 推送奖励领取成功消息
     */
    public void pushRewardClaimed(Long roomId, Long userId, String userName, Long rewardAmount) {
        try {
            // 构建消息
            ImMessage message = buildRewardClaimedMessage(userId, userName, rewardAmount);

            // TODO: 调用实际的IM推送服务
            // imPushService.pushToRoom(roomId, message);

            log.info("推送奖励领取消息成功: roomId={}, userId={}, amount={}", roomId, userId, rewardAmount);

        } catch (Exception e) {
            log.error("推送奖励领取消息失败: roomId={}, userId={}", roomId, userId, e);
        }
    }

    /**
     * 推送火箭重置消息
     */
    public void pushRocketReset(Long roomId) {
        try {
            // 构建消息
            ImMessage message = buildResetMessage();

            // TODO: 调用实际的IM推送服务
            // imPushService.pushToRoom(roomId, message);

            log.info("推送火箭重置消息成功: roomId={}", roomId);

        } catch (Exception e) {
            log.error("推送火箭重置消息失败: roomId={}", roomId, e);
        }
    }

    /**
     * 构建能量更新消息
     */
    private ImMessage buildEnergyUpdateMessage(RocketStatusCO status) {
        ImMessage message = new ImMessage();
        message.setType("ROCKET_ENERGY_UPDATE");
        message.setData(JSON.toJSONString(status));
        return message;
    }

    /**
     * 构建发射消息
     */
    private ImMessage buildLaunchMessage(RocketStatusCO status) {
        ImMessage message = new ImMessage();
        message.setType("ROCKET_LAUNCH");
        message.setData(JSON.toJSONString(status));
        return message;
    }

    /**
     * 构建奖励领取消息
     */
    private ImMessage buildRewardClaimedMessage(Long userId, String userName, Long rewardAmount) {
        RewardClaimedData data = new RewardClaimedData();
        data.setUserId(userId);
        data.setUserName(userName);
        data.setRewardAmount(rewardAmount);

        ImMessage message = new ImMessage();
        message.setType("ROCKET_REWARD_CLAIMED");
        message.setData(JSON.toJSONString(data));
        return message;
    }

    /**
     * 构建重置消息
     */
    private ImMessage buildResetMessage() {
        ImMessage message = new ImMessage();
        message.setType("ROCKET_RESET");
        message.setData("{}");
        return message;
    }

    /**
     * IM消息对象
     */
    private static class ImMessage {
        private String type;
        private String data;

        public void setType(String type) {
            this.type = type;
        }

        public void setData(String data) {
            this.data = data;
        }
    }

    /**
     * 奖励领取数据
     */
    private static class RewardClaimedData {
        private Long userId;
        private String userName;
        private Long rewardAmount;

        public void setUserId(Long userId) {
            this.userId = userId;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public void setRewardAmount(Long rewardAmount) {
            this.rewardAmount = rewardAmount;
        }
    }
}
