import FoShanDianHan from "./FoShanDianHan";
import SingleNote from "./SingleNote";


const {ccclass, property} = cc._decorator;

@ccclass
export default class NoteLines extends cc.Component {

    @property(cc.Prefab)
    singleNote: cc.Prefab = null;

	@property(cc.Node)
    maskArea: cc.Node = null;

	// @property(cc.Node)
    // judgeArea: cc.Node = null;

	@property(cc.Node)
    startNode: cc.Node = null;

	@property(cc.Node)
    endNode: cc.Node = null;

	@property(cc.Node)
    keyArea: cc.Node = null;

    notes:cc.Node[] = []//进入轨道的所有note
    beatMap = []//该轨道的所有note的谱面
    noteGenerateTimes = []//生成note的时间
    currentNote=0//下一个待加入轨道的note
		
    noteToJudge=0//下一个被触发的note
    lastNoteToJudge=0//队列尾

    nodePerfectTime = 0
	// checkPosY = 0//节点至少需要下落至该点才能检测
	gameSpeed:number = 1000//note速度,单位:像素每秒

	foShanDianHanInstance = null
    onLoad () {
        // this.musicDuration = this.node.getComponent(cc.AudioSource).getDuration();
        let nodeSlideLength = Math.abs(this.startNode.x-this.endNode.x)
        this.nodePerfectTime = nodeSlideLength/this.gameSpeed;
		this.foShanDianHanInstance = FoShanDianHan.getInstance()
    }

    // onKeyDown(event){
    //     this.judgeNote();
	// }
	initData(){
		this.currentNote = 0
		this.noteToJudge = 0
		this.lastNoteToJudge = 0
	}
    judgeNote(){
		//当下标小于数组长度且游戏出于playing状态
		if(this.noteToJudge<this.lastNoteToJudge && this.foShanDianHanInstance.gameState === 'playing'){
			//取当前游戏时间与谱面要求的判定时间的差值的绝对值作为判定标准
			var judgeTime = Math.abs(this.beatMap[this.noteToJudge]-this.foShanDianHanInstance.timePassed);
			
			let bottomY = this.notes[this.noteToJudge].getComponent(SingleNote).getSingleNoteBottomY()
			//方块下沿至少到达按键上沿才可检测
			if(judgeTime<this.foShanDianHanInstance.missTime && (bottomY <= (this.keyArea.y + this.keyArea.width/2) )){
				// if(judgeTime<GameDefine_xxm.perfectTime){
				// 	this.notes[this.noteToJudge++].getComponent(SingleNote_xxm).perfect();
				// }else if(judgeTime<GameDefine_xxm.greatTime){
				// 	this.notes[this.noteToJudge++].getComponent(SingleNote_xxm).great();
				// }else if(judgeTime<GameDefine_xxm.goodTime){
				// 	this.notes[this.noteToJudge++].getComponent(SingleNote_xxm).good();
				// }else{
				// 	this.notes[this.noteToJudge++].getComponent(SingleNote_xxm).bad();
				// }
			}
		}
	}

    start () {
		// this.keyArea.getChildByName('keyLabel').getComponent(cc.Label).string =GameDefine.keySetting[parseInt(this.node.name)];
		for(var tmp = 0;tmp<this.beatMap.length;tmp++){
			//生成note的时间是最佳判定时间减去在轨道上的运动时间加上自定义偏移
			this.noteGenerateTimes[tmp] = this.beatMap[tmp]-this.nodePerfectTime//+GameDefine.offset;
		}
    }

	generateNote(){//生成note
		//当下标小于数组长度且当下一个待加入轨道的note的生成时间小于当前时间
		if(this.currentNote<this.beatMap.length&&this.noteGenerateTimes[this.currentNote]<this.foShanDianHanInstance.timePassed){
			var newNote = cc.instantiate(this.singleNote);//创建note
			newNote.setPosition(cc.v2(-this.node.width/2,0));//设置位置为轨道顶部
			newNote.name = this.node.name
			newNote['isCollider'] = true
			this.maskArea.addChild(newNote);//加入作为轨道的子节点
			this.notes.push(newNote);//加入待判定note队列
			this.lastNoteToJudge++;
			this.currentNote++;//下标加1
		}
	}

    update (dt) {
		if(this.foShanDianHanInstance.gameState === 'playing'){
			this.generateNote();
			// var judgeTime = this.foShanDianHanInstance.timePassed-this.beatMap[this.noteToJudge];
			// if(judgeTime>this.foShanDianHanInstance.missTime){
			// 	this.notes[this.noteToJudge++].getComponent(SingleNote).miss();
			// }
		}
	}
}