package doodoo.toyo.magicforest;

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

public class BearActivity extends Activity {
	
	private static final String LOGTAG = "BearActivity";
	
	ImageView imageView1,imageView2;
	
	//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_bear);
		
		Log.d(LOGTAG, "onCreate" );	
		
//moving image with touch events---------------------------------------------------------------------------
		imageView1 = (ImageView)findViewById(R.id.imageView3);

		imageView1.setOnTouchListener(new OnTouchListener() {

            @Override
			public boolean onTouch(View v, MotionEvent event) {
               
                int eid = event.getAction();
                switch (eid) {
                case MotionEvent.ACTION_MOVE:

                    RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) imageView1.getLayoutParams();
                    int x = (int) event.getRawX();
                    int y = (int) event.getRawY();                  
                    mParams.leftMargin = x-25;     
                    mParams.topMargin = y-50;
                    imageView1.setLayoutParams(mParams);
                    break;
                default:
                    break;
                }
                return true;
            }
        });
//end moving image with touch events--------------------------------------------------------------------------------

//moving image with touch events---------------------------------------------------------------------------
				imageView2 = (ImageView)findViewById(R.id.imageView4);

				imageView2.setOnTouchListener(new OnTouchListener() {

		            @Override
					public boolean onTouch(View v, MotionEvent event) {
		               
		                int eid = event.getAction();
		                switch (eid) {
		                case MotionEvent.ACTION_MOVE:

		                    RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) imageView2.getLayoutParams();
		                    int x = (int) event.getRawX();
		                    int y = (int) event.getRawY();
		                    mParams.leftMargin = x-25;
		                    mParams.topMargin = y-50;
		                    imageView2.setLayoutParams(mParams);
		                    break;
		                default:
		                    break;
		                }
		                return true;
		            }
		        });
//end moving image with touch events--------------------------------------------------------------------------------		
		
		final ImageView coada = (ImageView) findViewById(R.id.imageView2);				  		//ImageView coada
		final Animation coadaAnimation = AnimationUtils.loadAnimation(this, R.anim.coada);		//Animation coada
		coada.startAnimation(coadaAnimation);
		
		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.bearlaughing);
		 final MediaPlayer mp2=MediaPlayer.create(getApplicationContext(), R.raw.beariloveyou);
		 final MediaPlayer mp3=MediaPlayer.create(getApplicationContext(), R.raw.bearpretty);
		 final MediaPlayer mp4=MediaPlayer.create(getApplicationContext(), R.raw.sunetfluturi);
		 
		 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 buttonPlayVideo2 = (Button)findViewById(R.id.buttonflu);
        
        getWindow().setFormat(PixelFormat.UNKNOWN);
       
        //displays a video file   
        VideoView mVideoView2 = (VideoView)findViewById(R.id.videoviewflu);
        
        String uriPath2 = "android.resource://doodoo.toyo.magicforest/"+R.raw.butterfliesound;
		Uri uri2 = Uri.parse(uriPath2);
        mVideoView2.setVideoURI(uri2);
        mVideoView2.requestFocus();
        mVideoView2.start();
                
        buttonPlayVideo2.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				 mp4.start();	//sunet
				 VideoView mVideoView2 = (VideoView)findViewById(R.id.videoviewflu);
			       // VideoView mVideoView = new VideoView(this);
			        String uriPath = "android.resource://doodoo.toyo.magicforest/"+R.raw.butterfliesound;
					Uri uri2 = Uri.parse(uriPath);
			        mVideoView2.setVideoURI(uri2);
			        mVideoView2.requestFocus();
			        mVideoView2.start();				
			}});
//end video----------------------------------------------------------------------------------
	}
	
	//animatii butoane
    private void animate(){
        ImageView imgView = (ImageView)findViewById(R.id.imageView1);
        imgView.setVisibility(View.VISIBLE);
        imgView.setBackgroundResource(R.drawable.doo_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.doo_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.doo_animationdance);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
        if (frameAnimation.isRunning()){
            frameAnimation.stop();
        }
        else{
            frameAnimation.stop();
            frameAnimation.start();
        }
    }
    
  //fereastra monkey---------------------------------------
    public void gotoMonkey(View v){
    	Intent intent = new Intent(this, MonkeyActivity.class);
        startActivity(intent);
    }
    
    //fereastra magic forest---------------------------------------
    public void gotoMagicForest(View v){
    	Intent intent = new Intent(this, MagicForestActivity.class);
        startActivity(intent);
    }
	
	//click logo = home (functioneza la android4)
	@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) {}
}