package com.ruoyi.web.controller.front;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.generator.domain.SourceProject;
import com.ruoyi.generator.service.ISourceProjectService;
import com.ruoyi.system.service.ISysDictTypeService;

@RestController
@RequestMapping("/front/source")
public class FrontSourceController extends BaseController
{
    private static final String SOURCE_PROJECT_CATEGORY_DICT_TYPE = "source_project_category";

    @Autowired
    private ISourceProjectService sourceProjectService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @GetMapping("/categories")
    public AjaxResult categories()
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(SOURCE_PROJECT_CATEGORY_DICT_TYPE);
        if (data == null)
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    @GetMapping("/projects")
    public AjaxResult projects(SourceProject query)
    {
        return success(sourceProjectService.listPublicProjects(optionalUserId(), query));
    }

    @GetMapping("/projects/{slug}")
    public AjaxResult detail(@PathVariable String slug)
    {
        return success(sourceProjectService.getPublicProject(optionalUserId(), slug, true));
    }

    @PostMapping("/projects/{projectId}/purchase")
    public AjaxResult purchase(@PathVariable Long projectId)
    {
        Long userId = SecurityUtils.getUserId();
        return success(sourceProjectService.purchase(userId, projectId));
    }

    @GetMapping("/purchases")
    public AjaxResult purchases()
    {
        Long userId = SecurityUtils.getUserId();
        return success(sourceProjectService.listPurchases(userId));
    }

    @GetMapping("/projects/{projectId}/download")
    public AjaxResult download(@PathVariable Long projectId)
    {
        Long userId = SecurityUtils.getUserId();
        return success(sourceProjectService.getDownloadInfo(userId, projectId));
    }

    private Long optionalUserId()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || authentication.getPrincipal() == null
                || "anonymousUser".equals(authentication.getPrincipal()))
        {
            return null;
        }
        if (authentication.getPrincipal() instanceof LoginUser)
        {
            return ((LoginUser) authentication.getPrincipal()).getUserId();
        }
        return null;
    }
}
