package com.lion.lionwebsite.Controller;

import com.lion.lionwebsite.Service.LocalServiceImpl;
import com.lion.lionwebsite.Service.PersonalServiceImpl;
import com.lion.lionwebsite.Util.FileDownload;
import com.lion.lionwebsite.Util.Response;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@Slf4j
@RequestMapping("/personal")
public class PersonalController {

    PersonalServiceImpl personalService;

    LocalServiceImpl localService;

    public PersonalController(PersonalServiceImpl personalService, LocalServiceImpl localService) {
        this.personalService = personalService;
        this.localService = localService;
    }

    @GetMapping("/sub/self")
    public void sub(HttpServletResponse response, HttpServletRequest request){
        FileDownload.export(request, response, "sub/sub.txt");
    }

    @GetMapping("/files")
    public String file(String path) throws IOException {
        return personalService.getFiles(path);
    }

    @PostMapping("/uploadFile")
    public String uploadFile(String path, String fileName, MultipartFile file){
        return personalService.uploadFile(path, fileName, file);
    }

    @GetMapping("/private/**")
    public void getFile(HttpServletRequest request, HttpServletResponse response, String path){
        personalService.download(request, response, path);
    }

    @PostMapping("/share")
    public String shareFile(String path, Integer expireHour) {
        return personalService.shareFile(path, expireHour);
    }

    @PostMapping("/compress")
    public String compress(String path){
        return personalService.compress(path);
    }

    @PostMapping("/delete")
    public String deleteFile(String path){
        return personalService.deleteFile(path);
    }

    @PostMapping("/extendShareTime")
    public String extendShareTime(String path, Integer extendHour) {
        return personalService.extendShareTime(path, extendHour);
    }

    @PostMapping("/cancelShare")
    public String cancelShare(String path){
        return personalService.cancelShare(path);
    }

    @PostMapping("/updateSub")
    public String updateSub() throws IOException {
        Response response = Response.generateResponse();
        if(localService.updateSub(true))
            response.success();
        else
            response.failure();
        return response.toJSONString();
    }

    @GetMapping("/lastUpdate")
    public String lastUpdate(){
        return personalService.lastUpdate();
    }

    @GetMapping("/ip")
    public String ip() throws JsonProcessingException {
        return personalService.getIp();
    }
}
