package com.bourke.glimmr.activities;

import com.actionbarsherlock.view.Menu;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.net.Uri;

import android.os.Bundle;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import android.util.Log;

import com.actionbarsherlock.app.SherlockFragment;

import com.androidquery.AQuery;

import com.bourke.glimmr.common.Constants;
import com.bourke.glimmr.fragments.explore.RecentPublicPhotosFragment;
import com.bourke.glimmr.R;
import com.bourke.glimmr.tasks.GetAccessTokenTask;
import com.bourke.glimmr.fragments.LoginFragment;

import com.googlecode.flickrjandroid.people.User;
import android.widget.Toast;
import com.actionbarsherlock.view.MenuItem;

/**
 * Hosts fragments that don't require log in.
 */
public class ExploreActivity extends BaseActivity
        implements LoginFragment.IOnNotNowClicked {

    private static final String TAG = "Glimmr/ExploreActivity";

    public static final int INTERESTING_PAGE = 0;
    //public static final int TAGS_PAGE = 1;

    public static String[] CONTENT;

    private LoginFragment mLoginFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Constants.DEBUG) Log.d(getLogTag(), "onCreate");

        CONTENT = new String[] { "Last 7 Days" };

        setContentView(R.layout.explore_activity);
        mAq = new AQuery(this);
        mLoginFragment = (LoginFragment) getSupportFragmentManager()
            .findFragmentById(R.id.loginFragment);
        mLoginFragment.setNotNowListener(this);
        initViewPager();

        handleIntent(getIntent());
        //Appirater.appLaunched(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshLoginFragment();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_login:
                mAq.id(R.id.loginFragment).visible();
                SharedPreferences sp = getSharedPreferences(
                        Constants.PREFS_NAME, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean(Constants.LOGIN_LATER_SELECTED, false);
                editor.commit();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void initViewPager() {
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        ExplorePagerAdapter adapter = new ExplorePagerAdapter(
                getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        //TitlePageIndicator indicator = (TitlePageIndicator) findViewById(
        //        R.id.indicator);
        //indicator.setOnPageChangeListener(this);
        //indicator.setViewPager(viewPager);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (Constants.DEBUG) Log.d(getLogTag(), "onNewIntent");

        setIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        if (intent != null) {
            String scheme = intent.getScheme();
            if (Constants.CALLBACK_SCHEME.equals(scheme)) {
                Uri uri = intent.getData();
                String[] data = uri.getQuery().split("&");
                SharedPreferences prefs = getSharedPreferences(Constants
                        .PREFS_NAME, Context.MODE_PRIVATE);
                String oAuthSecret = prefs.getString(
                        Constants.KEY_TOKEN_SECRET, null);
                String oauthToken = data[0].substring(data[0]
                        .indexOf("=")+1);
                String oauthVerifier = data[1].substring(data[1]
                        .indexOf("=")+1);
                new GetAccessTokenTask(mLoginFragment).execute(oauthToken,
                        oAuthSecret, oauthVerifier);
            } else {
                if (Constants.DEBUG) {
                    Log.d(TAG, "Received intent but unknown scheme: " +
                            scheme);
                }
            }
        } else {
            if (Constants.DEBUG)
                Log.d(TAG, "Started with null intent");
        }
    }

    @Override
    public void onNotNowClicked() {
        mAq.id(R.id.loginFragment).invisible();
        SharedPreferences sp =
            getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(Constants.LOGIN_LATER_SELECTED, true);
        editor.commit();
        Toast.makeText(this, getString(R.string.login_later),
                Toast.LENGTH_LONG).show();
    }

    private void refreshLoginFragment() {
        SharedPreferences sp =
            getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
        boolean loginLater =
            sp.getBoolean(Constants.LOGIN_LATER_SELECTED, false);
        if (loginLater) {
            mAq.id(R.id.loginFragment).invisible();
        } else {
            mAq.id(R.id.loginFragment).visible();
        }
    }

    @Override
    public User getUser() {
        return mUser;
    }

    class ExplorePagerAdapter extends FragmentPagerAdapter {
        public ExplorePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public SherlockFragment getItem(int position) {
            switch (position) {
                case INTERESTING_PAGE:
                    return RecentPublicPhotosFragment.newInstance();
            }
            return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return ExploreActivity.CONTENT[
                position % ExploreActivity.CONTENT.length].toUpperCase();
        }
    }
}

@@ -849,16 +849,67 @@
 enuItem;
+%0Aimport android.support.v4.app.FragmentTransaction;
 %0A%0A/**%0A *
@@ -2145,37 +2145,28 @@
             
-mAq.id(R.id.l
+setL
 oginFragment
@@ -2161,34 +2161,39 @@
 oginFragment
-).v
+V
 isib
-le(
+ility(true
 );%0A         
@@ -4560,45 +4560,40 @@
     
-mAq.id(R.id.loginFragment).invisible(
+setLoginFragmentVisibility(false
 );%0A 
@@ -4947,32 +4947,431 @@
 .show();%0A    %7D%0A%0A
+    private void setLoginFragmentVisibility(boolean show) %7B%0A        FragmentTransaction ft =%0A            getSupportFragmentManager().beginTransaction();%0A        ft.setCustomAnimations(android.R.anim.fade_in,%0A                android.R.anim.fade_out);%0A        if (show) %7B%0A            ft.show(mLoginFragment);%0A        %7D else %7B%0A            ft.hide(mLoginFragment);%0A        %7D%0A        ft.commit();%0A    %7D%0A%0A
     private void
@@ -5642,45 +5642,40 @@
     
-mAq.id(R.id.loginFragment).invisible(
+setLoginFragmentVisibility(false
 );%0A 
@@ -5706,21 +5706,12 @@
     
-mAq.id(R.id.l
+setL
 ogin
@@ -5722,18 +5722,23 @@
 ment
-).v
+V
 isib
-le(
+ility(true
 );%0A 

Apply fragment fade animations to login fragment