package cn.axzo.msg.center.message.controller;

import cn.axzo.msg.center.api.request.CmsMsgQueryReq;
import cn.axzo.msg.center.api.response.MessageNewRes;
import cn.axzo.msg.center.inside.notices.config.PendingMessageBizConfig;
import cn.axzo.msg.center.message.service.GeneralMessageOldService;
import cn.axzo.msg.center.message.service.GeneralMessageService;
import cn.axzo.msg.center.service.dto.PersonDTO;
import cn.axzo.msg.center.service.general.client.GeneralMessageClient;
import cn.axzo.msg.center.service.general.request.GeneralMessageOldDataStatisticRequest;
import cn.axzo.msg.center.service.general.request.GeneralMessageSendRequest;
import cn.axzo.msg.center.service.general.request.OldMsgStatWithMultiIdentifiesRequest;
import cn.axzo.msg.center.service.general.response.GeneralMessageOldDataStatisticResponse;
import cn.azxo.framework.common.model.CommonResponse;
import cn.azxo.framework.common.model.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cold_blade
 * @date 2023/10/19
 * @version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class GeneralMessageController implements GeneralMessageClient {

    private static final Integer ZERO = 0;

    private final GeneralMessageService generalMessageService;
    private final GeneralMessageOldService generalMessageOldService;
    private final PendingMessageBizConfig cfg;

    @Override
    public CommonResponse<Void> batchSend(GeneralMessageSendRequest request) {
        generalMessageService.batchSendMessage(request);
        return CommonResponse.success();
    }

    @Override
    public CommonResponse<GeneralMessageOldDataStatisticResponse> statisticOldData(
            GeneralMessageOldDataStatisticRequest request) {
        return CommonResponse.success(generalMessageService.statisticOldData(request));
    }

    @Override
    public CommonResponse<Integer> countOldMsgUnreadWithIdentities(
            OldMsgStatWithMultiIdentifiesRequest request) {
        if (cfg.isOldMsgOffline()) {
            return CommonResponse.success(ZERO);
        }
        return CommonResponse.success(generalMessageOldService
                .countUnreadWithIdentities(request.getPersonId(), request.getIdentities()));
    }

    @Override
    public CommonResponse<Page<MessageNewRes>> pageQueryOldMessage(CmsMsgQueryReq request) {
        if (cfg.isOldMsgOffline()) {
            return CommonResponse.success(Page.zero());
        }
        request.setLogRequest(true);
        return CommonResponse.success(generalMessageOldService.pageMsgInfo(request));
    }

    @Override
    public CommonResponse<Integer> countUnreadFromOldMessage(GeneralMessageOldDataStatisticRequest request) {
        PersonDTO person = PersonDTO.from(request.getPersonId(), request.getIdentityId(), request.getIdentityType());
        return CommonResponse.success(generalMessageOldService.countUnread(person));
    }
}
