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.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);

        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.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;
    }

    @Override
    public void deleteByDesignPythonOutfitId(Long designPythonOutfitId) {
//		QueryWrapper<TDesignPythonOutfitDetail> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("design_python_outfit_id",designPythonOutfitId);
//		TDesignPythonOutfitDetail tDesignPythonOutfitDetail = new TDesignPythonOutfitDetail();
//		tDesignPythonOutfitDetail.setIsDeleted(1);
        UpdateWrapper<TDesignPythonOutfitDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("design_python_outfit_id", designPythonOutfitId);
        updateWrapper.set("is_deleted", (byte) 1);

        baseMapper.update(null, updateWrapper);
    }


    @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());
            }
        }
    }

}
