package com.jwipc.message.controller;

import cn.hutool.core.util.ObjectUtil;
import com.jwipc.message.dto.MessageAddresseeDTO;
import com.jwipc.message.dto.MessageCenterDTO;
import com.jwipc.message.logic.MessageAddresseeLogic;
import com.jwipc.message.logic.MessageCenterLogic;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.FileClient;
import com.jwipc.sys.client.dto.FileResposePreviewCDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import static com.jwipc.message.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 我发起的 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2024-07-03
 */
@Controller
@RequestMapping(MODULE_ROOT + "/received")
@RequiredArgsConstructor
public class MessageReceivedPageController {
    private final MessageAddresseeLogic messageAddresseeLogic;
    private final FileClient fileClient;

    @GetMapping(value = "dialog")
    public String dialog(Model model){
        List<MessageAddresseeDTO> addresseeMessages = messageAddresseeLogic.getReceived(LoginUtil.getUserId());
        model.addAttribute("addresseeMessages",addresseeMessages);
        model.addAttribute("addresseeMessageNum",addresseeMessages.size());
        return MODULE_ROOT + "/dialog";
    }

    @GetMapping("/dialog/look")
    public String detailLook(Model model, Long messageCenterId, Long addresseeId){
        MessageAddresseeDTO messageAddresseeDTO = new MessageAddresseeDTO();
        if (ObjectUtil.isNotNull(addresseeId)){
            messageAddresseeDTO = messageAddresseeLogic.getById(addresseeId);
        }
        if (StringUtils.isNotEmpty(messageAddresseeDTO.getAttachments())){
            List<FileResposePreviewCDTO> fileList = fileClient.getPreviewUrl(Arrays.asList(StringUtils.split(messageAddresseeDTO.getAttachments(), ",")));
            model.addAttribute("fileList", fileList);
        }
        model.addAttribute("data", messageAddresseeDTO);
        model.addAttribute("messageCenterId",messageCenterId);
        model.addAttribute("addresseeId",addresseeId);
        return MODULE_ROOT + "/dialog_look";
    }
}
