const leftColor = {
	HZ: "#008c5e",
	HL: "#008c5e",
	HO: "#008c5e",
	HF: "#008c5e",
	NZ: "#7f8c95",
	NL: "#7f8c95",
	NO: "#7f8c95",
	NF: "#7f8c95",
}
const rightTopColor = {
	HZ: "#008c5e",
	HL: "red",
	HO: "#7f8c95",
	HF: "#008c5e",
	NZ: "#008c5e",
	NL: "red",
	NO: "#7f8c95",
	NF: "#008c5e",
}

export function handleColor(data) {

	if (checkType(data) == "array") {
		data.map((item) => {
			if (!item.color) {
				item.leftColor = "#7b75ff";
				item.rightTopColor = "#7b75ff";
				return;
			}
			for (let key in leftColor) {
				if (item.color == key) {
					item.leftColor = leftColor[key];
					item.rightTopColor = rightTopColor[key];
				}
			}
		});
		return data;
	} else if (checkType(data) == "object") {
		if (!data.color) {
			data.leftColor = "#7b75ff";
			data.rightTopColor = "#7b75ff";
			return;
		}
		for (let key in leftColor) {
			if (data.color == key) {
				data.leftColor = leftColor[key];
				data.rightTopColor = rightTopColor[key];
			}
		}
		return data;
	} else {
		return data;
	}
}

function checkType(value) {
	if (Array.isArray(value)) {
		return "array";
	} else if (value !== null && typeof value === "object") {
		return "object";
	} else {
		return "neither";
	}
}

export function validateAndParseIDCard(idCard, type) {
	// 验证身份证号格式
	const idCardPattern =
		/^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{3}([0-9Xx])$/;
	if (!idCardPattern.test(idCard)) {
		return "-";
	}

	// 提取出生年月
	const birthYear =
		idCard.length === 18 ?
		idCard.substring(6, 10) :
		`19${idCard.substring(6, 8)}`;
	const birthMonth =
		idCard.length === 18 ? idCard.substring(10, 12) : idCard.substring(8, 10);
	const birthDay =
		idCard.length === 18 ? idCard.substring(12, 14) : idCard.substring(10, 12);

	// 计算年龄
	const today = new Date();
	const birthDate = new Date(`${birthYear}-${birthMonth}-${birthDay}`);
	let age = today.getFullYear() - birthDate.getFullYear();
	if (
		today.getMonth() < birthDate.getMonth() ||
		(today.getMonth() === birthDate.getMonth() &&
			today.getDate() < birthDate.getDate())
	) {
		age--;
	}

	// 提取性别（第17位，奇数为男，偶数为女）
	const genderCode =
		idCard.length === 18 ? idCard.substring(16, 17) : idCard.substring(14, 15);
	const gender = genderCode % 2 === 0 ? "女" : "男";

	// 根据type返回对应的信息
	switch (type) {
		case "年龄":
			return age;
		case "性别":
			return gender;
		case "出生年月":
			return `${birthYear}-${birthMonth}-${birthDay}`;
		default:
			return "无效的type参数";
	}
}