import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import state, { customData } from "/src/state";
import { browser } from "../utils/index";
import {
	isSpecialMark,
	isSpeedKeyword,
	Fraction,
	SourceMeasure,
	isGradientWords,
	GRADIENT_SPEED_RESET_TAG,
	StringUtil,
	OpenSheetMusicDisplay,
} from "/osmd-extended/src";
import { GradualChange, speedInfo } from "./calcSpeed";
const browserInfo = browser();
dayjs.extend(duration);

/**
 * 获取节拍器的时间
 * @param speed 速度
 * @param firstMeasure 曲谱第一个小节
 * @returns 节拍器的时间
 */
export const getFixTime = (speed: number) => {
	const duration: any = getDuration(state.osmd as unknown as OpenSheetMusicDisplay);
	let numerator = duration.numerator || 0;
	let denominator = duration.denominator || 4;
	const beatUnit = duration.beatUnit || "quarter";
	if (state.repeatedBeats) {
		// 音频制作问题仅2拍不重复
		numerator = numerator === 2 ? 4 : numerator;
	} else if (numerator === 2 && denominator === 4) {
		numerator = 4
	}
	// console.log('diff', speed, duration, formatBeatUnit(beatUnit), denominator, numerator, (numerator / denominator))
	return state.isOpenMetronome ? (60 / speed) * formatBeatUnit(beatUnit) * (numerator / denominator) : 0;
};

export const retain = (time: number) => {
	return Math.ceil(time * 1000000) / 1000000;
};

export const formatLyricsEntries = (note: any) => {
	const voiceEntries = note.parentStaffEntry?.voiceEntries || [];
	const lyricsEntries: string[] = [];

	for (const voic of voiceEntries) {
		if (voic.lyricsEntries?.table) {
			const values: any[] = Object.values(voic.lyricsEntries.table);
			for (const lyric of values) {
				lyricsEntries.push(lyric?.value.text);
			}
		}
	}
	return lyricsEntries;
};

export const getMeasureDurationDiff = (measure: any) => {
	const { realValue: SRealValue } = measure.activeTimeSignature;
	const { realValue: RRealValue } = measure.duration;
	return SRealValue - RRealValue;
};

/** 按照dorico的渐快渐慢生成对应的速度 */
export const createSpeedInfo = (gradualChange: GradualChange | undefined, speed: number) => {
	if (gradualChange && speedInfo[gradualChange.startWord?.toLocaleLowerCase()]) {
		const notenum = Math.max(gradualChange.endXmlNoteIndex, 3);
		const speeds: number[] = [];
		const startSpeed = speed;
		const endSpeed = speed / speedInfo[gradualChange.startWord?.toLocaleLowerCase()];

		for (let index = 0; index < notenum; index++) {
			const speed = startSpeed + ((endSpeed - startSpeed) / notenum) * (index + 1);
			speeds.push(speed);
		}
		return speeds;
	}
	return;
};

const tranTime = (str: string = "") => {
	let result = str;
	const splits = str.split(":");
	if (splits.length === 1) {
		result = `00:${splits[0]}:00`;
	} else if (splits.length === 2) {
		result = `00:${splits[0]}:${splits[1]}`;
	}
	// console.log(`1970-01-01 00:${result}0`)
	return `1970-01-01 00:${result}0`;
};

export const getSlursNote = (note: any, pos?: "start" | "end") => {
	return pos === "end" ? note.noteElement.slurs[0]?.endNote : note.noteElement.slurs[0]?.startNote;
};

export const getNoteBySlursStart = (note: any, anyNoteHasSlurs?: boolean, pos?: "start" | "end") => {
	let activeNote = note;
	let slursNote = getSlursNote(activeNote, pos);

	for (const item of activeNote.measures) {
		if (item.noteElement.slurs.length) {
			slursNote = getSlursNote(item, pos);
			activeNote = item;
		}
	}
	return activeNote;
};

/** 根据 noteElement 获取note */
export const getParentNote = (note: any) => {
	let parentNote;
	if (note) {
		// time = activeNote.time
		for (const n of state.times) {
			if (note === n.noteElement) {
				// console.log(note)
				return n;
			}
		}
	}
	return parentNote;
};

export type FractionDefault = {
	numerator: number;
	denominator: number;
	wholeValue: number;
};

export type Duration = FractionDefault & {
	TempoInBPM: number;
	beatUnit: string;
};

export const getDuration = (osmd?: OpenSheetMusicDisplay): Duration => {
	if (osmd) {
		const { Duration, TempoInBPM, ActiveTimeSignature, TempoExpressions } = osmd.GraphicSheet.MeasureList[0][0]?.parentSourceMeasure;
		if (Duration) {
			let beatUnit = "quarter";
			for (const item of TempoExpressions) {
				beatUnit = item.InstantaneousTempo.beatUnit || "quarter";
			}
			const duration = formatDuration(ActiveTimeSignature, Duration) as unknown as FractionDefault;
			return {
				...duration,
				TempoInBPM,
				beatUnit,
			};
		}
	}
	const duration = new Fraction() as unknown as FractionDefault;
	return {
		...duration,
		TempoInBPM: 90,
		beatUnit: "quarter",
	};
};

