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

import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.jutil.jdk.text.TextFile;
import cn.reghao.oss.sdk.OssConsoleClient;
import cn.reghao.oss.sdk.model.OssConsoleConfig;
import cn.reghao.oss.sdk.model.dto.ObjectInfo;
import cn.reghao.tnb.content.app.ContentApplication;
import cn.reghao.tnb.content.app.geo.db.mapper.*;
import cn.reghao.tnb.content.app.geo.model.po.*;
import cn.reghao.tnb.content.app.geo.model.vo.GeoData;
import cn.reghao.tnb.content.app.geo.model.vo.MapMarker;
import cn.reghao.tnb.content.app.geo.model.vo.MapPoint;
import cn.reghao.tnb.content.app.geo.service.MapService;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author reghao
 * @date 2025-03-15 19:39:57
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = ContentApplication.class)
public class GeoTest {
    @Autowired
    MapService mapService;
    @Autowired
    GeoPointMapper geoPointMapper;
    @Test
    public void geoPointTest() {
        String lngN = "123.266667";
        String latN = "53.55";

        String lngS = "112.283333";
        String latS = "3.966667";

        String lngE = "134.7738";
        String latE = "47.7738";

        String lngW = "73.5";
        String latW = "39.38";

        MapPoint mapPointN = new MapPoint(lngN, latN);
        MapPoint mapPointS = new MapPoint(lngS, latS);
        MapPoint mapPointE = new MapPoint(lngE, latE);
        MapPoint mapPointW = new MapPoint(lngW, latW);

        /*List<GeoPoint> list = new ArrayList<>();
        list.add(new GeoPoint(mapPointN, "最北"));
        list.add(new GeoPoint(mapPointS, "最南"));
        list.add(new GeoPoint(mapPointE, "最东"));
        list.add(new GeoPoint(mapPointW, "最西"));
        geoPointMapper.saveAll(list);*/
    }

    @Autowired
    GeoChinaMapper geoChinaMapper;
    @Autowired
    GeoPolygonMapper geoPolygonMapper;
    @Test
    public void geoPolygonTest() {
        TextFile textFile = new TextFile();

        String filePath = "/home/reghao/Downloads/geo/ok_geo.csv";
        List<String> list = textFile.read(filePath);
        Map<Integer, GeoData> map  = new HashMap<>();
        for (int i = 1; i < list.size(); i++) {
            String line = list.get(i);
            String[] arr = line.split(",\"");
            String[] arr1 = arr[0].split(",");

            if (arr1[0].startsWith("71")) {
                continue;
            }

            Integer id = Integer.parseInt(arr1[0]);
            Integer pid = Integer.parseInt(arr1[1]);
            Integer deep = Integer.parseInt(arr1[2]);
            String name = arr[1].replace("\"", "");;
            String extPath = arr[2].replace("\"", "");;
            String geo = arr[3];
            String[] geoArr = geo.replace("\"", "").split(" ");
            BigDecimal lat = BigDecimal.valueOf(Double.parseDouble(geoArr[0]));
            BigDecimal lng = BigDecimal.valueOf(Double.parseDouble(geoArr[1]));
            String polygon = arr[4].replace("\"", "");
            String first = polygon.split(",")[0];
            polygon += "," + first;

            GeoPolygon geoPolygon = new GeoPolygon(id, polygon);
            GeoChina geoChina = new GeoChina(id, pid, deep, name, extPath, lat, lng);
            GeoData geoData = new GeoData(geoChina, geoPolygon);
            map.put(id, geoData);
        }

        map.forEach((id, geoData) -> {
            GeoPolygon geoPolygon = geoData.getGeoPolygon();
            GeoChina geoChina = geoData.getGeoChina();
            GeoChina geoChina1 = geoChinaMapper.findById(id);
            if (geoChina1 == null) {
                geoChina1 = geoChina;
            }
            mapService.add(geoChina1, geoPolygon);
        });
    }

    private String getEarthPolygon(String polygon) {
        List<String> list1 = new ArrayList<>();
        String[] polygonArr = polygon.split(",");
        for (String point : polygonArr) {
            String[] pointArr = point.split(" ");
            list1.add(pointArr[0] + "," + pointArr[1] + ",0");
        }

        StringBuilder sb = new StringBuilder();
        for (String str : list1) {
            sb.append(str).append(" ");
        }

        return sb.toString().trim();
    }

