package com.beloko.quake2touch;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class Utils {
	static String LOG = "Utils";



	static public void copyFile(InputStream in, OutputStream out) throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while((read = in.read(buffer)) != -1){
			out.write(buffer, 0, read);
		}
		out.close(); 
	}

	public static boolean checkFiles(String basePath, boolean demo)
	{
		File[] files = new File(basePath+ "/baseq2/").listFiles();
		boolean ok=true;
		String[] demo_files = {"pak0.pak"};
		String[] full_files = {"pak0.pak"};

		String[] expected;

		if (demo)
			expected = demo_files;
		else
			expected = full_files;

		if (files!=null)
		{

			for (String e: expected)
			{
				boolean found=false;
				for (File f: files)
				{
					//Log.d(LOG,"check " + f.toString());
					if (f.toString().toLowerCase().endsWith(e))
						found = true;
				}
				if (!found) ok = false;
			}
		}
		else	
			ok =  false;

		return ok;
	}

	
	static public void copyAssets(Context ctx,String dir) {
		AssetManager assetManager = ctx.getAssets();
		String[] files = null;
		try {
			files = assetManager.list("");
		} catch (IOException e) {
			Log.e("tag", "Failed to get asset file list.", e);
		}
		for(String filename : files) {
			if (filename.endsWith("png")){
				InputStream in = null;
				OutputStream out = null;
				Log.d("test","file = " + filename);
				try {
					in = assetManager.open(filename);
					out = new FileOutputStream(dir + "/" + filename);
					copyFile(in, out);
					in.close();
					in = null;
					out.flush();
					out.close();
					out = null;
				} catch(IOException e) {
					Log.e("tag", "Failed to copy asset file: " + filename, e);
				}       
			}
		}
	}
	
	static public String[] creatArgs(String appArgs)
	{
		return appArgs.split(" ");
	}
}
