package cn.reghao.tnb.content.app.geo.service;

import cn.reghao.file.api.iface.OssService;
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.content.api.dto.geo.MallReplyDto;
import cn.reghao.tnb.content.api.dto.geo.MallReplyPhotoDto;
import cn.reghao.tnb.content.app.geo.db.mapper.CameraPhotoMapper;
import cn.reghao.tnb.content.app.geo.db.mapper.MallReplyMapper;
import cn.reghao.tnb.content.app.geo.model.constant.GeoItem;
import cn.reghao.tnb.content.app.geo.model.po.CameraPhoto;
import cn.reghao.tnb.content.app.geo.model.po.MallReply;
import cn.reghao.tnb.content.app.geo.model.vo.MapMarker;
import cn.reghao.tnb.content.app.geo.model.vo.MarkerInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2023-08-31 17:49:48
 */
@Slf4j
@Service
public class PhotoMapService {
    @DubboReference(check = false, retries = 0)
    private OssService ossService;

    private final MallReplyMapper mallReplyMapper;
    private final CameraPhotoMapper cameraPhotoMapper;

    public PhotoMapService(MallReplyMapper mallReplyMapper, CameraPhotoMapper cameraPhotoMapper) {
        this.mallReplyMapper = mallReplyMapper;
        this.cameraPhotoMapper = cameraPhotoMapper;
    }

    public boolean checkReplyId(String replyId) {
        MallReply mallReply = mallReplyMapper.findByReplyId(replyId);
        return mallReply != null;
    }

    public synchronized void addTmallReply(MallReplyDto mallReplyDto) {
        String replyId = mallReplyDto.getReplyId();
        MallReply mallReply = mallReplyMapper.findByReplyId(replyId);
        if (mallReply != null) {
            log.info("{} 已存在", replyId);
            return;
        }

        mallReply = new MallReply(mallReplyDto);
        List<CameraPhoto> list = mallReplyDto.getPhotos().stream()
                .map(mallReplyPhotoDto -> {
                    CameraPhoto cameraPhoto = new CameraPhoto(mallReplyPhotoDto);
                    try {
                        String shotAtStr = mallReplyPhotoDto.getShotAt();
                        if (shotAtStr != null) {
                            LocalDateTime shotAt = DateTimeConverter.localDateTime2(mallReplyPhotoDto.getShotAt());
                            cameraPhoto.setShotAt(shotAt);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return cameraPhoto;
                })
                .collect(Collectors.toList());

        if (!list.isEmpty()) {
            saveTmallReply(mallReply, list);
        }
    }

    public void addCameraPhotos(List<MallReplyPhotoDto> list) {
        List<CameraPhoto> locations = list.stream().map(mallReplyPhotoDto -> {
            CameraPhoto cameraPhoto = new CameraPhoto(mallReplyPhotoDto);
            try {
                String shotAtStr = mallReplyPhotoDto.getShotAt();
                if (shotAtStr != null) {
                    LocalDateTime shotAt = DateTimeConverter.localDateTime2(mallReplyPhotoDto.getShotAt());
                    cameraPhoto.setShotAt(shotAt);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return cameraPhoto;
        }).collect(Collectors.toList());

        if (!locations.isEmpty()) {
            cameraPhotoMapper.saveAll(locations);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveTmallReply(MallReply mallReply, List<CameraPhoto> list) {
        mallReplyMapper.save(mallReply);
        cameraPhotoMapper.saveAll(list);
    }

    public List<MapMarker> getPhotoMarks(int itemType) {
        int pageSize = 1000;
        if (itemType == 1) {
            List<MapMarker> list = cameraPhotoMapper.findAllByReplyIdGroup(pageSize);
            return list;
        } else if (itemType == 2) {
            List<MapMarker> list = cameraPhotoMapper.findByWestChina(pageSize, itemType);
            return list;
        } else if (itemType == 3) {
            List<MapMarker> list = cameraPhotoMapper.findByWestGeoGroup(pageSize, itemType);
            return list;
        } else if (itemType == 4) {
            List<MapMarker> list = cameraPhotoMapper.findByWestGeoGroup(pageSize, itemType);
            list.forEach(mapMarker -> {
                String objectId = mapMarker.getId();
            });
            return list;
        }

        return Collections.emptyList();
    }

    public MarkerInfo getMarkerInfo(int id) {
        CameraPhoto cameraPhoto = cameraPhotoMapper.findById(id);
        if (cameraPhoto == null) {
            return null;
        }
        
        int type = cameraPhoto.getType();
        MarkerInfo markerInfo;
        if (type == 1) {
            markerInfo = cameraPhotoMapper.findMarkerInfo(id);
        } else if (type == 2) {
            markerInfo = cameraPhotoMapper.findCommentMarkerInfo(id);
        } else if (type == 3) {
            markerInfo = cameraPhotoMapper.findMarkerInfo(id);
        } else {
            markerInfo = cameraPhotoMapper.findAlbumMarkerInfo(id);
        }
        markerInfo.setType(type);

        int channelId = markerInfo.getChannelId();
        if (channelId != 0) {
            String uploadId = markerInfo.getUploadId();
            try {
                String signedUrl = ossService.getSignedUrl(channelId, uploadId);
                if (signedUrl != null) {
                    markerInfo.setPhotoUrl(signedUrl);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return markerInfo;
    }

    public List<SelectOption> getGeoItems() {
        return Arrays.stream(GeoItem.values()).map(geoItem -> {
            int id = geoItem.getValue();
            String name = geoItem.getName();
            return new SelectOption(name, id);
        }).collect(Collectors.toList());
    }
}
