var pagenumber = 1; // Menu page ref
var defaultPageTimeOut = 5000; // Timeout of menu pages
var TimeOutRef;
var btn_FRENCH = null;
var btn_ENGLISH = null;
var btn_VIDEO_1 = null;
var btn_VIDEO_2 = null;
var btn_VIDEO_3 = null;
var btn_VIDEO_4 = null;
var btn_VIDEO_5 = null;
var btn_VIDEO_6 = null;
var btn_BACK = null;

function ControlApp() {    	
}

ControlApp.prototype = {
	initilialize: function(ifaces) 
	{	
		
		// Read and setup user's configuration
		this.getApplicationConfiguration().then((config) => 
		{			
			console.log(JSON.stringify(config));
            defaultPageTimeOut = config.timeout;
		}).catch(() => console.error("Erreur lors du chargement de la configuration (config.json)"));
		
				
        this.ifaces = ifaces;  

		// Initialize buttons
        btn_FRENCH = document.getElementById("FRENCH");
		btn_ENGLISH = document.getElementById("ENGLISH");
		btn_VIDEO_1 = document.getElementById("VIDEO_1");
		btn_VIDEO_2 = document.getElementById("VIDEO_2");
		btn_VIDEO_3 = document.getElementById("VIDEO_3");
		btn_VIDEO_4 = document.getElementById("VIDEO_4");
		btn_VIDEO_5 = document.getElementById("VIDEO_5");
		btn_VIDEO_6 = document.getElementById("VIDEO_6");
		btn_BACK = document.getElementById("BACK");
				
		// Initialize button click action
		btn_VIDEO_1.addEventListener("click", () => this.onButtonPlayFolderClick(1));  	
		btn_VIDEO_2.addEventListener("click", () => this.onButtonPlayFolderClick(2));  	
		btn_VIDEO_3.addEventListener("click", () => this.onButtonPlayFolderClick(3));  	
		btn_VIDEO_4.addEventListener("click", () => this.onButtonPlayFolderClick(4));  	
		btn_VIDEO_5.addEventListener("click", () => this.onButtonPlayFolderClick(5));  	
		btn_VIDEO_6.addEventListener("click", () => this.onButtonPlayFolderClick(6));  				
		btn_FRENCH.addEventListener("click", () => this.onButtonPlayFolderClick(101));  	
		btn_ENGLISH.addEventListener("click", () => this.onButtonPlayFolderClick(102));  	
		btn_BACK.addEventListener("click", () => this.onButtonPlayFolderClick(100));
				
		SetButtonStates(); // Set UI states of buttons
		
		// Manage 'player' events
		ifaces.player.addListener(function (e) 
		{
			if (e.detail.type == 'folder-enter') // If enter in a new folder
			{				
				pagenumber = e.detail.folder.number; // Ref of menu page = number of current folder				
				if ((pagenumber == 2) || (pagenumber == 3)) // If on page FRENCH or ENGLISH menu				
				{				
					TimeOutRef = setTimeout(BackToMainMenu, defaultPageTimeOut); // Do a Timeout to exit automaticly to the page FRENCH or ENGLISH menu
				}
				else
				{
					clearTimeout(TimeOutRef); // Clear the timeout
				};	
				SetButtonStates(); // Set UI states of buttons				
			}
		});
		
		// End of the timeout
		function BackToMainMenu() 
		{						
			ifaces.player.playFolder(1); // Back to main menu
		}
		
		// Set UI states of buttons
		function SetButtonStates() 
		{							
			switch (pagenumber)
			{
				case 1:
					btn_FRENCH.disabled = false;
					btn_ENGLISH.disabled = false;
					btn_VIDEO_1.disabled = true;
					btn_VIDEO_2.disabled = true;
					btn_VIDEO_3.disabled = true;
					btn_VIDEO_4.disabled = true;
					btn_VIDEO_5.disabled = true;
					btn_VIDEO_6.disabled = true;
					btn_BACK.disabled = true;
					break;
				case 2:
					btn_FRENCH.disabled = true;
					btn_ENGLISH.disabled = true;
					btn_VIDEO_1.disabled = false;
					btn_VIDEO_2.disabled = false;
					btn_VIDEO_3.disabled = false;
					btn_VIDEO_4.disabled = false;
					btn_VIDEO_5.disabled = false;
					btn_VIDEO_6.disabled = false;
					btn_BACK.disabled = false;					
					break;
				case 3:					
					btn_FRENCH.disabled = true;
					btn_ENGLISH.disabled = true;
					btn_VIDEO_1.disabled = false;
					btn_VIDEO_2.disabled = false;
					btn_VIDEO_3.disabled = false;
					btn_VIDEO_4.disabled = false;
					btn_VIDEO_5.disabled = false;
					btn_VIDEO_6.disabled = false;
					btn_BACK.disabled = false;
					break;
				default:
					btn_FRENCH.disabled = true;
					btn_ENGLISH.disabled = true;
					btn_VIDEO_1.disabled = true;
					btn_VIDEO_2.disabled = true;
					btn_VIDEO_3.disabled = true;
					btn_VIDEO_4.disabled = true;
					btn_VIDEO_5.disabled = true;
					btn_VIDEO_6.disabled = true;
					btn_BACK.disabled = true;	
					break;				
			};
		}				
    },
	    
	// Read configuration file
	getApplicationConfiguration: function() {
        return new Promise(function(resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.addEventListener("readystatechange", function() {
                if(this.readyState === 4) {
                    if (xhr.status === 200) {
                        try {
                            var response = JSON.parse(this.responseText);
                            resolve(response);
                        } catch(e) {
                            reject(e);
                        }
                    } else {
                        reject("Invalid HTTP response status " + xhr.status);
                    }
                }
            });
            xhr.open("GET", "./config.json");
            xhr.send();
        });
    },
	
	// On click any interactive button
	onButtonPlayFolderClick: function(param) {        					
		if (pagenumber == 1) // If on page main menu
		{
			if (param == 101) // If click on French menu
			{
				this.ifaces.player.playFolder(2); // Go to French menu 
			}
			else
			{
				if (param == 102) // If click on English menu
				{	
					this.ifaces.player.playFolder(3); // Go to English menu 
				}
			}
		}
		if (pagenumber == 2) // If on page FRENCH menu
		{
			if (param == 100) // If click on back button
			{
				this.ifaces.player.playFolder(1); // Back to main menu
			}
			else
			{
				this.ifaces.player.playFolder(200 + param);	// Start a french video folder regarding the video selected
			}
		}
		if (pagenumber == 3) // If on page ENGLISH menu
		{
			if (param == 100) // If click on back button
			{
				this.ifaces.player.playFolder(1); // Back to main menu
			}
			else
			{
				this.ifaces.player.playFolder(300 + param);	// Start a french video folder regarding the video selected	
			}
		}
    }			
};

window.addEventListener('load', function() {	
	//var HostPlayer = "10.91.20.98";	
	//IDAL.getRemoteControlInterfaces(HostPlayer, "user", "password")	
	//.then(function(ifaces) {
	//		var app = new ControlApp();
    //       app.initilialize(ifaces);
    //    })	
	
	IDAL.getControlInterfaces()	
        .then(function(ifaces) {
            var app = new ControlApp();
            app.initilialize(ifaces);
        })		
});