import {timeDifferenceInDaysAndHours,dateFormatter} from '../../UtilitiesFunctions/objectUtilities.js';


    export function createNotificationEntry(data){
        let notificationEntry = {};
        notificationEntry.data = {};
        notificationEntry.alertType = "ticket";
        notificationEntry.header = {action: data.pusherTriggerActionType, ticketType: data.ticketMetadataInfoDto.ticketType};
        //notificationEntry.header = {action: 'FYI', ticketType: 'CWH Missing'};
        notificationEntry.read = false;
        return notificationEntry;
    }

   export function createRowEntryInRequiredFormat(el){
	    let rowEntery = {};
	    rowEntery["tripCode"] = el.metadata["tripCode"]?el.metadata["tripCode"]:'--';

	    let len = rowEntery['tripCode'].length;
	    for(let i=0;i< len ;i++){
	        if(rowEntery['tripCode'][i] === '|'){
	            rowEntery['tripCode'] = rowEntery['tripCode'].substr(0,i) + ' | ' +
	                rowEntery['tripCode'].substr(i+1,rowEntery['tripCode'].length -i-1);
	            i++;
	        }
	    }
	    rowEntery["vehicleNumber"] = el.metadata["vehicleNumber"]?el.metadata["vehicleNumber"]:'--';
	    rowEntery["reason"] = el.metadata["reason"]?el.metadata["reason"]:'--';
	    rowEntery["comment"] = el.metadata["comment"]?el.metadata["comment"]:'--';
        rowEntery["lastUpdatedBy"] = el.lastUpdatedBy;
        rowEntery["lastUpdatedTimestamp"] = el.lastUpdatedTimestamp;
	    let lastUpdateTime = el.lastUpdatedTimestamp;
	    if(lastUpdateTime && lastUpdateTime.iMillis)
	        lastUpdateTime = lastUpdateTime.iMillis;
	    console.log('last update', el, el.lastUpdatedTimestamp, (new Date).getTime(), timeDifferenceInDaysAndHours(el.lastUpdatedTimestamp,(new Date).getTime()));
        rowEntery["raisedBy"] = el.lastUpdatedBy + ((lastUpdateTime)?(' | ' + timeDifferenceInDaysAndHours(lastUpdateTime,(new Date).getTime())):'');
	    return rowEntery;
}

 export function createRowEntryInRequiredFormatForEdit(el){

  var rowEntery = {};
   var elKeys = Object.keys(el);
    elKeys.forEach((type) =>{
      if(type === 'metadata'){
            Object.keys(el.metadata).forEach((key) =>{                    
                  if(el.metadata[key] === "" || !el.metadata[key]){
                   rowEntery[key] = '--';
                  }else{
                   rowEntery[key] = el.metadata[key];
                  } 
                })
            }else{
                rowEntery[type] = el[type];

                if(!rowEntery[type] || rowEntery[type] === ''){
                  rowEntery[type] = '--';
                }
            }
        })

    	rowEntery['createdTimestamp'] = el.createdTimestamp;
    	rowEntery['lastUpdatedTimestamp'] = el.lastUpdatedTimestamp;;
        rowEntery['createdTimestampInEpoch'] = null;
        if(rowEntery['createdTimestamp'] && rowEntery['createdTimestamp'] !== '--'){
          rowEntery['createdTimestampInEpoch'] = rowEntery['createdTimestamp'];
          rowEntery['createdTimestamp'] = timeDifferenceInDaysAndHours(rowEntery['createdTimestamp'],(new Date).getTime());
        }

        if(rowEntery['lastUpdatedTimestamp'] && rowEntery['lastUpdatedTimestamp'] !== '--'){
          rowEntery['lastUpdatedTimestampInEpoch'] = rowEntery['lastUpdatedTimestamp'];
          rowEntery['lastUpdatedTimestamp'] = dateFormatter(rowEntery['lastUpdatedTimestamp'],'D MMM YYYY hh:mm a');
        }

        if(rowEntery['tripCode'] && rowEntery['tripCode']!== '--'){
          var len = rowEntery['tripCode'].length;
          for(var i=0;i< len ;i++){
            if(rowEntery['tripCode'][i] === '|'){
              rowEntery['tripCode'] = rowEntery['tripCode'].substr(0,i) + ' | ' + 
              rowEntery['tripCode'].substr(i+1,rowEntery['tripCode'].length -i-1);  
              i++;
            }
          }
        }
        var vehicleType = rowEntery['vehicleType'];
        if(vehicleType && vehicleType.indexOf("TYPE") !== -1){
          vehicleType = vehicleType.substr(5,vehicleType.length - 5)+"'";
        }

        if(vehicleType && vehicleType.indexOf('REEFER') !== -1){
          let index = vehicleType.indexOf('REEFER');
          vehicleType = vehicleType.substr(0,index-1) + "'R";
        }

        rowEntery['vehicleType'] = vehicleType;

        rowEntery['vehicleNoType'] = rowEntery['vehicleNumber'] + 
      ((rowEntery['vehicleNumber'] && vehicleType) ? ' | ' : '' ) + vehicleType;

      if(!rowEntery['vehicleNoType']){
        rowEntery['vehicleNoType'] = '--';
      }


  return rowEntery;

  }