export function formatDuration(activeTimeSignature: Fraction, duration: Fraction): Fraction {
	// 弱起第一小节duration不对
	return activeTimeSignature;
}

export function formatBeatUnit(beatUnit: string) {
	let multiple = 4;
	switch (beatUnit) {
		case "1024th":
			// bpm = bpm;
			multiple = 1024;
			break;
		case "512th":
			// divisionsFromNote = (noteDuration / 4) * 512;
			multiple = 512;
			break;
		case "256th":
			// divisionsFromNote = (noteDuration / 4) * 256;
			multiple = 256;
			break;
		case "128th":
			// divisionsFromNote = (noteDuration / 4) * 128;
			multiple = 128;
			break;
		case "64th":
			// divisionsFromNote = (noteDuration / 4) * 64;
			multiple = 64;
			break;
		case "32nd":
			// divisionsFromNote = (noteDuration / 4) * 32;
			multiple = 32;
			break;
		case "16th":
			// divisionsFromNote = (noteDuration / 4) * 16;
			multiple = 16;
			break;
		case "eighth":
			// divisionsFromNote = (noteDuration / 4) * 8;
			multiple = 8;
			break;
		case "quarter":
			multiple = 4;
			break;
		case "half":
			// divisionsFromNote = (noteDuration / 4) * 2;
			multiple = 2;
			break;
		case "whole":
			// divisionsFromNote = (noteDuration / 4);
			multiple = 1;
			break;
		case "breve":
			// divisionsFromNote = (noteDuration / 4) / 2;
			multiple = 0.5;
			break;
		case "long":
			// divisionsFromNote = (noteDuration / 4) / 4;
			multiple = 0.25;
			break;
		case "maxima":
			// divisionsFromNote = (noteDuration / 4) / 8;
			multiple = 0.125;
			break;
		default:
			break;
	}

	return multiple;
}

/** 根据音符单位，速度，几几拍计算正确的时间 */
export function getTimeByBeatUnit(beatUnit: string, bpm: number, denominator: number) {
	return (denominator / formatBeatUnit(beatUnit)) * bpm;
}

export type CustomInfo = {
	showSpeed: boolean;
	parsedXML: string;
};

/** 从xml中获取自定义信息，并删除多余的字符串 */
export const getCustomInfo = (xml: string): CustomInfo => {
	const data = {
		showSpeed: true,
		parsedXML: xml,
	};
	const xmlParse = new DOMParser().parseFromString(xml, "text/xml");
	const words: any = xmlParse.getElementsByTagName("words");
	for (const word of words) {
		if (word && word.textContent?.trim() === "隐藏速度") {
			data.showSpeed = false;
			word.textContent = "";
		}
		if (word && word.textContent?.trim() === "@") {
			word.textContent = "segno";
		}
	}
	data.parsedXML = new XMLSerializer().serializeToString(xmlParse);
	return data;
};

/**
 * 替换文本标签中的内容
 */
const replaceTextConent = (beforeText: string, afterText: string, ele: Element): Element => {
	const words: any = ele?.getElementsByTagName("words");
	for (const word of words) {
		if (word && word.textContent?.trim() === beforeText) {
			word.textContent = afterText;
		}
	}
	return ele;
};

/**
 * 添加第一分谱信息至当前分谱
 * @param ele 需要插入的元素
 * @param fitstParent 合奏谱第一个分谱
 * @param parent 需要添加的分谱
 */
const setElementNoteBefore = (ele: Element, fitstParent: Element, parent?: Element | null) => {
	let noteIndex: number = 0;
	if (!fitstParent) {
		return;
	}
	for (let index = 0; index < fitstParent.childNodes.length; index++) {
		const element = fitstParent.childNodes[index];
		if (element.nodeName === "note") {
			noteIndex++;
		}
		if (element === ele) {
			break;
		}
	}
	if (noteIndex === 0 && parent) {
		parent.insertBefore(ele, parent.childNodes[0]);
		return;
	}
	if (parent && parent.childNodes.length > 0) {
		let noteIndex2: number = 0;
		const notes = Array.from(parent.childNodes).filter((child) => child.nodeName === "note");
		const lastNote = notes[notes.length - 1];
		if (noteIndex >= notes.length && lastNote) {
			parent.insertBefore(ele, parent.childNodes[Array.from(parent.childNodes).indexOf(lastNote)]);
			return;
		}
		for (let index = 0; index < notes.length; index++) {
			const element = notes[index];
			if (element.nodeName === "note") {
				noteIndex2 = noteIndex2 + 1;
				if (noteIndex2 === noteIndex) {
					parent.insertBefore(ele, element);
					break;
				}
			}
		}
	}
	// console.log(noteIndex, parent)
};

/**
 * 检查传入文字是否为重复关键词
 * @param text 总谱xml
 * @returns 是否是重复关键词
 */
export const isRepeatWord = (text: string): boolean => {
	if (text) {
		const innerText = text.toLocaleLowerCase();
		const dsRegEx: string = "d\\s?\\.s\\.";
		const dcRegEx: string = "d\\.\\s?c\\.";

		return (
			innerText === "@" ||
			StringUtil.StringContainsSeparatedWord(innerText, dsRegEx + " al fine", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, dsRegEx + " al coda", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, dcRegEx + " al fine", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, dcRegEx + " al coda", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, dcRegEx) ||
			StringUtil.StringContainsSeparatedWord(innerText, "da\\s?capo", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, dsRegEx, true) ||
			StringUtil.StringContainsSeparatedWord(innerText, "dal\\s?segno", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, "al\\s?coda", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, "to\\s?coda", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, "a (la )?coda", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, "fine", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, "coda", true) ||
			StringUtil.StringContainsSeparatedWord(innerText, "segno", true)
		);
	}
	return false;
};

