package doodoo.toyo.magicforest;

import android.app.Activity;
import gif.decoder.GifRun;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.VideoView;

public class MonkeyActivity extends Activity {
	
	private static final String LOGTAG = "MonkeyActivity";
	
	//variabile pentru video
	MediaPlayer mediaPlayer;
	SurfaceView surfaceView;
	SurfaceHolder surfaceHolder;
	boolean pausing = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_monkey);
		
		 Log.d(LOGTAG, "onCreate" );	
		
		//gif animat
        SurfaceView v = (SurfaceView) findViewById(R.id.surfaceView1);
        GifRun w = new  GifRun();
		w.LoadGiff(v, this, R.drawable.my);
		
		final ImageView magic = (ImageView) findViewById(R.id.imageView5);				  		//ImageView text
		final Animation magicAnimation = AnimationUtils.loadAnimation(this, R.anim.rotatext);	//Animation text
		magic.startAnimation(magicAnimation);

		//zone
		Button button1 = (Button) findViewById(R.id.button1);
		Button button2 = (Button) findViewById(R.id.button2);
		Button button3 = (Button) findViewById(R.id.button3);
		
		//sounds
		 final MediaPlayer mp=MediaPlayer.create(getApplicationContext(), R.raw.monkeyuuaa);
		 final MediaPlayer mp2=MediaPlayer.create(getApplicationContext(), R.raw.monkeykiss);
		 final MediaPlayer mp3=MediaPlayer.create(getApplicationContext(), R.raw.monkeydance);
		 final MediaPlayer mp4=MediaPlayer.create(getApplicationContext(), R.raw.butterflysound);

		 OnClickListener elem = new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				switch (v.getId()) {
				case R.id.button1:
					animate();
		        	mp.start();	 
		        	 break;
		         case R.id.button2:
		        	 animate2();
			         mp2.start();
			         break;
		         case R.id.button3:
		        	 animate3();
			         mp3.start();
			         break;
		         }				
			}	
	    };
	    
	    button1.setOnClickListener(elem);
	    button2.setOnClickListener(elem);
	    button3.setOnClickListener(elem);
	    
//start video--------------------------------------------------------------------------------
	    Button buttonPlayVideo = (Button)findViewById(R.id.button5);
	           
	           getWindow().setFormat(PixelFormat.UNKNOWN);
	          
	           //displays a video file   
	           VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
	           
	           String uriPath = "android.resource://doodoo.toyo.magicforest/"+R.raw.butterfly;
	   		Uri uri = Uri.parse(uriPath);
	           mVideoView.setVideoURI(uri);
	           mVideoView.requestFocus();
	           mVideoView.start();
	                   
	           buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
	   			@Override
	   			public void onClick(View v) {
	   				mp4.start();	//sunet
	   				 VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
	   			       // VideoView mVideoView = new VideoView(this);
	   			        String uriPath = "android.resource://doodoo.toyo.magicforest/"+R.raw.butterfly;
	   					Uri uri = Uri.parse(uriPath);
	   			        mVideoView.setVideoURI(uri);
	   			        mVideoView.requestFocus();
	   			        mVideoView.start();				
	   			}});
//end video----------------------------------------------------------------------------------
	   
	}
	
	//animatiile
    private void animate(){
        ImageView imgView = (ImageView)findViewById(R.id.imageView1);
        imgView.setVisibility(View.VISIBLE);
        imgView.setBackgroundResource(R.drawable.monkey_animationbody);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
        if (frameAnimation.isRunning()){
            frameAnimation.stop();
        }
        else{
            frameAnimation.stop();
            frameAnimation.start();
        }
    }
    
    private void animate2(){
        ImageView imgView = (ImageView)findViewById(R.id.imageView1);
        imgView.setVisibility(View.VISIBLE);
        imgView.setBackgroundResource(R.drawable.monkey_animationhead);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
        if (frameAnimation.isRunning()){
            frameAnimation.stop();
        }
        else{
            frameAnimation.stop();
            frameAnimation.start();
        }
    }
    
    private void animate3(){
        ImageView imgView = (ImageView)findViewById(R.id.imageView1);
        imgView.setVisibility(View.VISIBLE);
        imgView.setBackgroundResource(R.drawable.monkey_animationdance);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
        if (frameAnimation.isRunning()){
            frameAnimation.stop();
        }
        else{
            frameAnimation.stop();
            frameAnimation.start();
        }
    }
	
	//fereastra bear----------------------------------------
    public void gotoBear(View v){
    	Intent intent = new Intent(this, BearActivity.class);
        startActivity(intent);
    }
    
    //fereastra magic forest---------------------------------------
    public void gotoMagicForest(View v){
    	Intent intent = new Intent(this, MagicForestActivity.class);
        startActivity(intent);
    }
	
	//click logo = home
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {	
			finish();
		}
		return super.onOptionsItemSelected(item);
	}
	
    
//lifecycle---------------------------------------------------
@Override
protected void onResume() {
	super.onResume();
	Log.d(LOGTAG, "onResume");
}

@Override
protected void onStart() {
	super.onStart();
	Log.d(LOGTAG, "onStart");
}

@Override
protected void onPause() {
	super.onPause();
	Log.d(LOGTAG, "onPause");
}

@Override
protected void onStop() {
	super.onStop();
	Log.d(LOGTAG, "onStop");
}

@Override
protected void onDestroy() {
	super.onDestroy();
	Log.d(LOGTAG, "onDestroy");
}

@Override
protected void onRestart() {
	super.onRestart();
 	Log.d(LOGTAG, "onRestart");
}
//end lifecycle

	
	//video
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
	public void surfaceCreated(SurfaceHolder holder) {}
	public void surfaceDestroyed(SurfaceHolder holder) {}
}
