package me.qwq.doghouse.controller.api.v2;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import me.qwq.dogface.Dogface;
import me.qwq.doghouse.component.Rk;
import me.qwq.doghouse.controller.common.BaseController;
import me.qwq.doghouse.exception.PageNotFoundException;
import me.qwq.doghouse.pojo.dto.R;
import me.qwq.doghouse.service.CommentService;
import me.qwq.doghouse.service.StatisticsService;
import me.qwq.doghouse.util.CompressUtils;
import me.qwq.doghouse.util.GravatarUtils;

@RestController
@RequestMapping("/api/v2/gravatar")
@Slf4j
public class GravatarApiControllerV2 extends BaseController {
    
    @Autowired
    StatisticsService statisticsService;
    
    @Autowired
    GravatarUtils gravatarUtils;
    
    @Autowired
    CommentService commentService;
    
    @Autowired
    Dogface dogface;

    @GetMapping(value={
            "/get/{hash}", 
            "/get/{hash}/{size}"}, produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getGravatar(@PathVariable(value="hash") String hash,
            @PathVariable(value="size", required=false) Integer size, 
            @RequestParam(required=false) String nocache) {

        if (size == null || size <= 0) size = 36;
        Future<byte[]> future = 
                gravatarUtils.getReversedProxyGravatarAsync(hash, size, StringUtils.isBlank(nocache));
        
        byte[] avatar;
        try {
            avatar = future.get();
        }
        catch (InterruptedException | ExecutionException e) {
            throw new PageNotFoundException();
        }
        if (avatar == null) {
            throw new PageNotFoundException();
        }
        try {
            response.setHeader("Content-Encoding", "gzip");
            return CompressUtils.compressGZ(avatar);
        }
        catch (IOException e) {
            log.error("GZIP 压缩 gravatar 头像错误", e);
            throw new PageNotFoundException();
        }
    }
    
    @GetMapping(value={
            "/dog/{seed}", 
            "/dog/{seed}/{size}"}, produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] getDogavatar(@PathVariable(value="seed") String seed,
            @PathVariable(value="size", required=false) Integer size) throws IOException {
        
        if (size == null || size <= 0) size = 36;
        byte[] avatar = dogface.getDogavatar(seed, size);
        if (avatar == null) {
            throw new PageNotFoundException();
        }
        try {
            response.setHeader("Content-Encoding", "gzip");
            return CompressUtils.compressGZ(avatar);
        }
        catch (IOException e) {
            log.error("GZIP 压缩 gravatar 头像错误", e);
            throw new PageNotFoundException();
        }
    }
    @GetMapping(value={
            "/clearCache"})
    public R<?> clearGravatarCache() {
        
        Rk.Gravatar.clearGravatarCache();
        return R.ok();
        
    }

}