export const onlyVisible = (xml: string, partIndex: number): string => {
	if (!xml) return "";
	const detailId = state.examSongId + "";
	const xmlParse = new DOMParser().parseFromString(xml, "text/xml");
	const partList = xmlParse.getElementsByTagName("part-list")?.[0]?.getElementsByTagName("score-part") || [];
	const partListNames = Array.from(partList).map((item) => item.getElementsByTagName("part-name")?.[0].textContent || "");
	const parts: any = xmlParse.getElementsByTagName("part");
	// const firstTimeInfo = parts[0]?.getElementsByTagName('metronome')[0]?.parentElement?.parentElement?.cloneNode(true)
	const firstMeasures = [...parts[0]?.getElementsByTagName("measure")];
	const metronomes = [...parts[0]?.getElementsByTagName("metronome")];
	const words = [...parts[0]?.getElementsByTagName("words")];
	const codas = [...parts[0]?.getElementsByTagName("coda")];
	const rehearsals = [...parts[0]?.getElementsByTagName("rehearsal")];

	/** 第一分谱如果是约定的配置分谱则跳过 */
	if (partListNames[0]?.toLocaleUpperCase?.() === "COMMON") {
		partIndex++;
		partListNames.shift();
	}
	const visiblePartInfo = partList[partIndex];
	// console.log(visiblePartInfo, partIndex)
	state.partListNames = partListNames;
	if (visiblePartInfo) {
		const id = visiblePartInfo.getAttribute("id");
		Array.from(parts).forEach((part: any) => {
			if (part && part.getAttribute("id") !== id) {
				part.parentNode?.removeChild(part);
				// 不等于第一行才添加避免重复添加
			} else if (part && part.getAttribute("id") !== "P1") {
				// 速度标记仅保留最后一个
				const metronomeData: {
					[key in string]: Element;
				} = {};
				for (let i = 0; i < metronomes.length; i++) {
					const metronome = metronomes[i];
					const metronomeContainer = metronome.parentElement?.parentElement?.parentElement;
					if (metronomeContainer) {
						const index = firstMeasures.indexOf(metronomeContainer);
						metronomeData[index] = metronome;
					}
				}
				Object.values(metronomeData).forEach((metronome) => {
					const metronomeContainer: any = metronome.parentElement?.parentElement;
					const parentMeasure: any = metronomeContainer?.parentElement;
					const measureMetronomes = [...(parentMeasure?.childNodes || [])];
					const metronomesIndex = metronomeContainer ? measureMetronomes.indexOf(metronomeContainer) : -1;
					// console.log(parentMeasure)
					if (parentMeasure && metronomesIndex > -1) {
						const index = firstMeasures.indexOf(parentMeasure);
						const activeMeasure = part.getElementsByTagName("measure")[index];
						setElementNoteBefore(metronomeContainer, parentMeasure, activeMeasure);
					}
				});
				/** word比较特殊需要精确到note位置 */
				words.forEach((word) => {
					let text = word.textContent || "";
					text = ["cresc."].includes(text) ? "" : text;
					if ((isSpecialMark(text) || isSpeedKeyword(text) || isGradientWords(text) || isRepeatWord(text) || GRADIENT_SPEED_RESET_TAG) && text) {
						const wordContainer = word.parentElement?.parentElement;
						const parentMeasure = wordContainer?.parentElement;
						const measureWords = [...(parentMeasure?.childNodes || [])];
						const wordIndex = wordContainer ? measureWords.indexOf(wordContainer) : -1;
						if (wordContainer && parentMeasure && wordIndex > -1) {
							const index = firstMeasures.indexOf(parentMeasure);
							const activeMeasure = part.getElementsByTagName("measure")[index];
							// 找当前小节是否包含word标签
							const _words = Array.from(activeMeasure?.getElementsByTagName("words") || []);
							// 遍历word标签,检查是否和第一小节重复,如果有重复则不平移word
							const total = _words.reduce((total: any, _word: any) => {
								if (_word.textContent?.includes(text)) {
									total++;
								}
								return total;
							}, 0);
							if (total === 0) {
								if (["12280"].includes(detailId)) {
									activeMeasure?.insertBefore(wordContainer.cloneNode(true), activeMeasure?.childNodes[wordIndex]);
								} else {
									setElementNoteBefore(wordContainer, parentMeasure, activeMeasure);
								}
							}
						}
					}
				});
				/** word比较特殊需要精确到note位置 */
				codas.forEach((coda) => {
					const wordContainer = coda.parentElement?.parentElement;
					const parentMeasure = wordContainer?.parentElement;
					const measureWords = [...(parentMeasure?.childNodes || [])];
					const wordIndex = wordContainer ? measureWords.indexOf(wordContainer) : -1;
					if (wordContainer && parentMeasure && wordIndex > -1) {
						const index = firstMeasures.indexOf(parentMeasure);
						const activeMeasure = part.getElementsByTagName("measure")[index];
						if (["12280"].includes(detailId)) {
							activeMeasure?.insertBefore(wordContainer.cloneNode(true), activeMeasure?.childNodes[wordIndex]);
						} else {
							setElementNoteBefore(wordContainer, parentMeasure, activeMeasure);
						}
					}
				});
				rehearsals.forEach((rehearsal) => {
					const container = rehearsal.parentElement?.parentElement;
					const parentMeasure = container?.parentElement;
					// console.log(rehearsal)
					if (parentMeasure) {
						const index = firstMeasures.indexOf(parentMeasure);
						part.getElementsByTagName("measure")[index]?.appendChild(container.cloneNode(true));
						// console.log(index, parentMeasure, firstMeasures.indexOf(parentMeasure))
					}
				});
			} else {
				words.forEach((word) => {
					const text = word.textContent || "";
					if (isSpeedKeyword(text) && text) {
						const wordContainer = word.parentElement?.parentElement?.parentElement;
						if (wordContainer && wordContainer.firstElementChild && wordContainer.firstElementChild !== word) {
							const wordParent = word.parentElement?.parentElement;
							const fisrt = wordContainer.firstElementChild;
							wordContainer.insertBefore(wordParent, fisrt);
						}
					}
				});
			}

			// 最后一个小节的结束线元素不在最后 调整
			if (part && part.getAttribute("id") === id) {
				const barlines = part.getElementsByTagName("barline");
				const lastParent = barlines[barlines.length - 1]?.parentElement;
				if (lastParent?.lastElementChild?.tagName !== "barline") {
					const children = lastParent?.children || [];
					for (let el of children) {
						if (el.tagName === "barline") {
							// 将结束线元素放到最后
							lastParent?.appendChild(el);
							break;
						}
					}
				}
			}
		});
		Array.from(partList).forEach((part) => {
			if (part && part.getAttribute("id") !== id) {
				part.parentNode?.removeChild(part);
			}
		});
	}
	// console.log(xmlParse)
	return new XMLSerializer().serializeToString(appoggianceFormate(xmlParse));
};

