package com.ycwl.basic.controller.wvp;

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.WvpPassiveStorageOperator;
import com.ycwl.basic.model.wvp.WvpSyncReqVo;
import com.ycwl.basic.service.pc.DeviceService;
import com.ycwl.basic.service.pc.ScenicService;
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.springframework.beans.factory.annotation.Autowired;
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;

@Slf4j
@IgnoreToken
@RestController
@Api(tags = "WVP对接接口")
@RequestMapping("/wvp/v1/")
public class WvpController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ScenicService scenicService;

    @IgnoreLogReq
    @PostMapping("/scenic/{scenicId}/sync")
    public ApiResponse<List<WvpPassiveStorageOperator.Task>> sync(@PathVariable("scenicId") Long scenicId, @RequestBody WvpSyncReqVo reqVo) {
        deviceService.updateDevices(scenicId, reqVo);
        return ApiResponse.success(WvpPassiveStorageOperator.getTaskListByScenicId(scenicId));
    }

    @PostMapping("/scenic/{scenicId}/{taskId}/uploadUrl")
    public String uploadUrl(@PathVariable("scenicId") Long scenicId, @PathVariable("taskId") Long taskId) {
        IStorageAdapter adapter = scenicService.getScenicLocalStorageAdapter(scenicId);
        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 = scenicService.getScenicLocalStorageAdapter(scenicId);
        String filename = StorageUtil.joinPath(StorageConstant.VIDEO_PIECE_PATH, taskId.toString() + ".mp4");
        fileObject.setUrl(adapter.getUrl(filename));
        adapter.setAcl(StorageAcl.PUBLIC_READ, filename);
        WvpPassiveStorageOperator.onReceiveResult(taskId, fileObject);
        return ApiResponse.success("success");
    }
    @PostMapping("/scenic/{scenicId}/{taskId}/failure")
    public ApiResponse<String> fail(@PathVariable("scenicId") Long scenicId, @PathVariable("taskId") Long taskId) {
        WvpPassiveStorageOperator.onReceiveResult(taskId, null);
        return ApiResponse.success("success");
    }
}
