package com.gh4a;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.ViewGroup;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.gh4a.fragment.RepositoryListFragment;
import com.gh4a.fragment.WatchedRepositoryListFragment;

public class RepositoryListActivity extends BaseSherlockFragmentActivity {

    public String mUserLogin;
    public String mUserType;
    private ThisPageAdapter mAdapter;
    private ViewPager mPager;
    private ActionBar mActionBar;
    private int tabCount;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager);
        
        Bundle data = getIntent().getExtras();
        mUserLogin = data.getString(Constants.User.USER_LOGIN);
        mUserType = data.getString(Constants.User.USER_TYPE);

        tabCount = 2;
        
        mActionBar = getSupportActionBar();
        mAdapter = new ThisPageAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        
        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {}
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {}

            @Override
            public void onPageSelected(int position) {
                mActionBar.setSelectedNavigationItem(position);
            }
        });
        
        mActionBar.setTitle(mUserLogin);
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mActionBar.setDisplayShowTitleEnabled(true);
        mActionBar.setHomeButtonEnabled(true);
        
        Tab tab = mActionBar
                .newTab()
                .setText(R.string.user_pub_repos)
                .setTabListener(
                        new TabListener<SherlockFragmentActivity>(this, 0 + "", mPager));
        mActionBar.addTab(tab);
        
        tab = mActionBar
                .newTab()
                .setText(R.string.user_watched_repos)
                .setTabListener(
                        new TabListener<SherlockFragmentActivity>(this, 1 + "", mPager));
        mActionBar.addTab(tab);
        
    }
    
    public class ThisPageAdapter extends FragmentStatePagerAdapter {

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

        @Override
        public int getCount() {
            return tabCount;
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            if (position == 0) {
                return RepositoryListFragment.newInstance(RepositoryListActivity.this.mUserLogin,
                        RepositoryListActivity.this.mUserType);
            }
            else if (position == 1) {
                return WatchedRepositoryListFragment.newInstance(RepositoryListActivity.this.mUserLogin);
            }
            else {
                return RepositoryListFragment.newInstance(RepositoryListActivity.this.mUserLogin,
                        RepositoryListActivity.this.mUserType);
            }
        }
        
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }
    }
}

@@ -1144,16 +1144,133 @@
 TYPE);%0A%0A
+        if (Constants.User.USER_TYPE_ORG.equals(mUserType)) %7B%0A            tabCount = 1;%0A        %7D%0A        else %7B%0A    
         
@@ -1283,16 +1283,26 @@
 nt = 2;%0A
+        %7D%0A
         
@@ -2436,32 +2436,65 @@
 (tab);%0A        %0A
+        if (tabCount == 2) %7B%0A    
         tab = mA
@@ -2510,32 +2510,36 @@
                 
+    
 .newTab()%0A      
@@ -2524,32 +2524,36 @@
       .newTab()%0A
+    
                 
@@ -2598,32 +2598,36 @@
                 
+    
 .setTabListener(
@@ -2643,32 +2643,36 @@
                 
+   
+ 
 new TabListener%3C
@@ -2721,32 +2721,36 @@
 ager));%0A        
+    
 mActionBar.addTa
@@ -2749,32 +2749,42 @@
 ar.addTab(tab);%0A
+        %7D%0A
         %0A    %7D%0A 

Remove "Watched Repos" tab if type is Organization