package com.sabirjan.android;

import com.sabirjan.Dialog.CustomizeDialog;
import com.sabirjan.HanziCode.HuoquPinYin;
import com.sabirjan.HanziCode.Huoqu_HanZi_Code;
import com.sabirjan.android.R;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.ViewFlipper;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class BilKan extends TabActivity{
	
	private TabHost mTabHost;
	private static final int SWIPE_MIN_DISTANCE = 120; 
	private static final int SWIPE_MAX_OFF_PATH = 250; 
	private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
	

	View.OnTouchListener gestureListener; 
	
	
	private Animation slideLeftIn; 
	private Animation slideLeftOut; 
	private Animation slideRightIn; 
	private Animation slideRightOut; 
	private ViewFlipper viewFlipper; 

	int currentView = 0; 
	private static int maxTabIndex = 4; 
	
	GestureDetector gestureDetector;
	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		setupTabHost();
	
        
        
		//mTabHost.getTabWidget().setDividerDrawable(R.drawable.minitab_default);
		Intent intent = new Intent().setClass(this, ActiviyTab_1.class);
		TextView t=new TextView(this);
		setupTab(t, " ﺑﺎﺵ ﺑﻪﺕ",R.drawable.ic_tab_artists,intent);
		intent = new Intent().setClass(this, ActiviyTab_2.class);
		setupTab(new TextView(this), "ﻛﻮﺩ ﻧﻮﻣﯘﺭﻻﺭ",R.drawable.code,intent);
		intent = new Intent().setClass(this, ActiviyTab_3.class);
		setupTab(new TextView(this), "ﻗﺎﺗﻨﺎﺵ",R.drawable.bus,intent);
		intent = new Intent().setClass(this, ActiviyTab_4.class);
		setupTab(new TextView(this), " ﺋﻮﻳﯘﻥ",R.drawable.study,intent);
		intent = new Intent().setClass(this, ActiviyTab_5.class);
		setupTab(new TextView(this), "ﺗﻪﯕﺸﻪﺵ",R.drawable.ic_tab_songs,intent);

        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.alpha_scale_in); 
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.alpha_scale_translate_in); 
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.push_right_in); 
        slideRightOut = AnimationUtils.loadAnimation(this,R.anim.push_right_out); 
     
        gestureListener = new View.OnTouchListener() { 
        	public boolean onTouch(View v, MotionEvent event) { 
        	if (gestureDetector.onTouchEvent(event)) { 
        	return true; 
        	} 
        	return false; 
        	} 
        	}; 
        gestureDetector = new GestureDetector(new MyGestureDetector()); 
       
        
    
	}
	 
    class MyGestureDetector extends SimpleOnGestureListener {

    	
    	
    	
    	
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
				float velocityY) { 
				
				try { 
				if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
				return false; 
				// right to left swipe 
				if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
				&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
		
				if (currentView == maxTabIndex) { 
				currentView = 0; 
				} else { 
				currentView++; 
				} 
				mTabHost.setCurrentTab(currentView);	
				} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
				&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
		
				if (currentView == 0) { 
				currentView = maxTabIndex; 
				} else { 
				currentView--; 
				} 
			
				mTabHost.setCurrentTab(currentView);	
				
				} 
				} catch (Exception e) { 
				// nothing 
				} 
				return false; 
				} 
    	
    	
    	
    }
    
    
    
    
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(gestureDetector.onTouchEvent(event)){ 
			event.setAction(MotionEvent.ACTION_CANCEL); 
			} 

			return super.dispatchTouchEvent(event); 
	}

	private void setupTab(final View view, final String tag, int drawable,Intent intent) {
		View tabview = createTabView(mTabHost.getContext(), tag, drawable);
		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
		mTabHost.addTab(setContent);

	}

	private View createTabView(final Context context, final String text,int drawable) {
		View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		Typeface face=Typeface.createFromAsset(this.getAssets(),"fonts/ALKATIPTorTom.TTF");//
	    tv.setTypeface(face);
		tv.setText(text);
		ImageView iv = (ImageView)view.findViewById(R.id.icon);
		iv.setImageResource(drawable);
		return view; 
		
	}

	
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK)
		{
			  CustomizeDialog clsDialog=new CustomizeDialog(this);
	      	  clsDialog.show();
	      	  return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		gestureDetector.onTouchEvent(event);
    	return super.onTouchEvent(event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO Auto-generated method stub
		menu.add(0, 1, 1, "退出");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		System.out.println(item.getItemId());
		 android.os.Process.killProcess(android.os.Process.myPid());  
		  System.exit(0);
		return super.onOptionsItemSelected(item);
	}


}