package com.mcwl.web.controller.resource;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.mcwl.common.annotation.RepeatSubmit;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.resource.domain.ModelImage;
import com.mcwl.resource.domain.dto.ModelImagePageRes;
import com.mcwl.resource.domain.dto.ModelImageRes;
import com.mcwl.resource.domain.response.ResponseModelImage;
import com.mcwl.resource.domain.vo.ModelImageVo;
import com.mcwl.resource.domain.vo.PageVo;
import com.mcwl.resource.service.ModelImageLikeService;
import com.mcwl.resource.service.ModelImageService;
import com.mcwl.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 图片
 */
@Api(tags = "图片")
@RestController
@RequestMapping("/image")
@RequiredArgsConstructor
public class ModelImageController {

    private final ModelImageService modelImageService;

    private final ModelImageLikeService modelImageLikeService;

    private final ISysUserService sysUserService;


    /**
     * 图片列表
     */
    @ApiOperation(value = "图片列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody
                              @Valid
                              PageDomain pageDomain) {
        ModelImagePageRes imagePageRes = new ModelImagePageRes();
        BeanUtil.copyProperties(pageDomain, imagePageRes);
        return modelImageService.listByPage(imagePageRes);
    }

    /**
     * 图片详情
     */
    @ApiOperation(value = "图片详情")
    @GetMapping("/detail")
    public R<ModelImageVo> detail(@Valid @NotNull(message = "图片id不能为空") Long id) {
        ModelImageVo modelImageVo = modelImageService.getDetail(id);
        return R.ok(modelImageVo);
    }

    /**
     * 图片删除
     */
    @ApiOperation(value = "图片删除")
    @GetMapping("/delete")
    public R<Object> delete(@Valid @NotNull(message = "图片id不能为空") Long id) {
        modelImageService.removeById(id);
        return R.ok();
    }

    /**
     * 图片修改
     */
    @ApiOperation(value = "图片修改")
    @PostMapping("/update")
    public R<Object> update(@Valid @RequestBody ModelImageRes modelImageRes) {
        modelImageService.updateById(modelImageRes);
        return R.ok();
    }


    /**
     * 图片发布
     */
    @ApiOperation(value = "图片发布")
    @PostMapping("/publish")
    public R<Object> publish(@Valid @RequestBody ModelImageRes modelImageRes) {

        return modelImageService.publish(modelImageRes);
    }

    /**
     * 图片点赞/取消
     */
    @ApiOperation(value = "图片点赞/取消")
    @RepeatSubmit
    @GetMapping("/imageLike")
    public R<Object> like(@Valid @NotNull(message = "图片id不能为空") Long id) {
        modelImageLikeService.like(id);
        return R.ok();
    }


    /**
     * 设置图片置顶
     *
     * @param id
     * @param isTop
     * @return
     */
    @ApiOperation(value = "设置图片置顶状态")
    @GetMapping("/{id}/top")
    public R<Object> setModelImageTop(@PathVariable Long id, @RequestParam boolean isTop) {
        modelImageLikeService.setModelImageTop(id, isTop);
        return R.ok();
    }


    /**
     * 获取置顶的图片列表
     *
     * @return
     */
    @ApiOperation(value = "获取置顶的图片列表")
    @GetMapping
    public R<List<ModelImage>> fetchModelImagesSortedByTopStatus() {
        List<ModelImage> models = modelImageLikeService.fetchModelImageSortedByTopStatus();
        return R.ok(models);
    }

    /**
     * 作品灵感
     *
     * @return
     */
    @ApiOperation(value = "作品灵感")
    @GetMapping("/imageList")
    public R<PageInfo<ResponseModelImage>> imageList(PageVo pageVo) {
        PageInfo<ResponseModelImage> models = modelImageLikeService.imageList(pageVo);
        return R.ok(models);
    }

}
