// app management sidebar and navbar
/*
Load into pwa.state.data.tpl[name] templates content
appsidebarmenu.mustache  side menu  sbgroupmenu:[list of submenu]
apptopbarmenu.mustache top menu  menuprofil:[ list of submenu]
apptopbarnotification.mustache show a dropdown list if withnotification is true
apptopbarmessage.mustache show a dropdown list if withmessage is true

Load list of public data into pwa.state.data.menu
sidebar
topbar
Then add specific menu in based on login list into
pwa.state.data.login.user.ACCESSRIGHTS.app[tribeid:website]
each name starting by sidebar is added into sidebar.subgroupmenu
each name starting by topbar is added into topbar.menuprofil


*/
var pwa = pwa || {};
pwa.app = {};
pwa.app.init = async () => {
	//Load template in pwa.state.data.tpl
	logger.info( 'app.init()' );
	const tpllist = {};
	[ 'appsidebarmenu', 'apptopbarmenu', 'apptopbarnotification', 'apptopbarmessage' ].forEach( c => {
		tpllist[ c ] = `static/components/appmesa/${c}.mustache`
	} );
	// add other generic public template, carefull name have to be unique
	tpllist[ 'notiflist' ] = `static/components/notification/notiflist.mustache`;
	//	tpllist[ 'msglist' ] = `static/components/messagerie/msglist.mustache`;

	tpllist[ 'verticaltab' ] = `static/components/verticaltab/verticaltab.mustache`;

	await pwa.state.loadfile( tpllist, "tpl" );
	await pwa.app.getcontent();
	//get menu depending of user profile: tribeid:app
};
pwa.app.getcontent = async () => {
	const menubase = {};
	let menu = [ 'sidebar', 'topbar' ];
	//check if authentify and add specific menu
	if( pwa.state.data.login && pwa.state.data.login.isAuthenticated ) {
		//Get personnal menu user.ACCESSRIGHTS={app:{		"tribeid:website":{sidebar,top}
		const appname = `${pwa.state.data.ctx.tribeid}:${pwa.state.data.ctx.website}`
		//logger.info( 'with ', appname )
		menu = menu.concat( pwa.state.data.login.user.ACCESSRIGHTS.app[ appname ] )
	}
	//logger.info( 'update pwa.state.data.menu with ', menu )
	menu.forEach( c => { menubase[ c ] = `static/data/${c}_${pwa.state.data.ctx.lang}.json` } );
	await pwa.state.loadfile( menubase, 'menu' );
	pwa.app.loadsidebarmenu( menu.filter( m => m.includes( 'sidebar' ) ) )
	pwa.app.loadtopmenu( menu.filter( m => m.includes( 'topbar' ) ) );
	//active les icones svg de feather
	feather.replace();
	//active scroll presentation + sidebar animation
	pwa.app.simplebar();
	pwa.app.clickactive();
}
pwa.app.loadsidebarmenu = ( list ) => {
	//logger.info( 'list de menu sidebar', list )
	const data = pwa.state.data.menu.sidebar
	for( let m of list ) {
		if( m != 'sidebar' ) {
			logger.info( m )
			data.sbgroupmenu = data.sbgroupmenu.concat( pwa.state.data.menu[ m ] )
		}
	}
	//logger.info( data )
	document.querySelector( "#sidebar" )
		.innerHTML = Mustache.render( pwa.state.data.tpl.appsidebarmenu, data )
}
pwa.app.loadtopmenu = ( list ) => {
	const data = pwa.state.data.menu.topbar
	for( let m of list ) {
		if( m != 'topbar' ) {
			data.menuprofil = data.menuprofil.concat( pwa.state.data.menu[ m ] )
		}
	}
	//update date from login if authenticated
	if( pwa.state.data.login.isAuthenticated ) {
		//remove the login item from menuprofil
		data.menuprofil.shift();
		data.name = pwa.state.data.login.user.NAME;
		data.avatarimg = pwa.state.data.login.user.AVATARIMG;
		// get notification  / message if exist
		//const notifsrc= `${pwa.urlbackauth}/Tribes/logs/`;
		if( pwa.state.data.menu.topbar.withnotification ) {
			pwa.notification.update();
		}
		if( pwa.state.data.menu.topbar.withmessage ) {
			//pwa.message.update( );
		}
	}
	//logger.info( 'topbar data', data.menuprofil );
	document.querySelector( "#navbar" )
		.innerHTML = Mustache.render( pwa.state.data.tpl.apptopbarmenu, data )
}
pwa.app.simplebar = () => {
	const simpleBarElement = document.getElementsByClassName( "js-simplebar" )[ 0 ];

	if( simpleBarElement ) {
		/* Initialize simplebar */
		new SimpleBar( document.getElementsByClassName( "js-simplebar" )[ 0 ] )

		const sidebarElement = document.getElementsByClassName( "sidebar" )[ 0 ];
		const sidebarToggleElement = document.getElementsByClassName( "sidebar-toggle" )[ 0 ];

		sidebarToggleElement.addEventListener( "click", () => {
			sidebarElement.classList.toggle( "collapsed" );

			sidebarElement.addEventListener( "transitionend", () => {
				window.dispatchEvent( new Event( "resize" ) );
			} );
		} );
	}
}
pwa.app.clickactive = () => {
	const cleanactive = () => {
		const el = document.querySelectorAll( '.sidebar-item' )
		for( var i = 0; i < el.length; i++ ) {
			//logger.info( 'clean', el[ i ].classList )
			el[ i ].classList.remove( 'active' );
		}
	}
	document.addEventListener( "click", ( e ) => {
		logger.info( 'click', e );
		if( e.target.classList.contains( 'sidebar-link' ) ) {
			cleanactive();
			e.target.closest( '.sidebar-item' )
				.classList.add( 'active' );
			// remonte au menu au dessus si existe
			e.target.closest( '.sidebar-item' )
				.closest( '.sidebar-item' )
				.classList.add( 'active' );
		}
	} );
	// If enter run the research
	document.getElementById( 'globalsearch' )
		.addEventListener( 'keypress', ( e ) => {
			if( e.keyCode == 13 ) {
				pwa.search.req( 'globalsearch' );
				e.preventDefault();
			}
		} )
};
