/*
 * Land Sea Air -- an interactive music game
 * demonstrating the "secret yanni" techique, with key changes
 * design, animation, audio, and code by peter "pdx" drescher
 * twittering machine
 * 
 */
package com.twittering.landseaair;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LSAMenu extends Activity {
	
	FMODaudio mFMODaudio = new FMODaudio();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mFMODaudio.initFMOD();
        
        Button LandButton = (Button)findViewById(R.id.LSAland);
        LandButton.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        		Intent LSAlandIntent = new Intent(LSAMenu.this,LSAland.class);
        		startActivity(LSAlandIntent);
        	}
        });
        
        Button SeaButton = (Button)findViewById(R.id.LSAsea);
        SeaButton.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        		Intent LSAseaIntent = new Intent(LSAMenu.this,LSAsea.class);
        		startActivity(LSAseaIntent);
        	}
        });
        
        Button AirButton = (Button)findViewById(R.id.LSAair);
        AirButton.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        		Intent LSAairIntent = new Intent(LSAMenu.this,LSAair.class);
        		startActivity(LSAairIntent);
        	}
        });        
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add("About LandSeaAir");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent LSAaboutIntent = new Intent(LSAMenu.this,LSAabout.class);
		startActivity(LSAaboutIntent);
		return super.onOptionsItemSelected(item);
	}

	@Override
    protected void onStart() {
    	super.onStart();

    	//if necessary, copy the FMOD files to sdcard
        AssetManager mAssetManager = getAssets();
        String[] FMODfiles = null;
        try {
            FMODfiles = mAssetManager.list("fmod");
        } catch (IOException e) {
            Log.e("LSA LSAMenu", e.getMessage());
        }
        
        //make the fmod directory on the sdcard, if necessary
	    File sdCard = Environment.getExternalStorageDirectory();
	    File FMODdir = new File (sdCard.getAbsolutePath() + "/fmod");
	    if (!FMODdir.exists()) {
	    	try {
		      FMODdir.mkdirs();
		    } catch(Exception e) {
		        Log.e("LSA LSAMenu Exception", e.getMessage());
		    }       
	    }
	    
        //write the files into the fmod directory on the sdcard, if necessary
	    for(int i=0; i<FMODfiles.length; i++) {
		    InputStream in = null;
		    File fileOut = new File(FMODdir, FMODfiles[i]);
	        if (!fileOut.exists()) {
		       try {
		         in = mAssetManager.open("fmod/" + FMODfiles[i], 0);
		         FileOutputStream fout = new FileOutputStream(fileOut);
	     	     copyFile(in, fout);
		         in.close();
		         in = null;
		         fout.flush();
		         fout.close();
		         fout = null;
		       } catch(Exception e) {
		           Log.e("LSA LSAMenu Exception", e.getMessage());
		       }  
	        }
	    }
    }
   
    private 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);
        }
    }

	@Override
    protected void onDestroy() {
    	mFMODaudio.releaseFMOD();
        super.onDestroy();
    }
}