package com.dubiousoft.sakana;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

public class GameScreenView extends FrameLayout 
	implements OnTouchListener
{
	private final NativeSakana m_nativeLayer;
	private ScaleGestureDetector m_scaleDetector;
	private int m_activePointer;
	private Point m_previousMovePoint;
	private int m_score;

	public GameScreenView(Context context, NativeSakana nativeLayer) {
		this(context, null, nativeLayer);
	}
	
	public GameScreenView(Context context, AttributeSet attrs, NativeSakana nativeLayer) {
		this(context, attrs, R.style.AppTheme, nativeLayer);
	}
	
	public GameScreenView(Context context, AttributeSet attrs, int defStyle, NativeSakana nativeLayer) {
		super(context, attrs, defStyle);
		
		m_nativeLayer = nativeLayer;
		
		LayoutInflater inflator = LayoutInflater.from(context);
		inflator.inflate( R.layout.game_screen_view, this, true );

		Display display = ((GameScreenActivity)context).getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize( size );
		
		ReticleView reticule = (ReticleView)findViewById( R.id.reticule );
		// Hack: 75 is half the size of the reticle
		reticule.setX( size.x/2 - 75 );
		reticule.setY( size.y/2 - 75 );
		
		SliderView slider = (SliderView)findViewById( R.id.slider );
		LayoutParams layoutParams = new LayoutParams( 150, size.y - 150 );
		slider.setLayoutParams( layoutParams );
		slider.setX( size.x - 150 );
		slider.setY( 0 );		
		
		m_scaleDetector = new ScaleGestureDetector( context, new ScaleListener() );
		setOnTouchListener( this );
		
		final int FIRE_BTN_SIZE = 150;
		ImageButton fireBtn = (ImageButton)findViewById( R.id.fire_btn );
		layoutParams = new LayoutParams( FIRE_BTN_SIZE, FIRE_BTN_SIZE );
		fireBtn.setLayoutParams( layoutParams );
		fireBtn.setX( size.x - FIRE_BTN_SIZE );
		fireBtn.setY( size.y - FIRE_BTN_SIZE );
		fireBtn.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				onFireBtnClicked();
			}} 
		);
		
		WeaponSelectionView weaponSelection = (WeaponSelectionView)findViewById( R.id.weapon_selection );
		weaponSelection.readWeapon( "Weapons/SingleShot.xml" );
		weaponSelection.readWeapon( "Weapons/Shotgun.xml" );
		weaponSelection.setNativeLayer( m_nativeLayer );
		weaponSelection.setSlider( slider );
		
		Typeface typeface = Typeface.createFromAsset( getContext().getAssets(), "EraserRegular.ttf" ); 
		TextView scoreText = (TextView)findViewById( R.id.score_text ); 
		scoreText.setTypeface( typeface );
	}
	
	public int getScore() {
		return m_score;
	}

	public void onUiUpdate() {
		int score = m_nativeLayer.Score();
		if (score != m_score) {
			m_score = score;
			TextView scoreText = (TextView)findViewById( R.id.score_text ); 
			scoreText.setText( "Score: " + Integer.toString( m_score ) );
		}
		ReticleView reticule = (ReticleView)findViewById( R.id.reticule );
		reticule.onUiUpdate();
	}
	
	public void onFireBtnClicked() {
		Utils.LOG( "Fire!!" );
		ReticleView reticule = (ReticleView)findViewById( R.id.reticule );
		SliderView slider = (SliderView)findViewById( R.id.slider );
		m_nativeLayer.FireBullet( getContext().getAssets(), (1-slider.getSliderPos()), reticule.getPosition() );
	}
	
	@Override
	public boolean onTouch(View view, MotionEvent event) {
		m_scaleDetector.onTouchEvent( event );
		if (m_scaleDetector.isInProgress()) {
			return true;
		}
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			m_activePointer = event.getPointerId(0);
	        m_previousMovePoint = new Point( (int)event.getX(0), (int)event.getY(0) );	
			break;
		case MotionEvent.ACTION_UP:
			break;
		case MotionEvent.ACTION_MOVE:
			final int pointerIndex = event.findPointerIndex(m_activePointer);
			if (pointerIndex == -1) {
				m_activePointer = event.getPointerId(0);
		        m_previousMovePoint = new Point( (int)event.getX(0), (int)event.getY(0) );
		        break;
			}
	        final Point movePoint = new Point( (int)event.getX(pointerIndex), (int)event.getY(pointerIndex) );
	        final Point delta = new Point( m_previousMovePoint.x - movePoint.x, m_previousMovePoint.y - movePoint.y ); 
	        m_nativeLayer.MoveCamera( delta );
	        m_previousMovePoint = movePoint;
			break;
		}
		
		return true;
	}	
	
	private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
		@Override
	    public boolean onScaleBegin(ScaleGestureDetector detector) {
			m_activePointer = -1;
	        return true;
		}
		
		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			final int span = (int)((detector.getCurrentSpan() - detector.getPreviousSpan())/(2*detector.getScaleFactor()));
			m_nativeLayer.ZoomCamera( span );
			return true;
	    }	
	}	
}
