package com.fs.course.controller;

import com.fs.common.core.domain.R;
import com.fs.course.param.HuaweiCloudStatisticsParam;
import com.fs.course.service.IHuaweiCloudStatisticsService;
import com.fs.course.service.IHuaweiObsService;
import com.fs.course.service.IHuaweiVodService;
import com.huaweicloud.sdk.vod.v1.model.BaseInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static com.fs.course.service.impl.HuaweiObsServiceImpl.fileUrlMap;
import static com.fs.course.service.impl.HuaweiObsServiceImpl.uploadProgress;

@RestController
@RequestMapping("/hwcloud")
public class HuaweiCloudController {
    private static final Logger log = LoggerFactory.getLogger(HuaweiCloudController.class);

    @Autowired
    private IHuaweiVodService huaweiVodService;
    @Autowired
    private IHuaweiObsService huaweiObsService;
    @PostMapping("/uploadHuaWeiVod")
    public R uploadHuaWeiVod(@RequestParam("file") MultipartFile file) throws IOException
    {
        // 将MultipartFile转换为File对象
        File convFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
        file.transferTo(convFile);

        try {
            // 调用服务类上传视频并获取视频URL
            String assetId = huaweiVodService.uploadAndMergeVideo(convFile);
            BaseInfo data = huaweiVodService.getBaseInfo(assetId);
            return R.ok().put("data",data).put("assetId",assetId);
        } catch (Exception e) {
            return R.error("上传失败：" + e.getMessage());
        }
    }


    @PostMapping("/uploadHuaWeiObs")
    public R uploadHuaWeiObs(@RequestParam("file") MultipartFile file) throws IOException
    {
        // 将MultipartFile转换为File对象
        File convFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
        file.transferTo(convFile);
        String fileKey = UUID.randomUUID().toString(); // 用于标识上传进度
        uploadProgress.put(fileKey, 0.0);
        new Thread(() -> {
            try {
                huaweiObsService.uploadPartFile(convFile, fileKey);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        return R.ok().put("data", fileKey);
    }

    @GetMapping("/upload/progress")
    public R getUploadProgress(@RequestParam("fileKey") String fileKey) {
        Double progress = uploadProgress.get(fileKey);
        if (progress != null) {
            return R.ok().put("progress", progress);
        } else {
            return R.error("上传进度未找到");
        }
    }

    @GetMapping("/upload/videoUrl")
    public R getVideoUrl(@RequestParam("fileKey") String fileKey) {
        String videoUrl = getFileUrl(fileKey);
        if (videoUrl != null) {
            return R.ok().put("url", videoUrl);
        } else {
            return R.error("视频URL未找到");
        }
    }
    // 获取视频URL的方法
    public String getFileUrl(String fileKey) {
        return fileUrlMap.get(fileKey);
    }

    @Autowired
    private IHuaweiCloudStatisticsService huaweiCloudStatisticsService;

    @GetMapping("/cdnStatistics")
    public R cdnStatistics(HuaweiCloudStatisticsParam param) {
        return huaweiCloudStatisticsService.DomainStatistics(param);
    }
}
