#!/usr/bin/env js


(() => {
    var timer = null, count = 10, msgArea = null;
    var answer;
    function countdown() {
	count--;
	if (count==0) {
	    alert("どーーーーーーーん");
	} else {
	    msgArea.innerText = `爆発まであと ${count}秒`
	    timer = setTimeout(countdown, 1000);
	}
    }
    function stop(e) {
	console.log(`target = ${e.target}`)
	if (e.target.innerText == answer+1) {
	    clearTimeout(timer);
	    document.body.style.background = "#e99";
	    msgArea.innerText = "この世は安全";
	    msgArea.style.fontSize = "200%";
	    msgArea.style.textAlign = "center";
	} else {
	    //ハズレの処理
	    e.target.style.background = "blue";
	    e.target.textContent = `ハズレ\nだよ`;
	}
    }
    function init() {
	let divs = document.querySelectorAll("div");
	msgArea = document.getElementById("message");
	if (divs.length == 0) {
	    alert("divがないよ!"); return;
	}
	answer = Math.floor(Math.random()*10)
	console.log(`当たりは${answer+1}だよ`)
	for (let i of divs) {
	    i.addEventListener("click", stop);
	}
	timer = setTimeout(countdown, 1000);
    };
    document.addEventListener("DOMContentLoaded", init);
})();
