package com.lion.lionwebsite.Controller;


import com.lion.lionwebsite.Domain.User;
import com.lion.lionwebsite.Service.PublicServiceImpl;
import com.lion.lionwebsite.Service.RemoteService;
import com.lion.lionwebsite.Service.SubService;
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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

@RestController
@Slf4j
public class PublicController {

    final List<String> black_share_codes = new LinkedList<>();

    PublicServiceImpl publicService;

    RemoteService remoteService;

    SubService subService;

    public PublicController(PublicServiceImpl publicService, RemoteService remoteService, SubService subService) {
        this.publicService = publicService;
        this.remoteService = remoteService;
        this.subService = subService;
    }

    @GetMapping("/ip")
    public void ip(HttpServletRequest request, String auth, HttpServletResponse response) throws IOException {
        String ip = request.getHeader("X-Forwarded-For");
        if(ip == null)
            ip = request.getRemoteAddr();

        if(auth != null && auth.equals("ip"))
            publicService.logIpAddress(ip);

        log.info("ip地址:" + ip + " auth=" + auth);
        response.getOutputStream().write(ip.getBytes(StandardCharsets.UTF_8));
    }

    @GetMapping("/sub/{client}/{key}")
    public void publicSub(@PathVariable("key") String key,
                          @PathVariable("client") String client,
                          HttpServletResponse response,
                          HttpServletRequest request) {
        subService.updateSub(response, request, client, key);
    }

    @GetMapping("/GetFile/{path}")
    public void getFile(HttpServletRequest request, HttpServletResponse response, String ShareCode, @PathVariable("path") String path) throws IOException {
        synchronized (black_share_codes) {
            if (black_share_codes.contains(ShareCode))
                return;
        }

        log.info("ShareCode:{}", ShareCode);
        log.info("Path:{}", path);
        boolean result = publicService.GetFile(request, response, ShareCode);

        if(!result)
            black_share_codes.add(ShareCode);

        if(black_share_codes.size() > 100)
            black_share_codes.remove(0);
    }

    @PostMapping("/validate")
    public String validate(String AuthCode){
        Response response = Response.generateResponse();
        User user = publicService.getUserId(AuthCode);
        String isAvailable = remoteService.isDead() ? "false": "true";
        response.success(String.format("{\"userId\": %d, " +
                                        "\"username\": \"%s\", " +
                                        "\"isAvailable\": %s}", user.getId(), user.getUsername(), isAvailable));
        return response.toJSONString();
    }

    @PutMapping("/AuthCode")
    public String alterAuthCode(String AuthCode, String newAuthCode){
        return publicService.alterAuthCode(AuthCode, newAuthCode);
    }

    @GetMapping("/GalleryManage/ehThumbnail")
    public void getEhThumbnail(String path, HttpServletResponse response){
        publicService.getEhThumbnail(path, response);
    }

}
