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

import cn.axzo.msg.center.common.utils.PageHelperUtil;
import cn.axzo.msg.center.message.service.MessageTemplateGroupService;
import cn.axzo.msg.center.message.service.MessageTemplateNewService;
import cn.axzo.msg.center.service.dto.MessageBaseTemplateDTO;
import cn.axzo.msg.center.service.group.client.MessageTemplateGroupRelationClient;
import cn.axzo.msg.center.service.group.request.MessageTemplateGroupRelationMoveRequest;
import cn.axzo.msg.center.service.group.request.MessageTemplateGroupRelationPageRequest;
import cn.axzo.msg.center.service.group.request.MessageTemplateGroupRelationRemoveRequest;
import cn.axzo.msg.center.service.group.response.MessageTemplateGroupRelationResponse;
import cn.axzo.msg.center.service.template.request.MessageTemplatePageRequest;
import cn.azxo.framework.common.model.CommonResponse;
import cn.azxo.framework.common.model.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cold_blade
 * @date 2023/10/17
 * @version 1.0
 */
@RestController
@RequiredArgsConstructor
public class MessageTemplateGroupRelationController implements MessageTemplateGroupRelationClient {

    private final MessageTemplateNewService messageTemplateNewService;
    private final MessageTemplateGroupService messageTemplateGroupService;

    @Override
    public CommonResponse<Page<MessageTemplateGroupRelationResponse>> page(MessageTemplateGroupRelationPageRequest request) {
        MessageTemplatePageRequest pageRequest = new MessageTemplatePageRequest();
        pageRequest.setPage(request.getPage());
        pageRequest.setPageSize(request.getPageSize());
        pageRequest.setTemplateName(request.getTemplateName());
        pageRequest.setGroupNodeCode(request.getNodeCode());
        Page<MessageBaseTemplateDTO> result = messageTemplateNewService.pageBaseTemplate(pageRequest);
        return CommonResponse.success(PageHelperUtil.convert(result, this::convert));
    }

    @Override
    public CommonResponse<Void> move(MessageTemplateGroupRelationMoveRequest request) {
        messageTemplateGroupService.move(request.getCurNodeCode(), request.getTargetNodeCode(),
                request.getTemplateCodes());
        return CommonResponse.success();
    }

    @Override
    public CommonResponse<Void> remove(MessageTemplateGroupRelationRemoveRequest request) {
        messageTemplateGroupService.remove(request.getCurNodeCode(), request.getTemplateCodes());
        return CommonResponse.success();
    }

    private MessageTemplateGroupRelationResponse convert(MessageBaseTemplateDTO dto) {
        MessageTemplateGroupRelationResponse response = new MessageTemplateGroupRelationResponse();
        response.setTitle(dto.getTitle());
        response.setTemplateCode(dto.getCode());
        response.setTemplateName(dto.getName());
        response.setCreateTimestamp(dto.getCreateTimestamp());
        return response;
    }
}