// 倚音后连音线
export const appoggianceFormate = (xmlParse: Document): Document => {
	if (!xmlParse) return xmlParse;
	const graces: any = xmlParse.querySelectorAll("grace");
	if (!graces.length) return xmlParse;
	const getNextElement = (el: HTMLElement): HTMLElement => {
		if (el.querySelector("grace")) {
			return getNextElement(el?.nextElementSibling as HTMLElement);
		}
		return el;
	};
	for (let grace of graces) {
		const notations = grace.parentElement?.querySelector("notations");
		if (notations && notations.querySelectorAll("slur").length > 1) {
			let nextEle: Element = getNextElement(grace.parentElement?.nextElementSibling as HTMLElement);
			if (nextEle && nextEle.querySelectorAll("slur").length > 0) {
				const slurNumber = Array.from(nextEle.querySelector("notations")?.children || []).map((el: Element) => {
					return el.getAttribute("number");
				});
				const slurs = notations.querySelectorAll("slur");
				for (let nota of slurs) {
					if (!slurNumber.includes(nota.getAttribute("number"))) {
						nextEle.querySelector("notations")?.appendChild(nota);
					}
				}
			}
		}
	}
	return xmlParse;
};

/** 根据ID获取最顶级ID */
export const isWithinScope = (tree: any[], id: number): number => {
	if (!tree) return 0;
	let result = 0;
	for (const item of tree) {
		if (item.id === id) {
			result = item.id;
			break;
		}
		if (item.sysMusicScoreCategoriesList) {
			result = isWithinScope(item.sysMusicScoreCategoriesList, id);
			if (result > 0) {
				result = item.id;
			}
			if (result) break;
		}
	}
	return result;
};

class NoteList {
	notes: any[] = [];
	constructor(notes: any[]) {
		this.notes = notes;
	}

	public last() {
		return this.notes[this.notes.length - 1];
	}

	public list() {
		return this.notes;
	}
}

export const getNotesByid = (id: string): NoteList => {
	const notes = new NoteList(state.times.filter((item) => item.id === id));
	return notes;
};

/** 格式化当前曲谱缩放比例 */
export const formatZoom = (num = 1) => {
	return num * state.zoom;
};

/** 格式化曲谱
 * 1.全休止符的小节,没有音符默认加个全休止符
 */
export const formatXML = (xml: string): string => {
	if (!xml) return "";
	const xmlParse = new DOMParser().parseFromString(xml, "text/xml");
	const measures = Array.from(xmlParse.getElementsByTagName("measure"));
	// let speed = -1
	let beats = -1;
	let beatType = -1;
	// 小节中如果没有节点默认为休止符
	for (const measure of measures) {
		if (beats === -1 && measure.getElementsByTagName("beats").length) {
			beats = parseInt(measure.getElementsByTagName("beats")[0].textContent || "4");
		}
		if (beatType === -1 && measure.getElementsByTagName("beat-type").length) {
			beatType = parseInt(measure.getElementsByTagName("beat-type")[0].textContent || "4");
		}
		// if (speed === -1 && measure.getElementsByTagName('per-minute').length) {
		//   speed = parseInt(measure.getElementsByTagName('per-minute')[0].textContent || this.firstLib?.speed)
		// }
		const divisions = parseInt(measure.getElementsByTagName("divisions")[0]?.textContent || "256");
		if (measure.getElementsByTagName("note").length === 0) {
			const forwardTimeElement = measure.getElementsByTagName("forward")[0]?.getElementsByTagName("duration")[0];
			if (forwardTimeElement) {
				forwardTimeElement.textContent = "0";
			}
			measure.innerHTML =
				measure.innerHTML +
				`
        <note>
          <rest measure="yes"/>
          <duration>${divisions * beats}</duration>
          <voice>1</voice>
          <type>whole</type>
        </note>`;
		}
	}
	return new XMLSerializer().serializeToString(xmlParse);
};

