package com.bbc.fmtj.cps.enterprise.eo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestNSBundle {

	static URL urlWithSpaces;
	static URL urlWithNoSpaces;
	
	static String pathStringWithSpaces;
	static String pathStringWithNoSpaces;

	@BeforeClass
	public static void initTestData() throws MalformedURLException {
		urlWithSpaces = new URL("jar:file:/Users/jake/Documents/test%20folder/repository/com/webobjects/JavaFoundation/5.4.2-SNAPSHOT/JavaFoundation-5.4.2-SNAPSHOT.jar!/Resources/Info.plist");
		urlWithNoSpaces = new URL("jar:file:/Users/jake/.m2/repository/com/webobjects/JavaFoundation/5.4.2-SNAPSHOT/JavaFoundation-5.4.2-SNAPSHOT.jar!/Resources/Info.plist");
		
		pathStringWithSpaces = "/Users/jake/Documents/test folder/repository/com/webobjects/JavaFoundation/5.4.2-SNAPSHOT/JavaFoundation-5.4.2-SNAPSHOT.jar";
		pathStringWithNoSpaces = "/Users/jake/.m2/repository/com/webobjects/JavaFoundation/5.4.2-SNAPSHOT/JavaFoundation-5.4.2-SNAPSHOT.jar";		
	}
	
	@Test
	public void testOriginalExtractStringFromURL() throws MalformedURLException {
//		assertEquals(pathStringWithSpaces, __exctractStringFromURL(urlWithSpaces));
		assertEquals(pathStringWithNoSpaces, __exctractStringFromURL(urlWithNoSpaces));
		
//		File fileFromPathWithSpaces = new File(__exctractStringFromURL(urlWithSpaces));
//		assertTrue(fileFromPathWithSpaces.exists());
		
		String path = __exctractStringFromURL(urlWithNoSpaces);		
		File fileFromPathWithNoSpaces = new File(path);
		assertTrue(fileFromPathWithNoSpaces.exists());
	}
	
	@Test
	public void testNewExtractStringFromURL() throws IOException {		
		assertEquals(pathStringWithSpaces, extractPathToJarFileAsStringFromURLToInfoPlist(urlWithSpaces));
		assertEquals(pathStringWithNoSpaces, extractPathToJarFileAsStringFromURLToInfoPlist(urlWithNoSpaces));
		
		File fileFromPathWithSpaces = new File(extractPathToJarFileAsStringFromURLToInfoPlist(urlWithSpaces));
		assertTrue(fileFromPathWithSpaces.exists());
		
		String path = extractPathToJarFileAsStringFromURLToInfoPlist(urlWithNoSpaces);		
		File fileFromPathWithNoSpaces = new File(path);
		assertTrue(fileFromPathWithNoSpaces.exists());
	}
	
    private static final String __exctractStringFromURL(URL anURL) {
        String ResourcesInfoPlist = "Resources/Info.plist";
        String JarResourcesInfoPlist = (new StringBuilder()).append("!/").append(ResourcesInfoPlist).toString();
        String jarEndsWithString = ".jar".concat(JarResourcesInfoPlist);

        String url2Path = null;
        try
        {
            String urlPath = anURL.getPath();
            if(urlPath.endsWith(jarEndsWithString))
            {
                url2Path = urlPath.substring(0, urlPath.length() - JarResourcesInfoPlist.length());
                URL url2 = new URL(url2Path);
                url2Path = url2.getPath();
            }
        }
        catch(Exception urlException) { }
        return url2Path;
    }
    
    private static final String extractPathToJarFileAsStringFromURLToInfoPlist(URL anURL) {
    	String pathToJarFile = null;
    	
    	try {
	    	// trim the jar: from the front, and the !/Resources/Info.plist from the end
	    	String urlToJarFileString = anURL.toString().substring(0 + "jar:".length(), anURL.toString().length() - "!/Resources/Info.plist".length());
	    	
	    	// create a URI using the 'file:/' URL
	    	URI uriToJarFile = new URI(urlToJarFileString);
	    	
	    	// create a File object using the URI
	    	File jarFile = new File(uriToJarFile);
	    	pathToJarFile = jarFile.getCanonicalPath();
    	} catch (Exception e) {
    		// do something sensible
    		e.printStackTrace();
    	}
    	return pathToJarFile;
    }

}
