package com.ai.da.service.impl;

import com.ai.da.common.enums.LayersPriorityEnum;
import com.ai.da.common.utils.CopyUtil;
import com.ai.da.common.utils.MinioUtil;
import com.ai.da.mapper.primary.TDesignPythonOutfitDetailMapper;
import com.ai.da.mapper.primary.entity.TDesignPythonOutfitDetail;
import com.ai.da.model.vo.DesignPythonOutfitVO;
import com.ai.da.model.vo.TDesignPythonOutfitDetailVO;
import com.ai.da.service.ITDesignPythonOutfitDetailService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.ai.da.common.enums.LayersPriorityEnum.BODY;

/**
 * design item详情表 服务实现类
 *
 * @author SHAHAIBO
 * @since 2023-09-04
 */
@Service
public class TDesignPythonOutfitDetailServiceImpl extends ServiceImpl<TDesignPythonOutfitDetailMapper, TDesignPythonOutfitDetail> implements ITDesignPythonOutfitDetailService {

    @Resource
    private MinioUtil minioUtil;

    @Override
    public IPage<TDesignPythonOutfitDetailVO> selectTDesignPythonOutfitDetailPage(IPage<TDesignPythonOutfitDetailVO> page, TDesignPythonOutfitDetailVO tDesignPythonOutfitDetail) {
        return page.setRecords(baseMapper.selectTDesignPythonOutfitDetailPage(page, tDesignPythonOutfitDetail));
    }

    @Override
    public List<TDesignPythonOutfitDetail> getDetailByDesignPythonOutfitId(Long designPythonOutfitId) {
        QueryWrapper<TDesignPythonOutfitDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("design_python_outfit_id", designPythonOutfitId);
        queryWrapper.eq("is_deleted", 0);

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public DesignPythonOutfitVO convertToDesignPythonOutfitVO(TDesignPythonOutfitDetail detail, List<Long> offset) {
        if (Objects.isNull(detail)) {
            return null;
        }

        DesignPythonOutfitVO designPythonOutfitVO = CopyUtil.copyObject(detail, DesignPythonOutfitVO.class);
        designPythonOutfitVO.setPosition(StringUtil.isNullOrEmpty(detail.getPosition()) ? null : (List<Long>) JSON.parse(detail.getPosition()));
        designPythonOutfitVO.setImageSize(StringUtil.isNullOrEmpty(detail.getImageSize()) ? null : (List<Long>) JSON.parse(detail.getImageSize()));
        designPythonOutfitVO.setImageUrl(StringUtil.isNullOrEmpty(detail.getImageUrl()) ? null : minioUtil.getPreSignedUrl(detail.getImageUrl(), 24 * 60));
        designPythonOutfitVO.setImageMinioUrl(StringUtil.isNullOrEmpty(detail.getImageUrl()) ? null : detail.getImageUrl());
        designPythonOutfitVO.setMaskUrl(StringUtil.isNullOrEmpty(detail.getMaskUrl()) ? null : minioUtil.getPreSignedUrl(detail.getMaskUrl(), 24 * 60));
        designPythonOutfitVO.setMaskMinioUrl(StringUtil.isNullOrEmpty(detail.getMaskUrl()) ? null : detail.getMaskUrl());
//        designPythonOutfitVO.setScale(Float.parseFloat(detail.getScale()));
        designPythonOutfitVO.setScale(modifyScale(detail.getScale()));
        designPythonOutfitVO.setOffset(StringUtil.isNullOrEmpty(detail.getOffset()) ? Arrays.asList(0L, 0L) : (List<Long>) JSON.parse(detail.getOffset()));
        designPythonOutfitVO.setPriority(Math.abs(detail.getPriority()));
//        designPythonOutfitVO.setOffset(CollectionUtil.isEmpty(offset) ? Arrays.asList(0L, 0L) : offset);

		/*if (!StringUtil.isNullOrEmpty(detail.getImageSize())){
			List<Long> size = Arrays.stream(detail.getImageSize().replaceAll("\\[|\\]", "").split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
			ArrayList<Long> imageSize = new ArrayList<>();
            for (Long aLong : size) {
                imageSize.add((long) (aLong * Float.parseFloat(detail.getScale())));
            }
			designPythonOutfitVO.setImageSize(imageSize);
		}*/
        return designPythonOutfitVO;
    }

    private Float[] modifyScale(String scale){
        Float[] scaleFloat = new Float[2];
        if (!StringUtil.isNullOrEmpty(scale)){
            if (scale.startsWith("[")){
                // 去除中括号并去掉多余的空格
                scale = scale.trim().replaceAll("^\\[|\\]$", "");

                // 根据逗号和可选的空格拆分字符串
                String[] parts = scale.split("\\s*,\\s*");
//                Float[] floatArray = new Float[parts.length];

                // 转换每个字符串为 Float 并添加到数组中
                for (int i = 0; i < parts.length; i++) {
                    try {
                        scaleFloat[i] = Float.parseFloat(parts[i]);
                    } catch (NumberFormatException e) {
                        System.err.println("Invalid number format: " + parts[i]);
                        return new Float[0]; // 返回一个空数组或者其他错误处理逻辑
                    }
                }
            }else {
                scaleFloat = new Float[]{Float.parseFloat(scale),Float.parseFloat(scale)};
            }
        }
        return scaleFloat;
    }

    @Override
    public void deleteByDesignPythonOutfitIdLogical(Long designPythonOutfitId) {
        UpdateWrapper<TDesignPythonOutfitDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("design_python_outfit_id", designPythonOutfitId);
        updateWrapper.set("is_deleted", (byte) 1);
        updateWrapper.set("update_date", new Date());

        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteByDesignPythonOutfitIdPhysical(Long designPythonOutfitId) {
        // 执行物理删除
        baseMapper.deleteByDesignPythonOutfitIdPhysical(designPythonOutfitId);
    }


    @Override
    public void setDesignPythonOutfitDetailPriority(List<TDesignPythonOutfitDetail> details){
        for (TDesignPythonOutfitDetail detail:details){
            if (!detail.getImageCategory().equals(BODY.getRealName()) && detail.getPriority().equals(BODY.getValue())){
                detail.setPriority(LayersPriorityEnum.getValueByLayerCategory(detail.getImageCategory()).getValue());
            }
        }
    }

}
