package com.kamco.cd.kamcoback.postgres.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kamco.cd.kamcoback.changedetection.dto.ChangeDetectionDto;
import com.kamco.cd.kamcoback.common.enums.DetectionClassification;
import com.kamco.cd.kamcoback.postgres.entity.MapSheetAnalDataGeomEntity;
import com.kamco.cd.kamcoback.postgres.repository.changedetection.ChangeDetectionRepository;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ChangeDetectionCoreService {

  private final ChangeDetectionRepository changeDetectionRepository;

  public List<ChangeDetectionDto.TestDto> getPolygonToPoint() {
    List<MapSheetAnalDataGeomEntity> list = changeDetectionRepository.findAll();

    return list.stream()
        .map(
            p -> {
              Geometry polygon = p.getGeom();
              // 중심 좌표 계산
              Point centroid = polygon.getCentroid();

              return new ChangeDetectionDto.TestDto(
                  p.getId(), polygon, centroid.getX(), centroid.getY());
            })
        .collect(Collectors.toList());
  }

  public List<JsonNode> getPolygonToJson() {
    List<String> list = changeDetectionRepository.findPolygonJson();
    ObjectMapper mapper = new ObjectMapper();

    return list.stream()
        .map(
            s -> {
              try {
                return mapper.readTree(s);
              } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
              }
            })
        .collect(Collectors.toList());
  }

  public List<ChangeDetectionDto.CountDto> getChangeDetectionClassCount(
      Long id, String mapSheetNum) {
    List<ChangeDetectionDto.CountDto> list =
        changeDetectionRepository.getChangeDetectionClassCount(id, mapSheetNum);

    return list.stream()
        .map(
            s -> {
              String classCd = String.valueOf(s.getClassCd());
              s.setClassCd(DetectionClassification.fromString(classCd).name());
              s.setClassName(DetectionClassification.fromStrDesc(classCd));
              return s;
            })
        .collect(Collectors.toList());
  }

  public ChangeDetectionDto.CogUrlDto getChangeDetectionCogUrl(ChangeDetectionDto.CogUrlReq req) {
    return changeDetectionRepository.getChangeDetectionCogUrl(req);
  }

  public List<ChangeDetectionDto.AnalYearList> getChangeDetectionYearList() {
    return changeDetectionRepository.getChangeDetectionYearList();
  }

  public ChangeDetectionDto.PolygonFeatureList getChangeDetectionPolygonList(
      Long analUid, String mapSheetNum) {
    return changeDetectionRepository.getChangeDetectionPolygonList(analUid, mapSheetNum);
  }

  public ChangeDetectionDto.PointFeatureList getChangeDetectionPointList(
      Long analUid, String mapSheetNum) {
    return changeDetectionRepository.getChangeDetectionPointList(analUid, mapSheetNum);
  }

  public List<ChangeDetectionDto.MapSheetList> getChangeDetectionMapSheetList(Long analUid) {
    List<ChangeDetectionDto.MapSheetList> list =
        changeDetectionRepository.getChangeDetectionMapSheetList(analUid);
    return list.stream()
        .map(
            s -> {
              String name = s.getMapSheetName();
              s.setAlias(name + s.getMapSheetNum().substring(5, 8));
              return s;
            })
        .collect(Collectors.toList());
  }
}
