package com.ai.da.service.impl;

import com.ai.da.mapper.primary.ToProductImageResultMapper;
import com.ai.da.mapper.primary.entity.CollectionSort;
import com.ai.da.mapper.primary.entity.ToProductImageResult;
import com.ai.da.mapper.primary.entity.UserLike;
import com.ai.da.service.CollectionSortService;
import com.ai.da.service.ToProductImageResultService;
import com.ai.da.service.UserLikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class ToProductImageResultServiceImpl extends ServiceImpl<ToProductImageResultMapper, ToProductImageResult> implements ToProductImageResultService {

    private final CollectionSortService collectionSortService;

    private final UserLikeService userLikeService;

    // 获取未被like的to product\relight的parentId
    public Long getUnlikedResultParentId(ToProductImageResult toProductImageResult, String url) {
        // 卫语句处理null情况
        if (toProductImageResult == null && StringUtil.isNullOrEmpty(url)) {
            return null;
        }

        // 如果需要查询结果对象
        if (toProductImageResult == null) {
            toProductImageResult = baseMapper.selectOne(
                    new QueryWrapper<ToProductImageResult>().eq("url", url));
        }

        // 卫语句处理查询结果为空的情况
        if (toProductImageResult == null) {
            return null;
        }

        // 根据不同类型处理
        String elementType = toProductImageResult.getElementType();
        if ("DesignOutfit".equals(elementType)) {
            return handleDesignOutfitCase(toProductImageResult);
        } else if ("ToProductImage".equals(elementType)) {
            // 两种情况 resultType : ToProductImage | Relight
            return handleToProductImageCase(toProductImageResult);
        }

        return null;
    }

    private Long handleDesignOutfitCase(ToProductImageResult result) {
        UserLike userLike = userLikeService.getByDesignOutfitId(result.getElementId());
        if (userLike == null) {
            return null;
        }

        CollectionSort collectionSort = collectionSortService.queryCollectionSortByRelation(
                userLike.getId(), "Design", result.getProjectId());
        return collectionSort != null ? collectionSort.getId() : null;
    }

    private Long handleToProductImageCase(ToProductImageResult result) {
        CollectionSort collectionSort = collectionSortService.queryCollectionSortByRelation(
                result.getElementId(), result.getResultType(), result.getProjectId());
        if (Objects.isNull(collectionSort) && result.getElementType().equals("ToProductImage")) {
            ToProductImageResult toProductImageResult = baseMapper.selectById(result.getElementId());
            return getUnlikedResultParentId(toProductImageResult, null);
        }
        return collectionSort != null ? collectionSort.getParentId() : null;
    }
}
