package cn.reghao.oss.sdk;

import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.oss.api.dto.DownloadUrl;
import cn.reghao.oss.api.dto.ObjectChannel;
import cn.reghao.oss.api.dto.ObjectInfo;
import cn.reghao.oss.api.dto.ServerInfo;
import cn.reghao.oss.api.dto.media.*;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * @author reghao
 * @date 2024-02-23 21:47:39
 */
public class OssConsoleClient {
    private final HttpClient httpClient = HttpClient.newBuilder().build();
    private final String endpoint;
    private String token;

    public OssConsoleClient(String endpoint, String accessKeyId, String accessKeySecret) throws Exception {
        this.endpoint = endpoint;
        auth(accessKeyId, accessKeySecret);
    }

    private void auth(String accessKeyId, String accessKeySecret) throws Exception {
        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
                .addPart("accessKeyId", accessKeyId)
                .addPart("accessKeySecret", accessKeySecret);

        String api = String.format("%s/api/oss/key/auth", endpoint);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .version(HttpClient.Version.HTTP_1_1)
                .header("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
                .POST(publisher.build())
                .build();

        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        this.token = webResult.getData();
    }

    public void registerNode(String jsonPayload) throws Exception {
        String api = String.format("%s/api/oss/store/node", endpoint);
        HttpClient httpClient = HttpClient.newBuilder().build();
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .header("content-type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    public ServerInfo getServerInfo(int channelId) throws Exception {
        String api = String.format("%s/api/oss/server/info?channelId=%s", endpoint, channelId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<ServerInfo>>(){}.getType();
        WebResult<ServerInfo> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public List<ObjectChannel> getObjectChannels(String nodeAddr) throws Exception {
        String api = String.format("%s/api/oss/channels?nodeAddr=%s", endpoint, nodeAddr);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<List<ObjectChannel>>>(){}.getType();
        WebResult<List<ObjectChannel>> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public ObjectChannel getObjectChannel(String nodeAddr, int channelId) throws Exception {
        String api = String.format("%s/api/oss/channel?nodeAddr=%s&channelId=%s", endpoint, nodeAddr, channelId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<ObjectChannel>>(){}.getType();
        WebResult<ObjectChannel> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public ObjectInfo getObjectInfo(String objectId) throws Exception {
        String api = String.format("%s/api/oss/object/info?objectId=%s", endpoint, objectId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<ObjectInfo>>(){}.getType();
        WebResult<ObjectInfo> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public DownloadUrl getDownloadUrl(String objectId, int channelId, long userId) throws Exception {
        String api = String.format("%s/api/oss/object/url?objectId=%s&channelId=%s&userId=%s", endpoint, objectId, channelId, userId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<DownloadUrl>>(){}.getType();
        WebResult<DownloadUrl> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public void convertVideo(String videoFileId) throws Exception {
        String api = String.format("%s/api/oss/media/convert/video/%s", endpoint, videoFileId);
        convert(api);
    }

    public void convertAudio(String audioFileId) throws Exception {
        String api = String.format("%s/api/oss/media/convert/audio/%s", endpoint, audioFileId);
        convert(api);
    }

    private void convert(String api) throws Exception {
        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .POST(publisher.build())
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    @Deprecated
    public void setVideoScope(String videoFileId, int scope) throws Exception {
        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        publisher.addPart("videoId", videoFileId);
        publisher.addPart("scope", scope+"");

        String api = String.format("%s/api/oss/media/scope/video", endpoint);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .POST(publisher.build())
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    @Deprecated
    public void setAudioScope(String audioFileId, int scope) throws Exception {
        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        publisher.addPart("audioFileId", audioFileId);
        publisher.addPart("scope", scope+"");

        String api = String.format("%s/api/oss/media/scope/audio", endpoint);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .POST(publisher.build())
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    @Deprecated
    public void setImagesScope(Set<String> imageFileIds, int scope) throws Exception {
        String api = String.format("%s/api/oss/media/scope/image", endpoint);
        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        publisher.addPart("imageFileIds", imageFileIds.toString());
        publisher.addPart("scope", scope+"");

        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .POST(publisher.build())
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    public void setObjectScope(int scope, String objectId, int contentType) throws Exception {
        String api = String.format("%s/api/oss/media/scope/object", endpoint);
        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        publisher.addPart("scope", scope+"");
        publisher.addPart("objectId", objectId);
        publisher.addPart("contentType", contentType+"");

        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .POST(publisher.build())
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    public void setObjectsScope(int scope, List<String> objectIds, int contentType) throws Exception {
        String api = String.format("%s/api/oss/media/scope/objects", endpoint);
        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        publisher.addPart("scope", scope+"");
        publisher.addPart("objectIds", objectIds.toString());
        publisher.addPart("contentType", contentType+"");

        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .POST(publisher.build())
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    public AudioInfo getAudioInfo(String audioFileId) throws Exception {
        String api = String.format("%s/api/oss/media/audio/info/%s", endpoint, audioFileId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<AudioInfo>>(){}.getType();
        WebResult<AudioInfo> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public List<AudioUrl> getAudioUrls(String audioFileId, long loginUser) throws Exception {
        String api = String.format("%s/api/oss/media/audio/url?audioFileId=%s&userId=%s", endpoint, audioFileId, loginUser);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<List<AudioUrl>>>(){}.getType();
        WebResult<List<AudioUrl>> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public void deleteByObjectNames(List<String> objectNames) throws Exception {
        String api = String.format("%s/api/oss/media/image/delete/name", endpoint);
        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);

        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        publisher.addPart("objectNames", objectNames.toString());

        HttpRequest httpRequest = builder.POST(publisher.build()).build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    public void deleteByImageFileIds(List<String> imageFileIds) throws Exception {
        String api = String.format("%s/api/oss/media/image/delete/id", endpoint);
        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);

        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
        publisher.addPart("imageFileIds", imageFileIds.toString());

        HttpRequest httpRequest = builder.POST(publisher.build()).build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    public ImageUrlDto getImageUrl(int channelId, String imageFileId) throws Exception {
        String api = String.format("%s/api/oss/media/image/url?channelId=%s&imageFileId=%s", endpoint, channelId, imageFileId);
        //String api = String.format("%s/api/oss/media/image/url?channelId=%s&imageFileId=%s&loginUser=%s", endpoint, channelId, imageFileId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<ImageUrlDto>>(){}.getType();
        WebResult<ImageUrlDto> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public ImageUrlDto getImageUrl(String imageFileId, long loginUser, int channelId) throws Exception {
        String api = String.format("%s/api/oss/media/image/url?channelId=%s&imageFileId=%s&loginUser=%s", endpoint, channelId, imageFileId, loginUser);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<ImageUrlDto>>(){}.getType();
        WebResult<ImageUrlDto> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public List<ImageUrlDto> getImageUrls(Set<String> imageFileIds) throws Exception {
        String api = String.format("%s/api/oss/media/image/urls?imageFileIds=%s", endpoint, imageFileIds.toString());
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<List<ImageUrlDto>>>(){}.getType();
        WebResult<List<ImageUrlDto>> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public String getSignedUrl(String url, long loginUser, int channelId) throws Exception {
        String api = String.format("%s/api/oss/media/image/signedurl?channelId=%s&url=%s&loginUser=%s", endpoint, channelId, url, loginUser);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public void deleteVideoFile(String videoFileId) throws Exception {
        String api = String.format("%s/api/oss/media/video/delete/%s", endpoint, videoFileId);
        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
        HttpRequest httpRequest = builder.DELETE().build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }
    }

    public VideoInfo getVideoInfo(String videoFileId) throws Exception {
        String api = String.format("%s/api/oss/media/video/info/%s", endpoint, videoFileId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<VideoInfo>>(){}.getType();
        WebResult<VideoInfo> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    public List<VideoUrlDto> getVideoUrls(String videoFileId, long loginUser) throws Exception {
        String api = String.format("%s/api/oss/media/video/url?videoFileId=%s&userId=%s", endpoint, videoFileId, loginUser);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<List<VideoUrlDto>>>(){}.getType();
        WebResult<List<VideoUrlDto>> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }

    @Deprecated
    public LocalDateTime getCreateTime(String videoFileId) throws Exception {
        String api = String.format("%s/api/oss/media/video/time/%s", endpoint, videoFileId);
        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                .header("authorization", "Bearer " + token)
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
        if (statusCode != 200) {
            String errMsg = String.format("%s -> %s", statusCode, body);
            throw new Exception(errMsg);
        }

        Type type = new TypeToken<WebResult<LocalDateTime>>(){}.getType();
        WebResult<LocalDateTime> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
            throw new Exception(errMsg);
        }

        return webResult.getData();
    }
}
