package com.ycwl.basic.controller.vpt;

import com.alibaba.fastjson.JSONObject;
import com.ycwl.basic.annotation.IgnoreLogReq;
import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.constant.StorageConstant;
import com.ycwl.basic.device.entity.common.FileObject;
import com.ycwl.basic.device.operator.VptPassiveStorageOperator;
import com.ycwl.basic.device.operator.VptPassiveStorageOperator;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
import com.ycwl.basic.model.wvp.WvpSyncReqVo;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.storage.StorageFactory;
import com.ycwl.basic.storage.adapters.IStorageAdapter;
import com.ycwl.basic.storage.enums.StorageAcl;
import com.ycwl.basic.storage.utils.StorageUtil;
import com.ycwl.basic.utils.ApiResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@IgnoreToken
@RestController
@Api(tags = "自定义切片工具对接接口")
@RequestMapping("/vpt/v1/")
public class VptController {

    private final ScenicRepository scenicRepository;

    public VptController(ScenicRepository scenicRepository) {
        this.scenicRepository = scenicRepository;
    }

    @IgnoreLogReq
    @PostMapping("/scenic/{scenicId}/sync")
    public ApiResponse<List<VptPassiveStorageOperator.Task>> sync(@PathVariable("scenicId") Long scenicId, @RequestBody WvpSyncReqVo reqVo) {
        return ApiResponse.success(VptPassiveStorageOperator.getTaskListByScenicIdAndDeviceList(scenicId, reqVo.getDevices()));
    }
    @PostMapping("/scenic/{scenicId}/{taskId}/uploadUrl")
    public String uploadUrl(@PathVariable("scenicId") Long scenicId, @PathVariable("taskId") Long taskId) {
        IStorageAdapter adapter;
        ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenicId);
        if (scenicConfig != null && scenicConfig.getStoreType() != null) {
            adapter = StorageFactory.get(scenicConfig.getStoreType());
            adapter.loadConfig(JSONObject.parseObject(scenicConfig.getStoreConfigJson(), Map.class));
        } else {
            adapter = StorageFactory.use("video");
        }
        String filename = StorageUtil.joinPath(StorageConstant.VIDEO_PIECE_PATH, taskId.toString() + ".mp4");
        String urlForUpload = adapter.getUrlForUpload(new Date(System.currentTimeMillis() + 1000 * 60 * 60), "video/mp4", filename);
        urlForUpload = urlForUpload.replace("-internal.aliyuncs.com", ".aliyuncs.com");
        return urlForUpload;
    }
    @PostMapping("/scenic/{scenicId}/{taskId}/success")
    public ApiResponse<String> success(@PathVariable("scenicId") Long scenicId, @PathVariable("taskId") Long taskId, @RequestBody FileObject fileObject) {
        IStorageAdapter adapter;
        ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenicId);
        if (scenicConfig != null && scenicConfig.getStoreType() != null) {
            adapter = StorageFactory.get(scenicConfig.getStoreType());
            adapter.loadConfig(JSONObject.parseObject(scenicConfig.getStoreConfigJson(), Map.class));
        } else {
            adapter = StorageFactory.use("video");
        }
        String filename = StorageUtil.joinPath(StorageConstant.VIDEO_PIECE_PATH, taskId.toString() + ".mp4");
        fileObject.setUrl(adapter.getUrl(filename));
        adapter.setAcl(StorageAcl.PUBLIC_READ, filename);
        VptPassiveStorageOperator.onReceiveResult(taskId, fileObject);
        return ApiResponse.success("success");
    }
    @PostMapping("/scenic/{scenicId}/{taskId}/failure")
    public ApiResponse<String> fail(@PathVariable("scenicId") Long scenicId, @PathVariable("taskId") Long taskId) {
        VptPassiveStorageOperator.onReceiveResult(taskId, null);
        return ApiResponse.success("success");
    }
}
