package com.lion.lionwebsite.Controller;

import com.lion.lionwebsite.Service.CollectService;
import com.lion.lionwebsite.Service.GalleryManageService;
import com.lion.lionwebsite.Service.UserServiceImpl;
import com.lion.lionwebsite.Util.Response;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/GalleryManage")
@Slf4j
public class GalleryManageController {
    GalleryManageService galleryManageService;

    CollectService collectService;

    UserServiceImpl userService;

    public GalleryManageController(GalleryManageService galleryManageService, CollectService collectService, UserServiceImpl userService) {
        this.galleryManageService = galleryManageService;
        this.collectService = collectService;
        this.userService = userService;
    }

    @PostMapping("")
    public String create_task(String link, String targetResolution, String AuthCode){
        if(link == null || targetResolution == null)
            return Response._failure("参数不全");
        return galleryManageService.createTask(link, targetResolution, AuthCode);
    }

    @GetMapping("")
    public String selectGallery(String param, String type, String AuthCode) {
        int userId = userService.getUserId(AuthCode);   //能调到这里的授权码对应用户不可能为空

        if(type == null)
            return Response._failure("参数不全");
        return switch (type) {
            case "link" -> galleryManageService.selectTaskByLink(param);
            case "gid" -> galleryManageService.selectTaskByGid(Integer.parseInt(param));
            case "all" -> galleryManageService.selectAllGallery(userId);
            case "name" -> galleryManageService.selectGalleryByName(param);
            case "undone" -> galleryManageService.selectUnDoneGallery();
            case "downloader" ->  galleryManageService.selectGalleryByDownloader(AuthCode);
            default -> Response._failure("参数错误");
        };
    }

    @DeleteMapping("")
    public String deleteTask(Integer gid, String AuthCode){
        if(gid == null)
            return Response._failure("参数不全");
        return galleryManageService.deleteGalleryByGid(gid, AuthCode);
    }


    @PostMapping("/collect")
    public String collectGallery(Integer gid, String AuthCode){
        return collectService.collectGallery(gid, userService.getUserId(AuthCode));
    }

    @PostMapping("/disCollect")
    public String disCollectGallery(Integer gid, String AuthCode){
        return collectService.disCollectGallery(gid, userService.getUserId(AuthCode));
    }

    @GetMapping("/weekUsedAmount")
    public String getWeekUsedAmount(){
        return galleryManageService.getWeekUsedAmount();
    }

    @PostMapping("/cache")
    public String cacheImageKeys(String url){
        return galleryManageService.cacheImagesKey(url);
    }

    @GetMapping("/onlineImage/{page}")
    public void getCacheImage(String gid, @PathVariable("page") int page, HttpServletRequest request, HttpServletResponse response){
        galleryManageService.getCachedImage(gid, page, request, response);
    }

    @PostMapping("/reset")
    public String resetUndone(){
        return galleryManageService.resetUndone();
    }
}
