package vip.xiaonuo.exam.controller.admin;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.exam.base.BaseApiController;
import vip.xiaonuo.exam.base.RestResponse;
import vip.xiaonuo.exam.domain.Message;
import vip.xiaonuo.exam.domain.MessageUser;
import vip.xiaonuo.exam.service.MessageService;
import vip.xiaonuo.exam.utility.DateTimeUtil;
import vip.xiaonuo.exam.utility.PageInfoHelper;
import vip.xiaonuo.exam.viewmodel.admin.message.MessagePageRequestVM;
import vip.xiaonuo.exam.viewmodel.admin.message.MessageResponseVM;
import vip.xiaonuo.exam.viewmodel.admin.message.MessageSendVM;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController("AdminMessageController")
@RequestMapping(value = "/api/admin/message")
public class MessageController extends BaseApiController {

    private final MessageService messageService;


    @Autowired
    public MessageController(MessageService messageService) {
        this.messageService = messageService;

    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<MessageResponseVM>> pageList(@RequestBody MessagePageRequestVM model) {
        PageInfo<Message> pageInfo = messageService.page(model);
        List<Integer> ids = pageInfo.getList().stream().map(d -> d.getId()).collect(Collectors.toList());
        List<MessageUser> messageUsers = ids.size() == 0 ? null : messageService.selectByMessageIds(ids);
        PageInfo<MessageResponseVM> page = PageInfoHelper.copyMap(pageInfo, m -> {
            MessageResponseVM vm = modelMapper.map(m, MessageResponseVM.class);
            String receives = messageUsers.stream().filter(d -> d.getMessageId().equals(m.getId())).map(d -> d.getReceiveUserName())
                    .collect(Collectors.joining(","));
            vm.setReceives(receives);
            vm.setCreateTime(DateTimeUtil.dateFormat(m.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(page);
    }


    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public RestResponse send(@RequestBody @Valid MessageSendVM model) {
        SaBaseLoginUser user = getCurrentUser();
  //      List<User> receiveUser = userService.selectByIds(model.getReceiveUserIds());
        Date now = new Date();
        Message message = new Message();
        message.setTitle(model.getTitle());
        message.setContent(model.getContent());
        message.setCreateTime(now);
        message.setReadCount(0);
      //  message.setReceiveUserCount(receiveUser.size());
        message.setSendUserId(user.getId());
        message.setSendUserName(user.getName());
        message.setSendRealName(user.getName());
//        List<MessageUser> messageUsers = receiveUser.stream().map(d -> {
//            MessageUser messageUser = new MessageUser();
//            messageUser.setCreateTime(now);
//            messageUser.setReaded(false);
//            messageUser.setReceiveRealName(d.getRealName());
//            messageUser.setReceiveUserId(d.getId());
//            messageUser.setReceiveUserName(d.getUserName());
//            return messageUser;
//        }).collect(Collectors.toList());
        messageService.sendMessage(message, null);
        return RestResponse.ok();
    }

}
