package com.fca.uconnect.global.util;



import com.fca.uconnect.global.Base64;
import com.fca.uconnect.global.GlobalUconnectControl.KeepAliveMessage;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectMessage;
import com.fca.uconnect.global.GlobalUconnectExtAuth;
import com.fca.uconnect.global.GlobalUconnectExtProvision;
import com.fca.uconnect.global.GlobalUconnectExtRemoteOp;
import com.fca.uconnect.global.GlobalUconnectExtSignature;
import com.fca.uconnect.global.GlobalUconnectExtVehDataAcquisition;
import com.fca.uconnect.global.GlobalUconnectExtWIFIHotSpot;
import com.google.protobuf.ExtensionRegistry;

public class MessageUtil {

	private final static boolean DEBUG = true;
	
	public static String encodeKeepAliveMessage(KeepAliveMessage msg) {
		String encoded = Base64.encodeKreative85(msg.toByteArray());
		if (DEBUG) {
			System.out.println("\n**********************************");
			System.out.println("Encoding Message:\n"+msg.toString());
			//System.out.println("Base85 Encoded String:\n"+encoded);
			System.out.println("**********************************\n");
		}
		return encoded;		
	}

	public static KeepAliveMessage decodeKeepAliveMessageMessage(String msg) {
		
		KeepAliveMessage msgIn = null;
		try {
			byte[] decoded =  Base64.decodeKreative85(msg);				 
			msgIn = KeepAliveMessage.parseFrom(decoded);						
		} catch(Exception ex) {
			System.out.println("Failed Parsing... "+ex);			
		}
		
		if (DEBUG) {
			System.out.println("\n**********************************");
			System.out.println("Decoding String:\n"+msg);
			System.out.println("Decoded Message:\n"+msgIn.toString());
			System.out.println("**********************************\n");
		}
		return msgIn;
	}
	
	public static KeepAliveMessage decodeRawKeepAliveMessageMessage(byte[] msg) {
		
		KeepAliveMessage msgIn = null;
		try {
			msgIn = KeepAliveMessage.parseFrom(msg);						
		} catch(Exception ex) {
			System.out.println("Failed Parsing... "+ex);			
		}
		
		if (DEBUG) {
			System.out.println("\n**********************************");
			System.out.println("Decoded Message:\n"+msgIn.toString());
			System.out.println("**********************************\n");
		}
		return msgIn;
	}

	public static String encodeUconnectMessage(UconnectMessage msg) {
		String encoded = Base64.encodeKreative85(msg.toByteArray());
		
		if (DEBUG) {
			System.out.println("\n**********************************");
			System.out.println("Encoding Message:\n"+msg.toString());
			//System.out.println("Base85 Encoded String:\n"+encoded);
			System.out.println("**********************************\n");
		}
		
		return encoded;
	}	
	public static String getDisplayMessage(UconnectMessage msg) {
		String encoded = Base64.encodeKreative85(msg.toByteArray());
		
		return msg.toString();
	}	
	
	public static UconnectMessage decodeUconnectMessage(String msg) {
			
		UconnectMessage msgIn = null;
		try {
			byte[] decoded =  Base64.decodeKreative85(msg);
			
			ExtensionRegistry registry = ExtensionRegistry.newInstance();

			GlobalUconnectExtAuth.registerAllExtensions(registry);
			GlobalUconnectExtRemoteOp.registerAllExtensions(registry);
			GlobalUconnectExtVehDataAcquisition.registerAllExtensions(registry);
			GlobalUconnectExtProvision.registerAllExtensions(registry);
			GlobalUconnectExtWIFIHotSpot.registerAllExtensions(registry);
			GlobalUconnectExtSignature.registerAllExtensions(registry);


			msgIn = UconnectMessage.parseFrom(decoded,registry);						
		} catch(Exception ex) {
			System.out.println("Failed Parsing... "+ex);	
		}
		
		if (DEBUG) {
			System.out.println("\n**********************************");
			System.out.println("Decoding String:\n"+msg);
			System.out.println("sDecoded Message:\n"+msgIn.toString());
			System.out.println("**********************************\n");
		}

		return msgIn;
	}

	public static UconnectMessage decodeRawUconnectMessage(byte[] msg) {
		
		UconnectMessage msgIn = null;
		try {
//			byte[] decoded =  Base64.decodeKreative85(msg);
			
			ExtensionRegistry registry = ExtensionRegistry.newInstance();

	
			GlobalUconnectExtAuth.registerAllExtensions(registry);
			GlobalUconnectExtRemoteOp.registerAllExtensions(registry);
			GlobalUconnectExtVehDataAcquisition.registerAllExtensions(registry);
			GlobalUconnectExtProvision.registerAllExtensions(registry);
			GlobalUconnectExtWIFIHotSpot.registerAllExtensions(registry);
			GlobalUconnectExtSignature.registerAllExtensions(registry);

			msgIn = UconnectMessage.parseFrom(msg,registry);						
		} catch(Exception ex) {
			System.out.println("Failed Parsing... "+ex);	
		}
		
		if (DEBUG) {
//			System.out.println("\n**********************************");
////			System.out.println("Decoding String:\n"+msg);
//			System.out.println("Decoded Message:\n"+msgIn.toString());
//			System.out.println("**********************************\n");
		}

		return msgIn;
	}	
}
