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.*;

import java.util.List;

@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,
                              @RequestParam(value = "tags", required = false) List<Integer> tags,
                              @RequestParam(value = "mode", defaultValue = "3", required = false)String mode){
        if(link == null || targetResolution == null)
            return Response._failure("参数不全");
        return galleryManageService.createTask(link, targetResolution, AuthCode, tags, Byte.parseByte(mode));
    }

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

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


    @GetMapping("/onlineImage/{page}")
    public void getOnlineImage(Integer gid, @PathVariable("page") Short page, HttpServletRequest request, HttpServletResponse response){
        galleryManageService.getOnlineImage(gid, page, request, response);
    }


    @PostMapping("/share")
    public String shareGallery(Integer gid, Integer userId, Integer expireHour){
        if(userId.equals(3))
            return galleryManageService.shareGallery(gid, expireHour);
        return Response._failure("非法访问");
    }

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