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

import cn.axzo.msg.center.message.domain.param.MessageTemplateV3SaveOrUpdateParam;
import cn.axzo.msg.center.message.domain.param.RelationTemplateMapInitParam;
import cn.axzo.msg.center.message.domain.vo.RelationTemplateMapInitRequest;
import cn.axzo.msg.center.message.service.MessageTemplateNewService;
import cn.axzo.msg.center.message.service.MessageTemplateV3Service;
import cn.axzo.msg.center.message.service.RelationTemplateMapService;
import cn.axzo.msg.center.message.service.impl.MessageTemplateV3SyncService;
import cn.axzo.msg.center.message.xxl.UpdateTemplateV3ChannelStyleVersionJob;
import cn.axzo.msg.center.service.template.client.MessageTemplateV3Client;
import cn.axzo.msg.center.service.template.request.MessageTemplateSyncQueryRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateV3CreateRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateV3DeleteRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateV3PageRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateV3SyncRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateV3SyncResponse;
import cn.axzo.msg.center.service.template.request.MessageTemplateV3UpdateRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateV3UpdateStatusRequest;
import cn.axzo.msg.center.service.template.request.TemplateV3UpdateChannelStyleJobRequest;
import cn.axzo.msg.center.service.template.response.MessageDetailStyle;
import cn.axzo.msg.center.service.template.response.MessageTemplateV3ConfigStateResponse;
import cn.axzo.msg.center.service.template.response.MessageTemplateV3DetailResponse;
import cn.axzo.msg.center.service.template.response.MessageTemplateV3PageResponse;
import cn.azxo.framework.common.model.CommonResponse;
import cn.azxo.framework.common.model.Page;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 * 消息模板管理
 *
 * @author cold_blade
 * @date 2023/10/5
 * @version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class MessageTemplateV3Controller implements MessageTemplateV3Client {

    private final MessageTemplateNewService messageTemplateNewService;
    private final RelationTemplateMapService relationTemplateMapService;
    private final MessageTemplateV3SyncService messageTemplateV3SyncService;
    private final MessageTemplateV3Service messageTemplateV3Service;
    private final UpdateTemplateV3ChannelStyleVersionJob updateTemplateV3ChannelStyleVersionJob;

    @Override
    public CommonResponse<String> save(MessageTemplateV3CreateRequest request) {
        log.info("MessageTemplateV3Controller#save,request:{}", JSON.toJSONString(request));
        return CommonResponse.success(
                messageTemplateV3Service.createTemplate(MessageTemplateV3SaveOrUpdateParam.from(request)));
    }

    @Override
    public CommonResponse<Void> update(MessageTemplateV3UpdateRequest request) {
        log.info("MessageTemplateV3Controller#update,request:{}", JSON.toJSONString(request));
        MessageTemplateV3DetailResponse detail = messageTemplateV3Service.detail(request.getTemplateCode());
        messageTemplateV3Service.updateTemplate(MessageTemplateV3SaveOrUpdateParam.from(request, detail));
        return CommonResponse.success();
    }

    @Override
    public CommonResponse<Void> delete(MessageTemplateV3DeleteRequest request) {
        log.info("MessageTemplateV3Controller#delete,request:{}", JSON.toJSONString(request));
        messageTemplateV3Service.deleteTemplate(MessageTemplateV3SaveOrUpdateParam.from(request));
        return CommonResponse.success();
    }

    @Override
    public CommonResponse<MessageTemplateV3DetailResponse> detail(String templateCode) {
        log.info("MessageTemplateV3Controller#detail,templateCode:{}", templateCode);
        return CommonResponse.success(messageTemplateV3Service.detail(templateCode));
    }

    @Override
    public CommonResponse<Page<MessageTemplateV3PageResponse>> page(MessageTemplateV3PageRequest request) {
        log.info("MessageTemplateV3Controller#detail,request:{}", JSON.toJSONString(request));
        return CommonResponse.success(messageTemplateV3Service.page(request));
    }

    @Override
    public CommonResponse<List<MessageTemplateV3PageResponse>> listByCodes(Collection<String> templateCodes) {
        return CommonResponse.success(messageTemplateV3Service.listByCodes(templateCodes));
    }

    @Override
    public CommonResponse<Void> updateStatus(MessageTemplateV3UpdateStatusRequest request) {
        messageTemplateV3Service.updateStatus(request.getOperatorId(), request.getTemplateCode(), request.getStatus());
        return CommonResponse.success();
    }

    @Override
    public CommonResponse<List<MessageDetailStyle>> listTemplateDetailStyles() {
        List<MessageDetailStyle> styles = messageTemplateNewService.listTemplateDetailStyles();
        return CommonResponse.success(styles);
    }

    @Override
    public CommonResponse<MessageTemplateV3SyncResponse>
    getSyncMessageTemplate(MessageTemplateSyncQueryRequest request) {
        log.info("getSyncMessageTemplate param= [{}]", JSONUtil.toJsonStr(request));
        return CommonResponse.success(messageTemplateV3SyncService.getSyncMessageTemplate(request));
    }

    @Override
    public CommonResponse<String> syncTemplate(MessageTemplateV3SyncRequest request) {
        log.info("syncTemplate param= [{}]", JSONUtil.toJsonStr(request));
        return CommonResponse.success(messageTemplateV3SyncService.syncTemplate(request));
    }

    @PostMapping(value = "/message/template/relation/init/v3", produces = {MediaType.APPLICATION_JSON_VALUE})
    public CommonResponse<Void> initRelationTemplateMap(@RequestBody @Valid RelationTemplateMapInitRequest request) {
        RelationTemplateMapInitParam param = request.toInitParam();
        relationTemplateMapService.init(param);
        return CommonResponse.success();
    }

    /**
     * 配置状态戳
     */
    @Override
    public CommonResponse<List<MessageTemplateV3ConfigStateResponse>> configState() {
        return CommonResponse.success(MessageTemplateV3ConfigStateResponse.cardStateToResp());
    }

    /**
     * 定时更新模版V3信息
     */
    @Override
    public CommonResponse<Void> updateChannelStyleVersionJob(@RequestBody @Valid TemplateV3UpdateChannelStyleJobRequest request) throws Exception {
        updateTemplateV3ChannelStyleVersionJob.execute(request.getParams());
        return CommonResponse.success();
    }
}
