// 总条数
var num = 0;
var timer = null;
var actionTimer = null;
var moveTimer = null;
// 粉丝的昵称
var nameArr = ['鹅鹅鹅鹅鹅鹅','打嗝','旋风腿儿','霹雳腿儿','老寒腿儿','哇咔咔蹦跶'];
// 粉丝说的话
var sayArr = ['没人','内容','空空风','美女姐姐','小阔爱','老阔爱'];
// 动作
var actionArr = ['dancing-left','dancing-right','dancing-down','dancing-up','dancing-wobble','dancing-wobble-right'];
// 人物位移动画
var moveArr = ['run-right','run-left','run-right-top','run-left-top','run-right-bottom','run-left-bottom'];
var peopleId = 0;
// 窗体宽度
var winWidth = $(window).width();
// 窗体高度
var winheight = $(window).height();
//控制速度1px
var speed = 1;
var upTimer = null,downTimer = null,leftTimer = null,rightTimer = null;
var suiDanTimer = null;
var startIndex = 0;
// 集体舞
function allDancing(dangcing){
	if(dangcing!=''){
		clearInterval(upTimer);
		clearInterval(downTimer);
		clearInterval(leftTimer);
		clearInterval(rightTimer);
		actionArr.forEach((item,index)=>{
			var haveDan = $(`.people-img`).is(`.${item}`);
			if(haveDan){
				$(`.people-img`).removeClass(item);
				// $(`.people-img`).addClass(actionArr[Math.floor(Math.random()*actionArr.length)]);
				$(`.people-img`).addClass(dangcing);
			}
		})
		setTimeout(()=>{
			console.log('关闭集体舞');
			clearInterval(actionTimer);
			startIndex = 0;
			var allPeople = $('.dance-floor').find('.people-img').length;
			suiDanTimer = setInterval(()=>{
				if(startIndex==allPeople){
					clearInterval(suiDanTimer);
				} else {
					startIndex++;
					$(`.data${startIndex} .people-img`).addClass(actionArr[Math.floor(Math.random()*actionArr.length)]);
				}
			},3000)
		},3000)
	}
}
// 随机舞
function randomDancing(id){
	actionTimer = setInterval(()=>{
		actionArr.forEach((item,index)=>{
			// var chooseIdex = Math.floor(Math.random()*num);
			var haveDan = $(`.data${id} .people-img`).is(`.${item}`);
			if(haveDan){
				$(`.data${id} .people-img`).removeClass(item);
				$(`.data${id} .people-img`).addClass(actionArr[Math.floor(Math.random()*actionArr.length)]);
			}
		})
	},3000)
}
// 随机位移
function moveEv(id){
	var odiv = document.getElementById(`peopleId${id}`);
	var typeMove = Math.floor(Math.random()*4);
	switch (typeMove){
		case 0:
		upMove(odiv);
			break;
		case 1:
		downMove(odiv);
			break;
		case 2:
		leftMove(odiv);
			break;
		case 3:
		rightMove(odiv);
			break;
	}
	
}
// 上
function upMove(obj){
	upTimer = setInterval(()=>{
		if(obj.offsetTop < 100){
			clearInterval(upTimer);
			downMove(obj);
		} else {
			obj.style.top = obj.offsetTop-speed+'px';
		}
	},10)
}
// 下
function downMove(obj){
	downTimer = setInterval(()=>{
		if(obj.offsetTop > 500){
			clearInterval(downTimer);
			upMove(obj);
		} else {
			obj.style.top = obj.offsetTop+speed+'px';
		}
	},10)
}
// 左
function leftMove(obj){
	leftTimer = setInterval(()=>{
		if(obj.offsetLeft > 1500){
			clearInterval(leftTimer);
			rightMove(obj);
		} else {
			obj.style.left = obj.offsetLeft+speed+'px';
		}
	},10)
}
// 左
function rightMove(obj){
	rightTimer = setInterval(()=>{
		if(obj.offsetLeft < 150){
			clearInterval(rightTimer);
			leftMove(obj);
		} else {
			obj.style.left = obj.offsetLeft-speed+'px';
		}
	},10)
}
// 关闭说话
function closeSay(index){
	$(`.data${index} .speak`).addClass("dn");
}
// 开启说话
function sayIng(index){
	$(`.data${index} .speak`).removeClass("dn");
	setTimeout(()=>{
		closeSay(index);
	},6000)
}
// 退出广场
function exitEv(id){
	$(`${id}`).remove();
}
// 进入广场
function peopleObj(name,say,action,move){
	// 开始说话
	sayIng(peopleId);
	randomDancing(peopleId);
	let yspHtml = '';
	let yspData = [
		{
			id:peopleId++,
			top:`${Math.random()*500}px`,
			left:`${Math.random()*1500}px`,
			name:name,//用户名
			say:say,//说话内容
			peopleImg:`people-img0${Math.floor(Math.random()*6+1)}`,//人物图片
			ying_guang:`stick-0${Math.floor(Math.random()*5+1)}`,//荧光棒
			dancin:action,
			peopleMove:move
		}
	]
	$.each(yspData, function (commentIndex, comment) {
			yspHtml += `
			<div id="peopleId${comment.id}" class="dance-people data${comment.id} " style="top: ${comment.top}; left: ${comment.left};">
				<div class="speak ">${comment.say}</div>
				<div class="people-img ${comment.peopleImg} ${comment.dancin}">
					<div class="stick ${comment.ying_guang}"></div>
					<div class="people-name ">${comment.name}</div>
				</div>
			</div>`
	});
	$('.dance-floor').prepend(yspHtml);
	moveEv(peopleId-1);
	// 执行退出事件
	// exitEv(`#peopleId${peopleId-1}`)
}
$(function(){
	// 领舞者随机动效
	setInterval(()=>{
		actionArr.forEach((item,index)=>{
			var haveDan = $(`.leading-dancer`).is(`.${item}`);
			if(haveDan){
				$(`.leading-dancer`).removeClass(item);
				$(`.leading-dancer`).addClass(actionArr[Math.floor(Math.random()*actionArr.length)]);
			}
		})
	},4000)
	// setTimeout(()=>{
	// 	console.log('开始集体舞');
	// 	allDancing();
	// },50000)
	// timer = setInterval(()=>{
		// if(num==10){
		// 	clearInterval(timer);
		// } else {
			// peopleObj(nameArr[nameIndex],sayArr[sayIndex],actionArr[actionIndex],moveArr[moveIndex]);
			// num++;
			$.ajax({
				method: 'get',
				url: 'https://bili.dothis.top/src/fetch.php',
				data: '',
				contentType: false,
				processData: false,
				crossDomain:true,
				success: (res)=>{
					console.log(res);
					if(res.data.dm.length!=0){
						res.data.dm.forEach((item,index)=>{
							var nameIndex = Math.floor(Math.random()*nameArr.length);
							var sayIndex = Math.floor(Math.random()*sayArr.length);
							var actionIndex = Math.floor(Math.random()*actionArr.length);
							var moveIndex = Math.floor(Math.random()*moveArr.length);
							peopleObj(item.uname,item.text,actionArr[actionIndex],moveArr[moveIndex]);
							num++;
						})
						$('.lingname').text(res.data.leader.uname);
						if(res.data.leader.event=='scale-left' || res.data.leader.event=='scale-right' || res.data.leader.event=='scale-bottom-left' || 
						res.data.leader.event=='scale-bottom-right'){
							$('.ground-bg').addClass(res.data.leader.event);
						} else {
							setTimeout(()=>{
								console.log(res.data.leader.event);
								// 显示领舞说话
								$('.speak-pink').text(res.data.leader.text);
								$('.speak-pink').fadeIn();
								setTimeout(()=>{//关闭领舞说话
									$('.speak-pink').fadeOut();
								},3000) 
								allDancing(res.data.leader.event);
							},3000)
						}
					}
				}
			});
		// }
	// },1000)
	
})