package org.iheng.wechat.activities;

import java.lang.reflect.Method;

import org.iheng.wechat.R;
import org.iheng.wechat.fragments.ChatFragment;
import org.iheng.wechat.fragments.ContactsFragment;
import org.iheng.wechat.fragments.FoundFragment;
import org.iheng.wechat.fragments.SettingsFragment;

import com.astuetz.PagerSlidingTabStrip;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;


public class MainActivity extends FragmentActivity {
	
	private ChatFragment chatFragment;
	private FoundFragment foundFragment;
	private ContactsFragment contactsFragment;
	private SettingsFragment settingsFragment;
	private PagerSlidingTabStrip tabs;
	private DisplayMetrics dm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		setOverflowShowingAlways();
		dm = getResources().getDisplayMetrics();
		ViewPager pager = (ViewPager) findViewById(R.id.pager);
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		tabs.setViewPager(pager);
		setTabsValue();
    }

    
    public class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		private final String[] titles = { "", "", "ͨѶ¼",""};

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				if (chatFragment == null) {
					chatFragment = new ChatFragment();
				}
				return chatFragment;
			case 1:
				if (foundFragment == null) {
					foundFragment = new FoundFragment();
				}
				return foundFragment;
			case 2:
				if (contactsFragment == null) {
					contactsFragment = new ContactsFragment();
				}
				return contactsFragment;
			case 3:
				if(settingsFragment==null){
					settingsFragment=new SettingsFragment();
				}
				return settingsFragment;
			default:
				return null;
			}
		}

	}
    
    private void setTabsValue(){
    	// TabԶĻ
		tabs.setShouldExpand(true);
		// Tabķָ͸
		tabs.setDividerColor(Color.TRANSPARENT);
		// Tabײߵĸ߶
		tabs.setUnderlineHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 1, dm));
		// Tab Indicatorĸ߶
		tabs.setIndicatorHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, dm));
		// TabֵĴС
		tabs.setTextSize((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_SP, 16, dm));
		// Tab Indicatorɫ
		tabs.setIndicatorColor(Color.parseColor("#45c01a"));
		// ȡTabʱıɫ
		tabs.setTabBackground(0);
    }
    @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);
    }

    /**
     * ˷ڽز˵ͼʾ
     */
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		if(featureId==Window.FEATURE_ACTION_BAR&&menu!=null){
			if(menu.getClass().getSimpleName().equals("MenuBuilder")){
				try{
					Method m=menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
					m.setAccessible(true);
					m.invoke(menu, true);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return super.onMenuOpened(featureId, menu);
	}
    
}

@@ -297,48 +297,56 @@
 nt;%0A
-%0A
 import 
-com.astuetz.PagerSlidingTabStrip
+org.iheng.wechat.nio.client.MessageClient
 ;%0A%0Ai
@@ -783,16 +783,58 @@
 indow;%0A%0A
+import com.astuetz.PagerSlidingTabStrip;%0A%0A
 %0Apublic 
@@ -1106,16 +1106,49 @@
 rics dm;
+%0A%09%0A%09private MessageClient client;
 %0A%0A    @O
@@ -1582,16 +1582,16 @@
 pager);%0A
-
 %09%09setTab
@@ -1596,24 +1596,292 @@
 absValue();%0A
+%09%09%0A%09%09new Thread()%7B%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09// TODO Auto-generated method stub%0A%09%09%09%09client=new MessageClient(8001);%0A%09%09%09%09Thread clientDeamon=new Thread(client);%0A%09%09%09%09clientDeamon.setName(%22client-deamon%22);%0A%09%09%09%09clientDeamon.start();%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D.start();%0A%09%09%0A
     %7D%0A%0A    %0A

add a working thread to establish the connection to server