package org.xyzh.news.controller;

import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.news.resource.ResourceService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.resource.TbResource;
import org.xyzh.common.dto.user.TbSysUser;
import org.xyzh.common.dto.usercenter.TbUserCollection;
import org.xyzh.common.utils.TimeUtils;
import org.xyzh.common.vo.ResourceVO;
import org.xyzh.system.utils.LoginUtil;
/**
 * @description 资源控制器
 * @filename ResourceController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/news/resources")
public class ResourceController {
    private static final Logger logger = LoggerFactory.getLogger(ResourceController.class);

    @Autowired
    private ResourceService resourceService;

    /**
     * 获取资源列表
     */
    @GetMapping("/list")
    public ResultDomain<TbResource> getResourceList(TbResource filter) {
        return resourceService.getResourceList(filter);
    }

    /**
     * 获取资源分页
     */
    @PostMapping("/page")
    public ResultDomain<TbResource> getResourcePage(@RequestBody PageRequest<TbResource> request) {
        TbResource filter = request.getFilter();
        PageParam pageParam = request.getPageParam();
        return resourceService.getResourcePage(filter, pageParam);
    }

    /**
     * 根据ID获取资源详情
     */
    @GetMapping("/resource/{resourceID}")
    public ResultDomain<ResourceVO> getResourceById(@PathVariable("resourceID") String resourceID) {
        return resourceService.getResourceById(resourceID);
    }

    /**
     * 创建资源
     */
    @PostMapping("/resource")
    public ResultDomain<ResourceVO> createResource(@RequestBody ResourceVO resourceVO) {
        TbSysUser user = LoginUtil.getCurrentUser();
        if (user == null) {
            ResultDomain<ResourceVO> result = new ResultDomain<ResourceVO>();
            result.fail("请先登录");
            return result;
        }
        resourceVO.getResource().setCreator(user.getID());
        resourceVO.getResource().setAuthor(user.getUsername());
        Date now = new Date();
        resourceVO.getResource().setCreateTime(now);
        resourceVO.getResource().setPublishTime(now);
        return resourceService.createResource(resourceVO);
    }

    /**
     * 更新资源
     */
    @PutMapping("/resource")
    public ResultDomain<ResourceVO> updateResource(@RequestBody ResourceVO resource) {
        return resourceService.updateResource(resource);
    }

    /**
     * 删除资源
     */
    @DeleteMapping("/resource/{resourceID}")
    public ResultDomain<Boolean> deleteResource(@PathVariable("resourceID") String resourceID) {
        return resourceService.deleteResource(resourceID);
    }

    /**
     * 更新资源状态
     */
    @PutMapping("/resource/{resourceID}/status")
    public ResultDomain<TbResource> updateResourceStatus(
            @PathVariable("resourceID") String resourceID,
            @RequestParam("status") Integer status) {
        return resourceService.updateResourceStatus(resourceID, status);
    }

    /**
     * 发布资源
     */
    @PostMapping("/resource/{resourceID}/publish")
    public ResultDomain<TbResource> publishResource(@PathVariable("resourceID") String resourceID) {
        return resourceService.publishResource(resourceID);
    }

    /**
     * 下架资源
     */
    @PostMapping("/resource/{resourceID}/unpublish")
    public ResultDomain<TbResource> unpublishResource(@PathVariable("resourceID") String resourceID) {
        return resourceService.unpublishResource(resourceID);
    }

    /**
     * 增加浏览次数
     */
    @PostMapping("/resource/{resourceID}/view")
    public ResultDomain<Boolean> incrementViewCount(@PathVariable("resourceID") String resourceID) {
        return resourceService.incrementViewCount(resourceID);
    }

    /**
     * 增加点赞次数
     */
    @PostMapping("/resource/{resourceID}/like")
    public ResultDomain<TbResource> incrementLikeCount(@PathVariable("resourceID") String resourceID) {
        return resourceService.incrementLikeCount(resourceID);
    }

    /**
     * 增加收藏次数
     */
    @PostMapping("/resource/collect")
    public ResultDomain<TbResource> resourceCollect(@RequestBody TbUserCollection collection) {
        return resourceService.resourceCollect(collection);
    }

    /**
     * 设置资源推荐
     */
    @PutMapping("/resource/{resourceID}/recommend")
    public ResultDomain<TbResource> setResourceRecommend(
            @PathVariable("resourceID") String resourceID,
            @RequestParam("isRecommend") Boolean isRecommend) {
        return resourceService.setResourceRecommend(resourceID, isRecommend);
    }

    /**
     * 设置资源轮播
     */
    @PutMapping("/resource/{resourceID}/banner")
    public ResultDomain<TbResource> setResourceBanner(
            @PathVariable("resourceID") String resourceID,
            @RequestParam("isBanner") Boolean isBanner) {
        return resourceService.setResourceBanner(resourceID, isBanner);
    }

    /**
     * 获取推荐资源列表
     */
    @GetMapping("/recommend")
    public ResultDomain<TbResource> getRecommendResources(@RequestParam(required = false) Integer limit) {
        return resourceService.getRecommendResources(limit);
    }

    /**
     * 获取轮播资源列表
     */
    @GetMapping("/banner")
    public ResultDomain<TbResource> getBannerResources(@RequestParam(required = false) Integer limit) {
        return resourceService.getBannerResources(limit);
    }

    /**
     * 搜索资源
     */
    @GetMapping("/search")
    public ResultDomain<TbResource> searchResources(
            @RequestParam("keyword") String keyword,
            @RequestParam(value = "categoryID", required = false) String categoryID,
            @RequestParam(value = "status", required = false) Integer status) {
        return resourceService.searchResources(keyword, categoryID, status);
    }
}
