package com.yonge.netty.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class MusicXmlBasicInfo {

	private Integer id;

	private Integer subjectId;
	
	private String clientId;

	private Integer detailId;

	private Long examSongId;

	private String xmlUrl;

	private String behaviorId;

	private String platform;
	
	private String partIndex;

	// 自定义配置 默认空字符串
	private String customConfiguration ="";

	private int speed;

	private String heardLevel;

	private String uuid;
	
	private float beatLength;//节拍器的持续时长（毫秒）
	
	private Integer hertz;//标准音高
	
	private Integer reactionTimeMs;//用户设置的反应时间（毫秒）

	private List<MusicXmlNote> musicXmlInfos = new ArrayList<MusicXmlNote>();

	private Map<Integer, MusicXmlSection> musicXmlSectionMap = new HashMap<Integer, MusicXmlSection>();

	public String getCustomConfiguration() {
		return customConfiguration;
	}

	public void setCustomConfiguration(String customConfiguration) {
		this.customConfiguration = customConfiguration;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getSubjectId() {
		return subjectId;
	}

	public void setSubjectId(Integer subjectId) {
		this.subjectId = subjectId;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public Integer getDetailId() {
		return detailId;
	}

	public void setDetailId(Integer detailId) {
		this.detailId = detailId;
	}

	public Long getExamSongId() {
		return examSongId;
	}

	public void setExamSongId(Long examSongId) {
		this.examSongId = examSongId;
	}

	public String getXmlUrl() {
		return xmlUrl;
	}

	public void setXmlUrl(String xmlUrl) {
		this.xmlUrl = xmlUrl;
	}

	public String getBehaviorId() {
		return behaviorId;
	}

	public void setBehaviorId(String behaviorId) {
		this.behaviorId = behaviorId;
	}

	public String getPlatform() {
		return platform;
	}

	public void setPlatform(String platform) {
		this.platform = platform;
	}

	public void setPartIndex(String partIndex) {
		this.partIndex = partIndex;
	}

	public String getPartIndex() {
		return partIndex;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public String getHeardLevel() {
		return heardLevel;
	}

	public void setHeardLevel(String heardLevel) {
		this.heardLevel = heardLevel;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public float getBeatLength() {
		return beatLength;
	}

	public void setBeatLength(int beatLength) {
		this.beatLength = beatLength;
	}

	public List<MusicXmlNote> getMusicXmlInfos() {
		return musicXmlInfos;
	}

	public void setMusicXmlInfos(List<MusicXmlNote> musicXmlInfos) {
		this.musicXmlInfos = musicXmlInfos;
	}

	public Integer getHertz() {
		return hertz;
	}

	public void setHertz(Integer hertz) {
		this.hertz = hertz;
	}

	public Integer getReactionTimeMs() {
		return reactionTimeMs;
	}

	public void setReactionTimeMs(Integer reactionTimeMs) {
		this.reactionTimeMs = reactionTimeMs;
	}

	public Map<Integer, MusicXmlSection> getMusicXmlSectionMap() {

		if (musicXmlSectionMap.size() == 0) {
			Map<Integer, List<MusicXmlNote>> map = musicXmlInfos.stream().collect(Collectors.groupingBy(MusicXmlNote::getMeasureIndex));

			List<MusicXmlNote> noteList = null;
			MusicXmlSection section = null;
			for (Entry<Integer, List<MusicXmlNote>> entry : map.entrySet()) {
				noteList = entry.getValue();

				section = new MusicXmlSection();

				section.setStartTime(noteList.stream().map(t -> t.getTimeStamp()).distinct().min(Double::compareTo).get());
				section.setDuration(noteList.stream().mapToDouble(t -> t.getDuration()).sum());
				section.setNoteNum(noteList.size());
				section.setIndex(entry.getKey());
				section.setMeasureRenderIndex(noteList.stream().findFirst().get().getMeasureRenderIndex());

				musicXmlSectionMap.put(entry.getKey(), section);
			}
		}

		return musicXmlSectionMap;
	}

	public void setMusicXmlSectionMap(Map<Integer, MusicXmlSection> musicXmlSectionMap) {
		this.musicXmlSectionMap = musicXmlSectionMap;
	}
}
