package com.ai.da.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.enums.SingleOverallEnum;
import com.ai.da.common.utils.CopyUtil;
import com.ai.da.common.utils.DateUtil;
import com.ai.da.mapper.primary.LibraryModelPointMapper;
import com.ai.da.mapper.primary.entity.*;
import com.ai.da.model.dto.LibraryModelPointDTO;
import com.ai.da.model.dto.ModelsDotDTO;
import com.ai.da.model.enums.ModelType;
import com.ai.da.model.enums.Sex;
import com.ai.da.model.vo.LibraryModelPointVO;
import com.ai.da.python.PythonService;
import com.ai.da.python.vo.DesignPythonObjects;
import com.ai.da.service.DesignService;
import com.ai.da.service.LibraryModelPointService;
import com.ai.da.service.LibraryService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 服务实现类
 *
 * @author yanglei
 * @since 2022-11-30
 */
@Slf4j
@Service
public class LibraryModelPointServiceImpl extends ServiceImpl<LibraryModelPointMapper, LibraryModelPoint> implements LibraryModelPointService {
    @Resource
    private LibraryModelPointMapper libraryModelPointMapper;
    @Resource
    private LibraryService libraryService;
    @Resource
    private PythonService pythonService;
    @Resource
    private DesignService designService;

    @Override
    public LibraryModelPointVO saveOrEditTemplatePoint(LibraryModelPointDTO libraryModelPointDTO) {
//        Library library = libraryService.getById(libraryModelPointDTO.getLibraryId());
//        Assert.notNull(library,"library does not exist!");
        LibraryModelPointVO libraryModelPointVO = CopyUtil.copyObject(libraryModelPointDTO, LibraryModelPointVO.class);
        if (Objects.isNull(libraryModelPointDTO.getTemplateId())) {
            //新增
            LibraryModelPoint libraryModelPoint = resolvePoint(libraryModelPointDTO);
            libraryModelPoint.setCreateDate(DateUtil.getByTimeZone(libraryModelPointDTO.getTimeZone()));
            save(libraryModelPoint);
            libraryModelPointVO.setTemplateId(libraryModelPoint.getId());
        } else {
            //编辑
            if (!StringUtils.isEmpty(libraryModelPointDTO.getModelSex())) {
                Library byId = libraryService.getById(libraryModelPointDTO.getLibraryId());
                if (!byId.getLevel2Type().equals(libraryModelPointDTO.getModelSex())) {
                    if (byId.getLevel2Type().equals(Sex.FEMALE.getValue())) {
                        libraryService.checkModel(Sex.FEMALE.getValue(), Collections.singletonList(byId.getId()), 1);
                    }else {
                        libraryService.checkModel(Sex.MALE.getValue(), Collections.singletonList(byId.getId()), 1);
                    }
                    byId.setLevel2Type(libraryModelPointDTO.getModelSex());
                    libraryService.updateById(byId);
                }
            }
            LibraryModelPoint modelPoint = getById(libraryModelPointDTO.getTemplateId());
            if (Objects.isNull(modelPoint)) {
                throw new BusinessException("modelPoint.not.found");
            }
            modelPoint = resolvePoint(libraryModelPointDTO);
            modelPoint.setUpdateDate(DateUtil.getByTimeZone(libraryModelPointDTO.getTimeZone()));
            modelPoint.setId(libraryModelPointDTO.getTemplateId());
            updateById(modelPoint);
            libraryModelPointVO.setTemplateId(modelPoint.getId());
        }
        return libraryModelPointVO;
    }

    private LibraryModelPoint resolvePoint(LibraryModelPointDTO libraryModelPointDTO) {
        LibraryModelPoint libraryModelPoint = CopyUtil.copyObject(libraryModelPointDTO, LibraryModelPoint.class);
        libraryModelPoint.setRelationId(libraryModelPointDTO.getLibraryId());
        libraryModelPoint.setHandLeft(JSON.toJSONString(libraryModelPointDTO.getHandLeft()));
        libraryModelPoint.setHandRight(JSON.toJSONString(libraryModelPointDTO.getHandRight()));
        libraryModelPoint.setWaistbandLeft(JSON.toJSONString(libraryModelPointDTO.getWaistbandLeft()));
        libraryModelPoint.setWaistbandRight(JSON.toJSONString(libraryModelPointDTO.getWaistbandRight()));
        libraryModelPoint.setShoulderLeft(JSON.toJSONString(libraryModelPointDTO.getShoulderLeft()));
        libraryModelPoint.setShoulderRight(JSON.toJSONString(libraryModelPointDTO.getShoulderRight()));
        return libraryModelPoint;
    }


    @Override
    public List<LibraryModelPointVO> selectByLibraryIds(List<Long> libraryIds) {
        QueryWrapper<LibraryModelPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LibraryModelPoint::getModelType, ModelType.LIBRARY.getValue());
        queryWrapper.in("relation_id", libraryIds);
        List<LibraryModelPoint> libraryModelPoints = libraryModelPointMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(libraryModelPoints)) {
            return null;
        }
        return CopyUtil.copyList(libraryModelPoints, LibraryModelPointVO.class, (o, d) -> {
            d.setTemplateId(o.getId());
            d.setHandLeft(JSON.parseObject(o.getHandLeft(), List.class));
            d.setHandRight(JSON.parseObject(o.getHandRight(), List.class));
            d.setShoulderRight(JSON.parseObject(o.getShoulderRight(), List.class));
            d.setShoulderLeft(JSON.parseObject(o.getShoulderLeft(), List.class));
            d.setWaistbandRight(JSON.parseObject(o.getWaistbandRight(), List.class));
            d.setWaistbandLeft(JSON.parseObject(o.getWaistbandLeft(), List.class));
        });
    }

    @Override
    public String modelsDot(ModelsDotDTO modelsDotDTO) {
        DesignPythonObjects objects = pythonService.covertModelsDotParam(
                modelsDotDTO, SingleOverallEnum.OVERALL.getRealName(), "");
        designService.relationImageId(objects);
        JSONObject jsonObject = pythonService.designNew(objects);
        JSONObject data = jsonObject.getJSONObject("data");
        if (data == null) {
            throw new BusinessException("design.interface.exception");
        }
        JSONObject jsonObject1 = data.getJSONObject("0");
        if (jsonObject1 == null) {
            throw new BusinessException("design.interface.exception");
        }
        String synthesisUrl = jsonObject1.getString("synthesis_url");
        if (StringUtils.isEmpty(synthesisUrl)) {
            throw new BusinessException("design.interface.exception");
        }
        return synthesisUrl;
    }

    @Override
    public LibraryModelPoint getByRelationId(Long relationId, String modelType) {
        QueryWrapper<LibraryModelPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LibraryModelPoint::getRelationId, relationId);
        queryWrapper.lambda().eq(LibraryModelPoint::getModelType, modelType);
        queryWrapper.lambda().last("limit 1");
        List<LibraryModelPoint> libraryModelPoints = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(libraryModelPoints)) {
            throw new BusinessException("modelPoint.not.found");
        }
        return libraryModelPoints.get(0);
    }

}
