// Upload de file
const express = require( 'express' );
const glob = require( 'glob' );
const path = require( 'path' );
// Classes
const Referentials = require( '../models/Referentials' );
// Middlewares
const checkHeaders = require( '../middlewares/checkHeaders' );
const isAuthenticated = require( '../middlewares/isAuthenticated' );
const hasAccessrighton = require( '../middlewares/hasAccessrighton' );
const router = express.Router();
/*
 * keylist = list of key at 1st level in clientconf.json separated by _
 * we use header.xworkon
 * To manage AccesRight obkect referentials does not follow the same logic than other object this is why
 */
router.get( '/clientconf/:keylist', checkHeaders, isAuthenticated, ( req, res ) => {
	// retourne liste info (non sensible) du tribeid inside headers.xworkon sur keylist ="key1_key2"
	/*
	if (req.session.header.accessrights.data[ "Alltribeid" ] && req.session.header.accessrights.data[ "Alltribeid" ].referentials.includes('R') ;
	*/
	logger.info( `get clientconf for ${req.session.header.xworkon} on ${req.params.keylist}` )
	let dataref = {}
	if( req.params.keylist.split( '_' )
		.length > 0 ) {
		const ref = Referentials.clientconf( req.session.header.xworkon, req.params.keylist.split( '_' ) )
		if( ref.status == 200 ) {
			dataref = ref.payload.data;
		} else {
			logger.info( "erreur ", ref )
		}
	}
	logger.info( 'envoie en jsonp: dataref' )
	res.jsonp( dataref )
} );
router.get( '/clientconfglob', checkHeaders, isAuthenticated, ( req, res ) => {
	res.jsonp( Referentials.clientconfglob()
		.payload.data );
} );
router.get( '/contentlist/:source', checkHeaders, isAuthenticated,
	( req, res ) => {
		const payload = [];
		logger.info( req.params.source, `${config.tribes}/${req.session.header.xworkon}/referentials/dataManagement/${req.params.source}/*.json` )
		glob.sync( `${config.tribes}/${req.session.header.xworkon}/referentials/dataManagement/${req.params.source}/*.json` )
			.forEach( f => {
				payload.push( path.basename( f, '.json' ) );
			} )
		res.json( payload );
	} );
router.get( '/contentfull/:source/:idref', checkHeaders, isAuthenticated,
	( req, res ) => {
		//only for data and object
		logger.info( 'route referentials get all language' + req.params.source + '-' + req.params.idref );
		const getref = Referentials.getref( true, req.params.source, req.params.idref, req.session.header.xworkon, req.session.header.xlang );
		// Return any status the data if any erreur return empty object
		res.jsonp( getref.payload.data );
	} );
router.get( '/content/:source/:idref', checkHeaders, isAuthenticated,
	( req, res ) => {
		logger.info( 'route referentials get ' + req.params.source + '-' + req.params.idref );
		const getref = Referentials.getref( false, req.params.source, req.params.idref, req.session.header.xworkon, req.session.header.xlang );
		res.jsonp( getref.payload.data );
	} );
// get with no authentification
router.get( '/contentnoauth/:source/:idref', checkHeaders,
	( req, res ) => {
		logger.info( 'route referentials get ' + req.params.source + '-' + req.params.idref );
		// @TODO check access right in clientconf before sending back json file
		const getref = Referentials.getref( false, req.params.source, req.params.idref, req.session.header.xworkon, req.session.header.xlang );
		res.jsonp( getref.payload.data );
	} );
router.get( '/lg', ( req, res ) => {
	logger.info( req.headers[ "accept-language" ] )
	let lg = '??';
	if( req.headers[ "accept-language" ] && req.headers[ "accept-language" ].split( ',' )
		.length > 0 ) {
		lg = req.headers[ "accept-language" ].split( ',' )[ 0 ];
	}
	res.json( { lg } )
} );
router.put( '/content/:source/:idref', checkHeaders, isAuthenticated, ( req, res ) => {
	logger.info( `route put content for ${req.params.idref} that is a ${req.params.source}` );
	const putref = Referentials.putref( req.params.source, req.params.idref, req.session.header.xworkon, req.body )
	return res.status( putref.status )
		.send( { payload: putref.payload } )
} );
//hasAccessrighton( 'referentials', 'U' )
router.get( '/updatefull', checkHeaders, isAuthenticated, hasAccessrighton( 'referentials', 'U' ), ( req, res ) => {
	logger.info( `route get to force update content updatefull is accessrighton` );
	const updtref = Referentials.updatefull( req.session.header.xworkon )
	return res.status( updtref.status )
		.send( { payload: updtref.payload } )
} );
module.exports = router;
