package net.northking.cctp.device.pub;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.device.api.engineInfo.service.CdEngineInfoApiService;
import net.northking.cctp.device.api.engineReleases.CdEngineReleasesQueryCtrl;
import net.northking.cctp.device.api.engineReleases.service.CdEngineReleasesApiService;
import net.northking.cctp.device.db.entity.CdEngineInfo;
import net.northking.cctp.device.db.entity.CdEngineReleases;
import net.northking.cctp.device.dto.engineInfo.DeviceListDto;
import net.northking.cctp.device.dto.engineInfo.EngineInfoListDto;
import net.northking.cctp.device.dto.engineReleases.ScriptEngineVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "执行引擎版本（pub）")
@RequestMapping("/pub")
@RestController
public class CdEngineReleasesPubCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(CdEngineReleasesQueryCtrl.class);

    @Autowired
    private CdEngineReleasesApiService apiService;


    @Autowired
    private CdEngineInfoApiService cdEngineInfoApiService;



    @ApiOperation(value = "获取最新版本")
    @GetMapping(
            value = "/engine/release/latest",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ScriptEngineVersion getLatestRelease(){
        CdEngineReleases release = this.apiService.getLatestRelease();
        if (ObjectUtils.isEmpty(release)){
            return null;
        }
        ScriptEngineVersion dto = new ScriptEngineVersion();
        dto.setVersion(release.getReleaseVersion());
        dto.setDownloadUrl(release.getResourceId());
        return dto;
    }

    @ApiOperation(value = "根据设备类型申请引擎")
    @PostMapping(value = "/query/engineByType",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<EngineInfoListDto> queryEngineByType(@RequestBody List<DeviceListDto> deviceListDto){
        return this.cdEngineInfoApiService.queryEngineByType(deviceListDto);
    }

    @ApiOperation(value = "获取一个可用执行引擎")
    @GetMapping(
            value = "/find/engine/{proId}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<CdEngineInfo> getEngineInfo(@PathVariable("proId") String proId){
        ResultWrapper<CdEngineInfo> wrapper = new ResultWrapper<>();
        CdEngineInfo engineInfo = this.cdEngineInfoApiService.getEngineInfo(proId);
        return wrapper.success(engineInfo);
    }

}
