package com.izouma.nineth.web.mobile.v1;


import com.izouma.nineth.domain.FileObject;
import com.izouma.nineth.dto.CollectionDTO;
import com.izouma.nineth.repo.CollectionRepo;
import com.izouma.nineth.service.CollectionService;
import com.izouma.nineth.service.LikeService;
import com.izouma.nineth.utils.SecurityUtils;
import com.izouma.nineth.web.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/collection/v1")
@AllArgsConstructor
public class CollectionControllerV1 extends BaseController {
    private final CollectionService collectionService;
    private final CollectionRepo collectionRepo;
    private final LikeService likeService;


    @GetMapping("/{id}/like")
    @ApiOperation("点赞")
    public void like(@PathVariable Long id) {
        likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
    }

    @GetMapping("/{id}/unlike")
    @ApiOperation("取消点赞")
    public void unlike(@PathVariable Long id) {
        likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
    }

    @GetMapping("/myLikes")
    @ApiOperation("我收藏的")
    public List<CollectionDTO> myLikes() {
        return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
    }


    @PostMapping("/appointment")
    public void appointment(@RequestParam Long id) {
        collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
    }

    @GetMapping("/recommend")
    public List<CollectionDTO> recommend(@RequestParam String type, @RequestParam(defaultValue = "0") int projectId) {
        return collectionService.toDTO(collectionRepo.recommend(type, projectId).stream().map(rc -> {
            if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
                rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
                        .getPic(), null, null)));
            }
            return rc.getCollection();
        }).collect(Collectors.toList()));
    }

}

