/*
 * Copyright 2012 GitHub Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.mobile.ui.user;

import static com.github.mobile.Intents.EXTRA_USER;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.ProgressBar;

import com.github.kevinsawicki.wishlist.ViewUtils;
import com.github.mobile.Intents.Builder;
import com.github.mobile.R.id;
import com.github.mobile.R.layout;
import com.github.mobile.R.string;
import com.github.mobile.core.user.RefreshUserTask;
import com.github.mobile.util.AvatarLoader;
import com.github.mobile.util.ToastUtils;
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
import com.google.inject.Inject;
import com.viewpagerindicator.TitlePageIndicator;

import org.eclipse.egit.github.core.User;

import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;

/**
 * Activity to view a user's various pages
 */
public class UserViewActivity extends RoboSherlockFragmentActivity implements
        OrganizationSelectionProvider {

    /**
     * Create intent for this activity
     *
     * @param user
     * @return intent
     */
    public static Intent createIntent(User user) {
        return new Builder("user.VIEW").user(user).toIntent();
    }

    @Inject
    private AvatarLoader avatars;

    @InjectExtra(EXTRA_USER)
    private User user;

    @InjectView(id.vp_pages)
    private ViewPager pager;

    @InjectView(id.pb_loading)
    private ProgressBar loadingBar;

    @InjectView(id.tpi_header)
    private TitlePageIndicator indicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(layout.pager_with_title);

        getSupportActionBar().setTitle(user.getLogin());

        if (user.getAvatarUrl() != null)
            configurePager();
        else {
            ViewUtils.setGone(loadingBar, false);
            ViewUtils.setGone(pager, true);
            ViewUtils.setGone(indicator, true);
            new RefreshUserTask(this, user.getLogin()) {

                @Override
                protected void onSuccess(User fullUser) throws Exception {
                    super.onSuccess(fullUser);

                    user = fullUser;
                    configurePager();
                }

                @Override
                protected void onException(Exception e) throws RuntimeException {
                    super.onException(e);

                    ToastUtils.show(UserViewActivity.this,
                            string.error_person_load);
                    ViewUtils.setGone(loadingBar, true);
                }
            }.execute();
        }
    }

    private void configurePager() {
        avatars.bind(getSupportActionBar(), user);
        ViewUtils.setGone(loadingBar, true);
        ViewUtils.setGone(pager, false);
        ViewUtils.setGone(indicator, false);
        pager.setAdapter(new UserPagerAdapter(getSupportFragmentManager(),
                getResources()));
        indicator.setViewPager(pager);
    }

    @Override
    public User addListener(OrganizationSelectionListener listener) {
        return user;
    }

    @Override
    public OrganizationSelectionProvider removeListener(
            OrganizationSelectionListener listener) {
        return this;
    }
}

@@ -734,50 +734,8 @@
 le;%0A
-import android.support.v4.view.ViewPager;%0A
 impo
@@ -1012,16 +1012,62 @@
 erTask;%0A
+import com.github.mobile.ui.TabPagerActivity;%0A
 import c
@@ -1160,168 +1160,27 @@
 om.g
-ithub.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;%0Aimport com.google.inject.Inject;%0Aimport com.viewpagerindicator.TitlePageIndicator
+oogle.inject.Inject
 ;%0A%0Ai
@@ -1388,55 +1388,61 @@
 nds 
-RoboSherlockFragmentActivity implements%0A       
+TabPagerActivity%3CUserPagerAdapter%3E%0A        implements
  Org
@@ -1799,67 +1799,8 @@
 r;%0A%0A
-    @InjectView(id.vp_pages)%0A    private ViewPager pager;%0A%0A
     
@@ -1867,82 +1867,8 @@
 r;%0A%0A
-    @InjectView(id.tpi_header)%0A    private TitlePageIndicator indicator;%0A%0A
     
@@ -1983,58 +1983,8 @@
 );%0A%0A
-        setContentView(layout.pager_with_title);%0A%0A
     
@@ -2123,16 +2123,46 @@
  else %7B%0A
+            createTabPager();%0A
         
@@ -2251,56 +2251,8 @@
 e);%0A
-            ViewUtils.setGone(indicator, true);%0A
     
@@ -3013,24 +3013,50 @@
 r(), user);%0A
+        createTabPager();%0A
         View
@@ -3133,201 +3133,8 @@
 e);%0A
-        ViewUtils.setGone(indicator, false);%0A        pager.setAdapter(new UserPagerAdapter(getSupportFragmentManager(),%0A                getResources()));%0A        indicator.setViewPager(pager);%0A
     
@@ -3399,11 +3399,266 @@
 s;%0A    %7D
+%0A%0A    @Override%0A    protected UserPagerAdapter createAdapter() %7B%0A        return new UserPagerAdapter(getSupportFragmentManager(), getResources());%0A    %7D%0A%0A    @Override%0A    protected int getContentView() %7B%0A        return layout.tabbed_progress_pager;%0A    %7D
 %0A%7D%0A

Use tabbed pager in UserViewActivity