package com.backendsys.controller.Ali;

import com.backendsys.entity.Ali.AliDashscopeBackgroundDTO;
import com.backendsys.entity.Ali.AliDashscopeBackgroundInputDTO;
import com.backendsys.entity.Ali.AliDashscopeBackgroundParametersDTO;
import com.backendsys.entity.Ali.AliDashscopeTaskDTO;
import com.backendsys.service.Ali.AliDashscopeService;
import com.backendsys.service.Ali.AliDashscopeTaskService;
import com.backendsys.utils.ResourceUtil;
import com.backendsys.utils.response.Result;
import com.backendsys.utils.response.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Validated
@RestController
@RequestMapping("/api/ali/dashscope")
public class AliDashscopeController {

    @Autowired
    private AliDashscopeTaskService aliDashscopeTaskService;

    @Autowired
    private AliDashscopeService aliDashscopeService;

    /**
     * 通义万相 - 查询任务生成结果
     * @param task_id
     */
    @PreAuthorize("@sr.hasPermission('32')")
    @GetMapping("getTask/{task_id}")
    public Result aliDashscopeGetTask(@PathVariable("task_id") String task_id) {

        // 判断是否存在
        AliDashscopeTaskDTO aliDashscopeTaskDTO = new AliDashscopeTaskDTO();
        aliDashscopeTaskDTO.setTask_id(task_id);
        Map<String, Object> response = aliDashscopeTaskService.getTaskDetail(aliDashscopeTaskDTO);
        if (response == null) {
            return Result.error(ResultEnum.NOT_FOUND.getCode(), ResultEnum.NOT_FOUND.getMessage());
        }

        return Result.success(aliDashscopeService.getTaskById(task_id));
    }

    /**
     * 阿里 - 通义万相 - 图像背景生成API (https://help.aliyun.com/zh/dashscope/developer-reference/tongyi-wanxiang-api-for-generating-backgrounds?spm=a2c4g.11186623.0.0.2a3866efOS1LTz)
     * @param aliDashscopeBackgroundDTO
     */
    @PreAuthorize("@sr.hasPermission('32')")
    @PostMapping("backgroundGeneration")
    public Result aliDashscopeBackgroundGeneration(@Validated @RequestBody AliDashscopeBackgroundDTO aliDashscopeBackgroundDTO){

        AliDashscopeBackgroundInputDTO input = aliDashscopeBackgroundDTO.getInput();
        AliDashscopeBackgroundParametersDTO parameters = aliDashscopeBackgroundDTO.getParameters();

        // 如果图片有值，则判断图片URL是否有效
        String base_image_url = input.getBase_image_url();
        String ref_image_url = input.getRef_image_url();
        if (base_image_url != null && base_image_url != "") {
            ResourceUtil.checkImageValid(base_image_url);
        }
        if (ref_image_url != null && ref_image_url != "") {
            ResourceUtil.checkImageValid(ref_image_url);
        }

        // input.sub_title: 仅当 input.title 不为空时生效
        if (input != null) {
            String input_title = input.getTitle();
            String input_sub_title = input.getSub_title();

            if ((input_sub_title != null && input_sub_title != "") && (input_title == null || input_title != "")) {
                return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), "副标题 (sub_title) 存在时，主标题 (title) 不能为空");
            }
        }
        //

        // parameters.noise_level: 仅当 input.ref_image_url 不为空时生效
        if (parameters != null) {
            Integer parameters_noise_level = parameters.getNoise_level();
            String input_ref_image_url = input.getRef_image_url();
            if (parameters_noise_level != null && (input_ref_image_url == null || input_ref_image_url.isEmpty())) {
                return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), "相似度阙值 (noise_level) 存在时，引导图URL (ref_image_url) 不能为空");
            }
        }

        return Result.success(aliDashscopeService.createBackgroundGenerationTask(aliDashscopeBackgroundDTO));
    }

}
