const express = require('express')
const router = express.Router();
const request = require('request');
const httpClient = require('urllib');
const bodyParser = require('body-parser');


const ntb_creds = 'xml_user:9999';


//GET THE STATUS OF THE NODE
router.get('/nodeStatus',(req,res,next)=>{
   res.status(200).json({
       status : 'Service Online'
   });
});


//GET THE PLI OF THE NTB
router.get('/checkPLI/:ntbAddress',(req,res,next)=>{

    const ntbAddress = req.params.ntbAddress;
    const ntb_url = "http://"+ntbAddress+"/XML2";
    var ntb_data = '';
    var xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><command_list><get_all_player_local_info command_id="all local info"/></command_list>'
    const options = {
        headers:{
            'Content-Type':'text/xml'
                },
        digestAuth: ntb_creds,
        content:xml_data,
		    method : 'POST'
    };
    const responseHandler = (error,data,res)=>{
        if(error){
            console.log(error);
        }else {
		console.log(res.statusCode);
        console.log(res.headers);
        console.log(data.toString('utf-8'));	
		}        
    }
    var checkers = httpClient.request(ntb_url,options,responseHandler);
    res.status(200).json({
        message: 'Successfully Submitted Command.'
    });
});

//Change PLI OF THE NTB
router.get('/updatePLI/:pliGroup/:setPLI/:ntbAddress',(req,res,next)=>{
  const pliGroup = req.params.pliGroup;
  const setPLI = req.params.setPLI;
  const ntbAddress = req.params.ntbAddress;
  const user_credential = ntb_creds;
  const ntb_url = "http://"+ntbAddress+"/XML2";
  var xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><command_list><set_player_local_info command_id="cmd_local" name="'+pliGroup+'"><data_player_local_info><value>'+setPLI+'</value></data_player_local_info></set_player_local_info></command_list>';

  const options = {
    method: 'POST',
    rejectUnauthorized : false,
    digestAuth : user_credential,
    content: xml_data,
    headers:{'Content-Type':'text/xml'}
  };

  const responseHandler = (error, data, res)=>{
    if(error){
      console.log(error);
    }else{
		console.log(res.statusCode);
		console.log(res.headers);
		console.log(data.toString('utf-8'));
	}
  };

  // FOR PRODUCTION
  httpClient.request(ntb_url,options,responseHandler);
  //FOR TESTING
  //httpClient.request(url_address,options,responseHandler);

    res.status(200).json({
        message: 'Successfully Submitted PLI Change Command.',
    });

});


// CHANGE THE RENDEZVOUS POINT OF THE NTB
router.get('/toRendezvous/:rendezvous/:ntbAddress',(req,res,next)=>{
    const rendez = req.params.rendezvous;
    const ntbAddress = req.params.ntbAddress;
    const user_credential = ntb_creds;
    const ntb_url = "http://"+ntbAddress+"/XML2";
    var xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><command_list><advance_to rendezvous_name="'+rendez+'" command_id="1"/></command_list>';


    const options = {
        method : 'POST',
        rejectUnauthorized : false,
        digestAuth: user_credential,
        content: xml_data,
        headers:{
            'Content-Type':'text/xml'
        }
    };

    const responseHandler = (error,data,res)=>{
        if(error){
            console.log(error);
        }else {
			  console.log(res.statusCode);
			  console.log(res.headers);
              console.log(data.toString('utf-8'));
		}
    };

    // FOR PRODUCTION
    httpClient.request(ntb_url,options,responseHandler);
    //FOR TESTING
    //httpClient.request(url_address,options,responseHandler);

      res.status(200).json({
          message: 'Successfully Submitted Rendezvous Change Command.',
      });

});






module.exports = router;