/** 获取所有音符的时值,以及格式化音符 */
export const formateTimes = (osmd: OpenSheetMusicDisplay) => {
	const customNoteRealValue = customData.customNoteRealValue;
	const customNoteCurrentTime = customData.customNoteCurrentTime;
	const detailId = state.examSongId + "";
	const partIndex = state.partIndex + "";
	let fixtime = browserInfo.huawei ? 0.08 : 0; //getFixTime()
	const allNotes: any[] = [];
	const allNoteId: string[] = [];
	const allMeasures: any[] = [];
	const { originSpeed: baseSpeed } = state;
	const formatRealKey = (realKey: number, detail: any) => {
		// 不是管乐迷, 不处理
		if (state.appName !== "GYM") return realKey;
		// 长笛的LEVEL 2-5-1条练习是泛音练习，以每小节第一个音的指法为准，高音不变变指法。
		const olnyOneIds = ["906"];
		if (olnyOneIds.includes(detailId)) {
			return detail.measures[0]?.realKey || realKey;
		}
		// 圆号的LEVEL 2-5条练习是泛音练习，最后四小节指法以连音线第一个小节为准
		const olnyOneIds2 = ["782", "784"];
		if (olnyOneIds2.includes(detailId)) {
			const measureNumbers = [14, 16, 30, 32];
			if (measureNumbers.includes(detail.firstVerticalMeasure?.measureNumber)) {
				return allNotes[allNotes.length - 1]?.realKey || realKey;
			}
		}
		// 2-6 第三小节指法按照第一个音符显示
		const filterIds = ["900", "901", "640", "641", "739", "740", "800", "801", "773", "774", "869", "872", "714", "715"];
		if (filterIds.includes(detailId)) {
			if (detail.firstVerticalMeasure?.measureNumber === 3 || detail.firstVerticalMeasure?.measureNumber === 9) {
				return detail.measures[0]?.realKey || realKey;
			}
		}
		return realKey;
	};
	if (!osmd.cursor) return [];
	const iterator: any = osmd.cursor.Iterator;
	// console.log("🚀 ~ iterator:", iterator)
	console.time("音符跑完时间");

	let i = 0;
	let si = 0;
	let measures: any[] = [];
	let stepSpeeds: number[] = [];
	/** 弱起时间 */
	let difftime = 0;
	let usetime = 0;
	let relaMeasureLength = 0;
	let beatUnit = "quarter";
	let gradualSpeed;
	let gradualChange: GradualChange | undefined;
	let gradualChangeIndex = 0;
	let totalMultipleRestMeasures = 0;
	let multipleRestMeasures = 0;
	const _notes = [] as any[];
	if (state.gradualTimes) {
		console.log("后台设置的渐慢小节时间", state.gradual, state.gradualTimes);
	}

	let currentTimeStamp = iterator.currentTimeStamp.RealValue;
	const currentTimes = [] as any[];
	let isSetNextNoteReal = false;
	let differFrom = 0;
	while (!iterator.EndReached) {
		// console.log({ ...iterator });
		const voiceEntries = iterator.CurrentVoiceEntries?.[0] ? [iterator.CurrentVoiceEntries?.[0]] : [];
		let currentVoiceEntries: any[] = [];
		// 单声部多声轨
		if (state.multitrack > 0) {
			currentVoiceEntries = [...iterator.CurrentVoiceEntries];
		} else {
			currentVoiceEntries = [...iterator.CurrentVoiceEntries].filter((n) => {
				return n && n?.ParentVoice?.VoiceId != 1;
			});
		}
		let currentTime = 0;
		let isDouble = false;
		let isMutileSubject = false;

		if (currentVoiceEntries.length && !isSetNextNoteReal) {
			isDouble = true;
			let voiceNotes = [...iterator.CurrentVoiceEntries].reduce((notes, n) => {
				notes.push(...n.Notes);
				return notes;
			}, [] as any);
			voiceNotes = voiceNotes.sort((a: any, b: any) => a?.length?.realValue - b?.length?.realValue);
			currentTime = voiceNotes?.[0]?.length?.realValue || 0;

			if (state.multitrack > 0 && currentVoiceEntries.length === 2) {
				const min = voiceNotes[0]?.length?.realValue || 0;
				const max = voiceNotes[voiceNotes.length - 1]?.length?.realValue || 0;
				differFrom = max - min;
				isSetNextNoteReal = differFrom === 0 ? false : true;
			}
		}
		// 多声部上下音符没对齐,光标多走一拍
		if (_notes[_notes.length - 1]?.isDouble && !currentVoiceEntries.length) {
			isMutileSubject = true;
		}
		if (state.multitrack > 0 && !isDouble && isSetNextNoteReal) {
			isDouble = true;
			currentTime = differFrom;
			isSetNextNoteReal = false;
			differFrom = 0;
		}
		currentTimes.push(iterator.currentTimeStamp.realValue - currentTimeStamp);
		currentTimeStamp = iterator.currentTimeStamp.realValue;
		for (const v of voiceEntries) {
			let note = v.notes[0];
			if (note.IsGraceNote) {
				// 如果是装饰音, 取不是装饰音的时值
				const voice = note.parentStaffEntry.voiceEntries.find((_v: any) => !_v.isGrace);
				note = voice.notes[0];
			}
			note.fixedKey = note.ParentVoiceEntry.ParentVoice.Parent.SubInstruments[0].fixedKey || 0;
			// 有倚音
			if (note?.voiceEntry?.isGrace) {
				isDouble = true;
				let ns = [...iterator.currentVoiceEntries].reduce((notes, n) => {
					notes.push(...n.notes);
					return notes;
				}, []);
				ns = ns.sort((a: any, b: any) => b?.length?.realValue - a?.length?.realValue);
				currentTime = currentTime != 0 ? Math.min(ns?.[0]?.length?.realValue, currentTime) : ns?.[0]?.length?.realValue;
			}
			if (state.multitrack > 0 && currentTime > note.length.realValue) {
				currentTime = note.length.realValue;
			}
			_notes.push({
				note,
				iterator: { ...iterator },
				currentTime,
				isDouble,
				isMutileSubject,
			});
		}

		iterator.moveToNextVisibleVoiceEntry(false);
	}
	for (let { note, iterator, currentTime, isDouble, isMutileSubject } of _notes) {
		if (note) {
			if (si === 0) {
				allMeasures.push(note.sourceMeasure);
			}
			if (si === 0 && state.isSpecialBookCategory) {
				for (const expression of (note.sourceMeasure as SourceMeasure)?.TempoExpressions) {
					if (expression?.InstantaneousTempo?.beatUnit) {
						// 取最后一个有效的tempo
						beatUnit = expression.InstantaneousTempo.beatUnit;
					}
				}
			}
			let measureSpeed = note.sourceMeasure.tempoInBPM;
			const { metronomeNoteIndex } = iterator.currentMeasure;
			if (metronomeNoteIndex !== 0 && metronomeNoteIndex > si) {
				measureSpeed = allNotes[allNotes.length - 1]?.speed || 100;
			}

			const activeVerticalMeasureList = [note.sourceMeasure.verticalMeasureList?.[0]] || [];
			const { realValue } = iterator.currentTimeStamp;
			const { RealValue: vRealValue, Denominator: vDenominator } = formatDuration(
				iterator.currentMeasure.activeTimeSignature,
				iterator.currentMeasure.duration
			);
			let { wholeValue, numerator, denominator, realValue: NoteRealValue } = note.length;
			if (customNoteRealValue[i]) {
				// console.log(NoteRealValue, customNoteRealValue[i])
				NoteRealValue = customNoteRealValue[i];
			}
			if (isDouble && currentTime > 0) {
				if (currentTime != NoteRealValue) {
					console.log(`小节 ${note.sourceMeasure.MeasureNumberXML} 替换: noteLength: ${NoteRealValue}, 最小: ${currentTime}`);
					NoteRealValue = currentTime;
				}
			}
			// note.sourceMeasure.MeasureNumberXML === 8 && console.error(`小节 ${note.sourceMeasure.MeasureNumberXML}`, NoteRealValue)
			// 管乐迷,按自定义按读取到的音符时值
			if (customNoteCurrentTime) {
				if (isMutileSubject && currentTimes[i + 1] > 0 && NoteRealValue > currentTimes[i + 1]) {
					console.log(NoteRealValue, currentTimes[i + 1])
					NoteRealValue = currentTimes[i + 1];
				}
			}

			let relativeTime = usetime;
			// 速度不能为0 此处的速度应该是按照设置的速度而不是校准后的速度，否则mp3速度不对
			let beatSpeed = (state.isSpecialBookCategory ? measureSpeed : baseSpeed) || 1;
			// 如果有节拍器,需要将节拍器的时间算出来
			if (i === 0) {
				fixtime += getFixTime(beatSpeed);
				state.fixtime = fixtime;
				console.log("fixtime:", fixtime, '速度:', beatSpeed, "state.isSpecialBookCategory:", state.isSpecialBookCategory, 'state.isOpenMetronome:', state.isOpenMetronome);
			}
			// console.log(getTimeByBeatUnit(beatUnit, measureSpeed, iterator.currentMeasure.activeTimeSignature.Denominator))
			let gradualLength = 0;
			let speed = (state.isSpecialBookCategory ? measureSpeed : baseSpeed) || 1;
			gradualChange = iterator.currentMeasure.speedInfo || gradualChange;
			gradualSpeed = osmd.Sheet.SoundTempos?.get(note.sourceMeasure.measureListIndex) || gradualSpeed;
			if (!gradualSpeed || gradualSpeed.length < 2) {
				gradualSpeed = createSpeedInfo(gradualChange, speed);
			}
			// console.log({...iterator.currentMeasure},gradualChange, gradualSpeed)
			const measureListIndex = iterator.currentMeasure.measureListIndex;
			// 计算相差时间按照比例分配到所有音符上
			if (state.gradualTimes && Object.keys(state.gradualTimes).length > 0) {
				const withInRangeNote = state.gradual.find((item, index) => {
					const nextItem: any = state.gradual[index + 1];
					return (
						item[0].measureIndex <= measureListIndex &&
						item[1]?.measureIndex! >= measureListIndex &&
						(!nextItem || nextItem?.[0].measureIndex !== measureListIndex)
					);
				});
				const [first, last] = withInRangeNote || [];
				if (first && last) {
					// 小节数量
					const continuous = last.measureIndex - first.measureIndex;
					// 开始小节内
					const inTheFirstMeasure = first.closedMeasureIndex == measureListIndex && si >= first.noteInMeasureIndex;
					// 结束小节内
					const inTheLastMeasure = last.closedMeasureIndex === measureListIndex && si < last.noteInMeasureIndex;
					// 范围内小节
					const inFiestOrLastMeasure = first.closedMeasureIndex !== measureListIndex && last.closedMeasureIndex !== measureListIndex;
					if (inTheFirstMeasure || inTheLastMeasure || inFiestOrLastMeasure) {
						const startTime = state.gradualTimes[first.measureIndex];
						const endTime = state.gradualTimes[last.measureIndex];
						if (startTime && endTime) {
							const times = continuous - first.leftDuration / first.allDuration + last.leftDuration / last.allDuration;
							const diff = dayjs(tranTime(endTime)).diff(dayjs(tranTime(startTime)), "millisecond");
							gradualLength = ((NoteRealValue / vRealValue / times) * diff) / 1000;
						}
					}
				}
			} else if (state.appName === "GYM" && gradualChange && gradualSpeed && (gradualChange.startXmlNoteIndex === si || gradualChangeIndex > 0)) {
				const startSpeed = gradualSpeed[0] - (gradualSpeed[1] - gradualSpeed[0]);
				const { resetXmlNoteIndex, endXmlNoteIndex } = gradualChange;
				const noteDiff = endXmlNoteIndex;
				let stepSpeed = (gradualSpeed[gradualSpeed.length - 1] - startSpeed) / noteDiff;
				stepSpeed = note.DotsXml ? stepSpeed / 1.5 : stepSpeed;
				if (gradualChangeIndex < noteDiff) {
					const tempSpeed = Math.ceil(speed + stepSpeed * gradualChangeIndex);
					let tmpSpeed = getTimeByBeatUnit(beatUnit, tempSpeed, iterator.currentMeasure.activeTimeSignature.Denominator);
					const maxLength = (wholeValue + numerator / denominator) * vDenominator * (60 / tmpSpeed);
					// speed += stepSpeeds.reduce((a, b) => a + b, 0)
					speed += Math.ceil(stepSpeed * (gradualChangeIndex + 1));
					tmpSpeed = getTimeByBeatUnit(beatUnit, speed, iterator.currentMeasure.activeTimeSignature.Denominator);
					const minLength = (wholeValue + numerator / denominator) * vDenominator * (60 / tmpSpeed);
					gradualLength = (maxLength + minLength) / 2;
				} else if (resetXmlNoteIndex > gradualChangeIndex) {
					speed = allNotes[i - 1]?.speed;
				}
				beatSpeed =
					(state.isSpecialBookCategory ? getTimeByBeatUnit(beatUnit, speed, iterator.currentMeasure.activeTimeSignature.Denominator) : baseSpeed) || 1;
				const isEnd = !(gradualChangeIndex < noteDiff) && !(resetXmlNoteIndex > gradualChangeIndex);
				gradualChangeIndex++;
				if (isEnd) {
					gradualChangeIndex = 0;
					gradualChange = undefined;
					gradualSpeed = undefined;
					stepSpeeds = [];
				}
			}
			const _noteLength = NoteRealValue;
			let noteLength = gradualLength ? gradualLength : Math.min(vRealValue, NoteRealValue) * formatBeatUnit(beatUnit) * (60 / beatSpeed);
			const measureLength = vRealValue * vDenominator * (60 / beatSpeed);
			// console.table({value: iterator.currentTimeStamp.realValue, vRealValue,NoteRealValue, noteLength,measureLength, MeasureNumberXML: note.sourceMeasure.MeasureNumberXML})
			// console.log(i, Math.min(vRealValue, NoteRealValue),noteLength,gradualLength, formatBeatUnit(beatUnit),beatSpeed, NoteRealValue * formatBeatUnit(beatUnit) * (60 / beatSpeed) )
			usetime += noteLength;
			relaMeasureLength += noteLength;
			let relaEndtime = noteLength + relativeTime;
			const fixedKey = note.fixedKey || 0;
			const svgElement = activeVerticalMeasureList[0]?.vfVoices["1"]?.tickables[si];
			// console.log(note.sourceMeasure.MeasureNumberXML,note,svgElement, NoteRealValue, measureLength)
			if (allNotes.length && allNotes[allNotes.length - 1].relativeTime === relativeTime) {
				continue;
			}
			// console.log(iterator.currentMeasure)
			// 如果是弱起就补齐缺省的时长
			if (i === 0) {
				let _firstMeasureRealValue = 0;
				const staffEntries = note.sourceMeasure.verticalMeasureList?.[0]?.staffEntries || [];
				//计算第一个小节里面的音符时值是否等于整个小节的时值
				staffEntries.forEach((_a: any) => {
					if (_a?.sourceStaffEntry?.voiceEntries?.[0]?.notes?.[0]?.length?.realValue) {
						_firstMeasureRealValue += _a.sourceStaffEntry.voiceEntries[0].notes[0].length.realValue;
					}
				});
				if (_firstMeasureRealValue < vRealValue) {
					// console.log(_firstMeasureRealValue, vRealValue)
					// 如果是弱起,将整个小节的时值减去音符的时值,就是缺省的时值
					difftime = measureLength - noteLength;
				}
				if (difftime > 0) {
					fixtime += difftime;
				}
				// 管乐迷 diff获取不准确时, 弱起补齐
				if (["2589", "2561", "2560", "2559", "2558", "2556", "2555", "2554"].includes(detailId)) {
					// difftime = iterator.currentTimeStamp.realValue * formatBeatUnit(beatUnit) * (60 / beatSpeed);
					// fixtime += difftime;
				}
			}
			let stave = activeVerticalMeasureList[0]?.stave;

			if (note.sourceMeasure.multipleRestMeasures) {
				totalMultipleRestMeasures = note.sourceMeasure.multipleRestMeasures;
				multipleRestMeasures = 0;
			}
			if (multipleRestMeasures < totalMultipleRestMeasures) {
				multipleRestMeasures++;
			} else {
				multipleRestMeasures = 0;
				totalMultipleRestMeasures = 0;
			}

			// console.log(note.tie)
			const nodeDetail = {
				isStaccato: note.voiceEntry.isStaccato(),
				isRestFlag: note.isRestFlag,
				noteId: note.NoteToGraphicalNoteObjectId,
				measureListIndex: note.sourceMeasure.measureListIndex,
				MeasureNumberXML: note.sourceMeasure.MeasureNumberXML,
				_noteLength: _noteLength,
				svgElement: svgElement,
				frequency: note?.pitch?.frequency || -1,
				nextFrequency: note?.pitch?.nextFrequency || -1,
				prevFrequency: note?.pitch?.prevFrequency || -1,
				difftime,
				octaveOffset: activeVerticalMeasureList[0]?.octaveOffset,
				speed,
				beatSpeed,
				i,
				si,
				stepSpeeds,
				measureOpenIndex: allMeasures.length - 1,
				measures,
				tempoInBPM: note.sourceMeasure.tempoInBPM,
				measureLength,
				relaMeasureLength,
				id: svgElement?.attrs.id,
				note: note.halfTone + 12, // see issue #224
				relativeTime: retain(relativeTime),
				time: retain(relativeTime + fixtime),
				endtime: retain(relaEndtime + fixtime),
				relaEndtime: retain(relaEndtime),
				realValue,
				halfTone: note.halfTone,
				noteElement: note,
				fixedKey,
				realKey: 0,
				duration: 0,
				formatLyricsEntries: formatLyricsEntries(note),
				stave,
				firstVerticalMeasure: activeVerticalMeasureList[0],
				noteLength: 1,
				osdmContext: osmd,
				speedbeatUnit: beatUnit,
				multipleRestMeasures: multipleRestMeasures,
			};
			nodeDetail.realKey = formatRealKey(note.halfTone - fixedKey * 12, nodeDetail);
			nodeDetail.duration = nodeDetail.endtime - nodeDetail.time;
			let tickables = activeVerticalMeasureList[0]?.vfVoices["1"]?.tickables || [];
			if ([121].includes(state.subjectId)) {
				tickables = note.sourceMeasure.verticalSourceStaffEntryContainers;
			}
			// console.log(note.sourceMeasure.MeasureNumberXML, note.sourceMeasure.verticalSourceStaffEntryContainers.length)
			nodeDetail.noteLength = tickables.length || 1;
			allNotes.push(nodeDetail);
			allNoteId.push(nodeDetail.id);
			measures.push(nodeDetail);
			if (si < tickables.length - 1) {
				si++;
			} else {
				si = 0;
				relaMeasureLength = 0;
				measures = [];
			}
		}
		i++;
	}
	// 按照时间轴排序
	const sortArray = allNotes.sort((a, b) => a.relativeTime - b.relativeTime).map((item, index) => ({ ...item, i: index }));
	console.timeEnd("音符跑完时间");
	try {
		osmd.cursor.reset();
	} catch (error) {}
	state.activeMeasureIndex = sortArray[0].MeasureNumberXML;
	return sortArray;
};

/** 获取小节之间的连音线，仅同音高*/
export const getNoteByMeasuresSlursStart = (note: any) => {
	let activeNote = note;
	let tieNote;
	if (note.noteElement.tie && note.noteElement.tie.StartNote) {
		tieNote = note.noteElement.tie.StartNote;
	}
	if (activeNote && tieNote && tieNote !== activeNote.noteElement) {
		for (const note of state.times) {
			if (tieNote === note.noteElement) {
				return note;
			}
		}
	}
	return activeNote;
};
