package com.example.mobapp_n_media1;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

	public Boolean click = false;
	public Boolean running = false;
	AnimationDrawable colorAnimation;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ImageView colorImage = (ImageView) findViewById(R.id.rocket_image);
		colorImage.setBackgroundResource(R.drawable.animation_sample);
		colorAnimation = (AnimationDrawable) colorImage.getBackground();
		  
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void setOnOff(View v)
	{
		ImageButton img = (ImageButton)findViewById(R.id.imageButton1);
		click = !click;
		if (click)
		{
			img.setImageResource(R.drawable.cake_off);
			MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.partytroete);  
			mPlayer.start();
		}
		else
		{
			img.setImageResource(R.drawable.cake);
			MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.feuerwerk);
			mPlayer.start();
		}
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		  if (event.getAction() == MotionEvent.ACTION_DOWN) {
			  if (!running)
				  colorAnimation.start();
			  else
				  colorAnimation.stop();
			  running = !running;
		    return true;
		  }
		  return super.onTouchEvent(event);
		}

}
