/*
 * Copyright (c) 2013-2025 Hutool Team and hutool.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.hutool.v7.json.issues;

import lombok.Data;
import cn.hutool.v7.core.bean.BeanUtil;
import cn.hutool.v7.core.convert.ConvertUtil;
import cn.hutool.v7.core.reflect.TypeReference;
import cn.hutool.v7.json.JSONArray;
import cn.hutool.v7.json.JSONObject;
import cn.hutool.v7.json.JSONUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * 测试转换为TreeSet是否成功。 TreeSet必须有默认的比较器
 */
public class Issue1101Test {

	@Test
	public void treeMapConvertTest(){
		final String json = "[{\"nodeName\":\"admin\",\"treeNodeId\":\"00010001_52c95b83-2083-4138-99fb-e6e21f0c1277\",\"sort\":0,\"type\":10,\"parentId\":\"00010001\",\"children\":[],\"id\":\"52c95b83-2083-4138-99fb-e6e21f0c1277\",\"status\":true},{\"nodeName\":\"test\",\"treeNodeId\":\"00010001_97054a82-f8ff-46a1-b76c-cbacf6d18045\",\"sort\":0,\"type\":10,\"parentId\":\"00010001\",\"children\":[],\"id\":\"97054a82-f8ff-46a1-b76c-cbacf6d18045\",\"status\":true}]";
		final JSONArray objects = JSONUtil.parseArray(json);
		final TreeSet<TreeNodeDto> convert = ConvertUtil.convert(new TypeReference<>() {
		}, objects);
		Assertions.assertEquals(2, convert.size());
	}

	@Test
	public void test(){
		final String json = """
			{
			\t"children": [{
			\t\t"children": [],
			\t\t"id": "52c95b83-2083-4138-99fb-e6e21f0c1277",
			\t\t"nodeName": "admin",
			\t\t"parentId": "00010001",
			\t\t"sort": 0,
			\t\t"status": true,
			\t\t"treeNodeId": "00010001_52c95b83-2083-4138-99fb-e6e21f0c1277",
			\t\t"type": 10
			\t}, {
			\t\t"children": [],
			\t\t"id": "97054a82-f8ff-46a1-b76c-cbacf6d18045",
			\t\t"nodeName": "test",
			\t\t"parentId": "00010001",
			\t\t"sort": 0,
			\t\t"status": true,
			\t\t"treeNodeId": "00010001_97054a82-f8ff-46a1-b76c-cbacf6d18045",
			\t\t"type": 10
			\t}],
			\t"id": "00010001",
			\t"nodeName": "测试",
			\t"parentId": "0001",
			\t"sort": 0,
			\t"status": true,
			\t"treeNodeId": "00010001",
			\t"type": 0
			}""";

		final JSONObject jsonObject = JSONUtil.parseObj(json);

		final TreeNode treeNode = JSONUtil.toBean(jsonObject, TreeNode.class);
		Assertions.assertEquals(2, treeNode.getChildren().size());

		final TreeNodeDto dto = new TreeNodeDto();
		BeanUtil.copyProperties(treeNode, dto, true);
		Assertions.assertEquals(2, dto.getChildren().size());
	}

	@Data
	public static class TreeNodeDto {
		private String id;
		private String parentId;
		private int sort;
		private String nodeName;
		private int type;
		private Boolean status;
		private String treeNodeId;
		private TreeSet<TreeNodeDto> children = new TreeSet<>(Comparator.comparing(o -> o.id));
	}

	@Data
	public static class TreeNode implements Comparable<TreeNode> {
		private String id;
		private String parentId;
		private int sort;
		private String nodeName;
		private int type;
		private Boolean status;
		private String treeNodeId;
		private TreeSet<TreeNode> children = new TreeSet<>();

		@Override
		public int compareTo(final TreeNode o) {
			return id.compareTo(o.getId());
		}
	}
}
