package credit.android.dg;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

public class BasicScreenPeyote extends Activity{
	
	private ImageView MoneyButton;
	private ImageView EnvelopeButton;
	private ImageView ProfileButton;
	private ImageView FeedbackButton;
	private ImageView MapButton;
	private ImageView TopButton;
	
	private ImageView Offers_1_icon;
	
	private TextView Cash;
	
	private TableRow[] Offers;
	
	private int mDrugIndex;
	private int mDrugsAvailable;	
	
	private float mTouchStartX, mTouchStartY, mTouchEndX, mTouchEndY;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);	
		
		setContentView(R.layout.basic_screen_peyote);	
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header);
		((TextView) findViewById(R.id.header_text)).setText("test");
		
		MoneyButton = (ImageView) findViewById(R.id.money_button);
		EnvelopeButton = (ImageView) findViewById(R.id.envelope_button);
		ProfileButton = (ImageView) findViewById(R.id.profile_button);
		FeedbackButton = (ImageView) findViewById(R.id.feedback_button);
		MapButton = (ImageView) findViewById(R.id.map_button);
		TopButton = (ImageView) findViewById(R.id.top_button);
		Cash = (TextView) findViewById(R.id.cash);
		
		getPlayersIcons();		
		setRowListeners();
		setButtons();	
		resolveExtras(savedInstanceState);				
	}			

	@Override
	public boolean onTouchEvent(MotionEvent event) {	
		float x = event.getX();
        float y = event.getY();
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchStartX = x;
                mTouchStartY = y;
                break;
            case MotionEvent.ACTION_UP:
            	mTouchEndX = x;
                mTouchEndY = y;
                doSlide();
                break;
        }        
		return true;
	}
	
	public void resolveExtras(Bundle bundle){
		Bundle extras = getIntent().getExtras();
		mDrugIndex = bundle != null ? bundle.getInt(BasicScreen.CURRENT_DRUG) : 0;
		mDrugsAvailable = bundle != null ? bundle.getInt(BasicScreen.AVAILABLE_DRUGS) : 0;
		if (mDrugIndex == 0){			
			mDrugIndex = extras != null ? extras.getInt(BasicScreen.CURRENT_DRUG) : 0;
		}
		if (mDrugsAvailable == 0){
			mDrugsAvailable = extras != null ? extras.getInt(BasicScreen.AVAILABLE_DRUGS) : 0;
		}
	}

	public void getPlayersIcons(){
		Offers_1_icon = (ImageView) findViewById(R.id.offer_0_icon);
		Offers_1_icon.setImageResource(R.drawable.weed_icon_f);
	}
	
	public void setRowListeners(){
		Offers = new TableRow[5];			
		
		Offers[0] = (TableRow) findViewById(R.id.peyote_body_0);
		Offers[1] = (TableRow) findViewById(R.id.peyote_body_1);
		Offers[2] = (TableRow) findViewById(R.id.peyote_body_2);
		Offers[3] = (TableRow) findViewById(R.id.peyote_body_3);
		Offers[4] = (TableRow) findViewById(R.id.peyote_body_4);
		
		Offers[0].setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				doChangePrice();
			}
		});		
		Offers[1].setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				doPurchase();
			}
		});		
		Offers[2].setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				doPurchase();
			}
		});		
		Offers[3].setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				doPurchase();
			}
		});
		Offers[4].setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				doPurchase();
			}
		});
	}
	
	public void setButtons(){					
		MoneyButton.setOnClickListener(new View.OnClickListener() {        
        	public void onClick(View view){        		
        		doMoney();}
        });		
		EnvelopeButton.setOnClickListener(new View.OnClickListener() {        	
        	public void onClick(View view){        		
        		doMessage();}
        });		
		ProfileButton.setOnClickListener(new View.OnClickListener() {        	
        	public void onClick(View view){        		
        		doProfile();}
        });		
		FeedbackButton.setOnClickListener(new View.OnClickListener() {        
        	public void onClick(View view){        		
        		doFeedback();}
        });
		MapButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){				
				doMap();
			}
		});
		TopButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){				
				doTop();
			}
		});
		Cash.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){				
				doCash();
			}
		});
	}
	
	public void doSlide(){
		if (Math.abs(mTouchStartX - mTouchEndX) > 60 && Math.abs(mTouchStartY - mTouchEndY) < 20){
			Intent i = new Intent(this, Constants.SCREEN_FLOW[(++mDrugIndex)%mDrugsAvailable]);
			i.putExtra(BasicScreen.AVAILABLE_DRUGS, mDrugsAvailable);
			i.putExtra(BasicScreen.CURRENT_DRUG, mDrugIndex);
			startActivity(i);
		}
	}
	
	public void doChangePrice(){
		Intent i = new Intent(this,ChangePrice.class);
		startActivity(i);
	}
	
	public void doPurchase(){
		Intent i = new Intent(this,Purchase.class);
		startActivity(i);
	}
	
	public void doProfile(){
		Intent i = new Intent(this, Profile.class);
		startActivity(i);
	}
	
	public void doMoney(){
		Intent i = new Intent(this, Money.class);
		startActivity(i);
	}
	
	public void doFeedback(){
		Intent i = new Intent(this, Feedback.class);
		startActivity(i);
	}
	
	public void doMessage(){
		Intent i = new Intent(this, Message.class);
		startActivity(i);
	}
	public void doTop(){
		Intent i = new Intent(this, Top.class);
		startActivity(i);
	}
	
	public void doCash(){
		Intent i = new Intent(this, Cash.class);
		startActivity(i);
	}
	
	public void doMap(){
		Intent i = new Intent(this, Map.class);
		startActivity(i);
	}
}
