package cn.iselab.mooctest.user.web.logic.impl;

import cn.iselab.mooctest.rpc.user.data.MedalCaughtDTO;
import cn.iselab.mooctest.rpc.user.data.MedalDTO;
import cn.iselab.mooctest.user.constants.DeleteStatus;
import cn.iselab.mooctest.user.model.Medal;
import cn.iselab.mooctest.user.model.User2Medal;
import cn.iselab.mooctest.user.service.MedalService;
import cn.iselab.mooctest.user.web.exception.HttpBadRequestException;
import cn.iselab.mooctest.user.web.logic.BaseLogic;
import cn.iselab.mooctest.user.web.logic.MedalLogic;
import cn.iselab.mooctest.user.web.wrapper.MedalCaughtWrapper;
import cn.iselab.mooctest.user.web.wrapper.Medalwrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @Author ROKG
 * @Description
 * @Date: Created in 下午11:05 2018/2/13
 * @Modified By:
 */
@Service
public class MedalLogicImpl extends BaseLogic implements MedalLogic {

    @Autowired
    MedalService medalService;

    @Autowired
    Medalwrapper medalwrapper;

    @Autowired
    MedalCaughtWrapper medalCaughtWrapper;

    @Override
    public MedalDTO saveMedal(MedalDTO dto){
        Medal medal=medalwrapper.unwrap(dto);
        medal.setCreateTime(new Timestamp(System.currentTimeMillis()));
        medal.setDeleted(DeleteStatus.IS_NOT_DELETED);
        medal=medalService.saveMedal(medal);
        return medalwrapper.wrap(medal);
    }

    @Override
    public MedalDTO getMedal(long medalId){
        Medal medal=medalService.findById(medalId);
        return medalwrapper.wrap(medal);
    }

    @Override
    public MedalDTO updateMedal(MedalDTO dto){
        Medal medal=medalService.findById(dto.getId());
        if (medal == null) {
            throw new HttpBadRequestException("medal not exists");
        }
        medal.setName(dto.getName());
        medal.setDescription(dto.getDescription());
        medal.setImgUrl(dto.getImgUrl());
        medalService.updateMedal(medal);
        return dto;
    }

    @Override
    public void deleteMedal(long medalId){
        Medal medal=medalService.findById(medalId);
        if (medal == null) {
            throw new HttpBadRequestException("medal not exists");
        }
        medal.setDeleted(DeleteStatus.IS_DELETED);
        medalService.updateMedal(medal);
    }

    @Override
    public List<MedalDTO> getMedals(String keyword, Long userId){
        List<Medal> medals=medalService.findByKeyword(keyword);
        return medals.parallelStream().map(medal -> {
            MedalDTO medalDTO=medalwrapper.wrap(medal);
            if (userId!=null) {
                medalDTO.setCaughted(medalService.findByUserIdAndMedalId(userId, medal.getId()) != null ? true : false);
            }
            return medalDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public MedalCaughtDTO saveMedalCaughtDTO(MedalCaughtDTO dto){
        User2Medal user2Medal=medalCaughtWrapper.unwrap(dto);
        user2Medal.setCreateTime(new Timestamp(System.currentTimeMillis()));
        user2Medal=medalService.saveUser2Medal(user2Medal);
        return medalCaughtWrapper.wrap(user2Medal);
    }

    @Override
    public List<MedalCaughtDTO> getMedalList(long userId){
        List<User2Medal> user2Medals=medalService.findByPage(userId);
        return user2Medals.parallelStream().map(user2Medal -> {
            MedalCaughtDTO dto=medalCaughtWrapper.wrap(user2Medal);
            Medal medal =medalService.findById(user2Medal.getMedalId());
            dto.setMedalDTO(medalwrapper.wrap(medal));
            return dto;
        }).collect(Collectors.toList());
    }
}
