package com.gic.haoban.manage.web.controller.content;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.template.ContentTemplateDTO;
import com.gic.content.api.dto.template.ContentTemplateTypeDTO;
import com.gic.content.api.qdto.template.CmeExportVideoQDTO;
import com.gic.content.api.qdto.template.ContentExportVideoQDTO;
import com.gic.content.api.qdto.template.ContentTemplateQDTO;
import com.gic.content.api.service.CmeFileApiService;
import com.gic.content.api.service.ContentTemplateApiService;
import com.gic.enterprise.api.dto.EnterpriseDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.manage.web.qo.content.ai.CmeExportVideoQO;
import com.gic.haoban.manage.web.qo.content.ai.CmeImportMaterialQO;
import com.gic.haoban.manage.web.qo.content.ai.ContentTemplateQO;
import com.gic.haoban.manage.web.vo.content.template.ContentTemplateDetailVO;
import com.gic.haoban.manage.web.vo.content.template.ContentTemplateVO;
import com.gic.thirdparty.cloudfile.CmeUtil;
import com.gic.thirdparty.cloudfile.pojo.cme.CmeImportMaterial;
import com.gic.thirdparty.cloudfile.pojo.cme.CmeTaskProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 好办-AI创作
 *
 */
@Controller
@RequestMapping("/ai")
public class ContentAIController {


    private static final Logger log = LoggerFactory.getLogger(ContentAIController.class);

    @Autowired
    private CmeFileApiService cmeFileApiService;
    @Autowired
    private ContentTemplateApiService templateApiService;
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 查询模板类型列表
     *
     * @param type 类型 1类别2风格 null查全部
     * @return RestResponse
     */
    @RequestMapping("list-template-type")
    @ResponseBody
    public RestResponse<List<ContentTemplateTypeDTO>> listTemplateType(@RequestParam(required = false)Integer type) {
        ServiceResponse<List<ContentTemplateTypeDTO>> response = templateApiService.listTemplateType(type, true);
        if (response.isSuccess()) {
            return RestResponse.successResult(response.getResult());
        }
        return RestResponse.failure(response.getCode(), response.getMessage());
    }


    /**
     * 模板分页查询
     *
     * @param search search
     * @return RestResponse
     */
    @RequestMapping("page-template")
    @ResponseBody
    public RestResponse<Page<ContentTemplateVO>> queryByPage(@RequestBody ContentTemplateQO search) {
        ContentTemplateQDTO templateQDTO = com.gic.commons.util.EntityUtil.changeEntityNew(ContentTemplateQDTO.class, search);
        BasePageInfo basePageInfo = search.getBasePageInfo();
        ServiceResponse<Page<ContentTemplateDTO>> response = templateApiService.pageTemplate(templateQDTO,basePageInfo);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        Page<ContentTemplateDTO> result = response.getResult();
        Page<ContentTemplateVO> ret = PageHelperUtils.changePageToCurrentPage(result, ContentTemplateVO.class);
        return RestResponse.successResult(ret);
    }

    /**
     * 模板详情查询
     *
     * @param contentTemplateId 主键ID
     * @return RestResponse
     */
    @RequestMapping("detail-template")
    @ResponseBody
    public RestResponse<ContentTemplateDetailVO> detailTemplate(Long contentTemplateId) {
        ServiceResponse<ContentTemplateDTO> response = templateApiService.getTemplate(contentTemplateId);
        if (response.isSuccess()) {
            ContentTemplateDetailVO detailVO = com.gic.commons.util.EntityUtil.changeEntityByJSON(ContentTemplateDetailVO.class, response.getResult());
            return RestResponse.successResult(detailVO);
        }
        return RestResponse.failure(response.getCode(), response.getMessage());
    }

    /**
     * 导入cme媒体
     * @param materialQO
     * @return materialId 素材id
     */
    @RequestMapping("import-material")
    @ResponseBody
    public RestResponse<String> importMaterial(@RequestBody CmeImportMaterialQO materialQO){
        CmeImportMaterial cmeImportMaterial = EntityUtil.changeEntityNew(CmeImportMaterial.class, materialQO);
        try {
            String materialId = CmeUtil.importMaterial(cmeImportMaterial);
            return RestResponse.successResult(materialId);
        } catch (Exception e) {
            log.warn("导入cme媒体:", e);
            return RestResponse.failure("-1", "导入媒体异常");
        }
    }

    /**
     * 剪辑视频导出
     * @param cmeExportVideo
     * @return taskId 任务id
     */
    @RequestMapping("export-video")
    @ResponseBody
    public RestResponse<String> exportVideo(@RequestBody CmeExportVideoQO cmeExportVideo){
        String enterpriseId = cmeExportVideo.getEnterpriseId();
        EnterpriseDTO enterpriseDTO = enterpriseService.getEnterpriseById(enterpriseId);
        String factoryCode = enterpriseDTO.getFactoryCode();
        ContentExportVideoQDTO exportVideoQDTO = new ContentExportVideoQDTO();
        exportVideoQDTO.setEnterpriseId(cmeExportVideo.getEnterpriseId());
        exportVideoQDTO.setContentTemplateId(cmeExportVideo.getContentTemplateId());
        exportVideoQDTO.setStoreId(cmeExportVideo.getStoreId());
        exportVideoQDTO.setStoreCode(cmeExportVideo.getStoreCode());
        exportVideoQDTO.setStoreName(cmeExportVideo.getStoreName());
        exportVideoQDTO.setUserId(cmeExportVideo.getClerkId());
        exportVideoQDTO.setUserCode(cmeExportVideo.getClerkCode());
        exportVideoQDTO.setUserName(cmeExportVideo.getClerkName());
        CmeExportVideoQDTO cmeExportVideoQDTO = EntityUtil.changeEntityByJSON(CmeExportVideoQDTO.class, cmeExportVideo);
        cmeExportVideoQDTO.setFactoryCode(factoryCode);
        try {
            ServiceResponse<String> response = cmeFileApiService.cmeExportVideo(exportVideoQDTO, cmeExportVideoQDTO);
            if (!response.isSuccess()) {
                return RestResponse.failure(response.getCode(), response.getMessage());
            }
            return RestResponse.successResult(response.getResult());
        } catch (Exception e) {
            log.warn("剪辑视频导出:", e);
            return RestResponse.failure("-1", "剪辑视频导出异常");
        }
    }

    /**
     * 获取视频导出任务进度
     * @param taskId    任务id
     * @return
     */
    @RequestMapping("get-export-task")
    @ResponseBody
    public RestResponse<CmeTaskProcess> getExportTask(String taskId){
        CmeTaskProcess cmeTaskProcess = CmeUtil.describeTaskProcess(taskId);
        return RestResponse.successResult(cmeTaskProcess);
    }

}