    @Autowired
    MallReplyMapper mallReplyMapper;
    @Test
    public void test() {
        List<MallReply> list = mallReplyMapper.findAll();
        list.forEach(mallReply -> {
            String extra = mallReply.getExtra();
            String cup = getFormattedTitle1(extra);
            if (cup != null) {
                mallReply.setCup(cup);
                mallReplyMapper.updateSetCup(mallReply.getReplyId(), cup);
            }
        });
    }

    String getFormattedTitle1(String title) {
        String extra1 = null;
        if (title.startsWith("{")) {
            JsonObject jsonObject = JsonConverter.jsonToJsonElement(title).getAsJsonObject();
            if (jsonObject.get("尺码") != null) {
                extra1 = jsonObject.get("尺码").getAsString();
                if (extra1.contains("码（建议")) {
                    String[] arr = extra1.split(" ");
                    extra1 = arr[arr.length-1].replace("）", "");
                } else if (extra1.contains("[【")) {
                    String[] arr = extra1.split("\\[【");
                    if (arr.length == 1) {
                        return null;
                    }

                    extra1 = arr[arr.length-1].replace("】]", "");
                    System.out.println();
                } else if (extra1.contains("（建议")) {
                    String[] arr = extra1.split("斤 ");
                    if (arr.length == 1) {
                        arr = extra1.split("\\[\\(");
                    }

                    extra1 = arr[arr.length-1].replace("）", "");
                } else if (extra1.contains("[(")) {
                    String[] arr = extra1.split(" ");
                    if (arr.length == 1) {
                        arr = extra1.split("\\[\\(");
                    }

                    extra1 = arr[arr.length-1].replace(")]", "");
                }
            } else if (jsonObject.get("颜色分类") != null) {
                extra1 = jsonObject.get("颜色分类").getAsString();
                if (extra1.contains("2023新版-肤色")) {
                    String[] arr = extra1.split("）");
                    if (arr.length == 1) {
                        return null;
                    }

                    extra1 = arr[arr.length-1];
                } else if (extra1.contains("杯【")) {
                    String[] arr = extra1.split("【");
                    if (arr.length == 1) {
                        arr = extra1.split("\\[\\(");
                    }

                    extra1 = arr[0].split("）")[1];
                }
            }
        } else {
            extra1 = title.split("尺码:")[1];
            String[] arr = title.split("&nbsp;&nbsp");
            for (String str : arr) {
                if (str.startsWith("尺码")) {
                    extra1 = str.split(":")[1];
                    break;
                }
            }
        }
        return extra1;
    }


    OssConsoleClient getOssConsoleClient() {
        String consoleUrl = "http://bnt.reghao.cn";
        String accessKeyId = "ESCKn3Cd";
        String accessKeySecret = "OL9SIOLoOqUjhMiQMv";
        int videoChannelId = 101;
        OssConsoleConfig ossConsoleConfig = new OssConsoleConfig(consoleUrl, accessKeyId, accessKeySecret);
        try {
            OssConsoleClient ossConsoleClient = new OssConsoleClient(ossConsoleConfig);
            return ossConsoleClient;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Autowired
    CameraPhotoMapper cameraPhotoMapper;
    @Test
    void photoMapTest() {
        OssConsoleClient ossConsoleClient = getOssConsoleClient();

        List<CameraPhoto> list = cameraPhotoMapper.findAll();
        for (CameraPhoto cameraPhoto : list) {
            String objectId = cameraPhoto.getObjectId();
            int channelId = cameraPhoto.getChannelId();
            try {
                ObjectInfo objectInfo = ossConsoleClient.getObjectInfo(channelId, objectId);
            } catch (Exception e) {
                log.info("objectId {} not exist", objectId);
            }
        }
    }

    @Autowired
    BusStationMapper busStationMapper;
    @Test
    void geoBusTest() {
        List<BusStation> list = busStationMapper.findAll();
        for (BusStation busStation : list) {
            String id = busStation.getId()+"";
            String stationName = busStation.getStationName();
            String latStr = String.valueOf(busStation.getLat());
            String latStr1 = String.format("%s.%s", latStr.substring(0, 2), latStr.substring(2));
            double lat = Double.parseDouble(latStr1);

            String lngStr = String.valueOf(busStation.getLng());
            String lngStr1 = String.format("%s.%s", lngStr.substring(0, 3), lngStr.substring(3));
            double lng = Double.parseDouble(lngStr1);
            MapPoint mapPoint = new MapPoint(lng, lat);

            MapMarker mapMarker = new MapMarker(id, stationName, mapPoint);
        }
    }
}
