package com.example.providers;

import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

// Our ImagePagerAdapter uses the Support Library versions
// of Fragment and FragmentManager.
public class ImagePagerActivity extends FragmentActivity {
  private static final int INTERNAL_LOADER = 0;
  private static final int EXTERNAL_LOADER = 1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      finish();
      return;
    }
    if (savedInstanceState == null) {
      ImagePagerFragment fragment = new ImagePagerFragment();
      fragment.setArguments(getIntent().getExtras());
      getSupportFragmentManager().beginTransaction()
          .add(android.R.id.content, fragment).commit();
    }
  }

  public static class ImagePagerFragment extends Fragment implements
      LoaderManager.LoaderCallbacks<Cursor> {

    private int listToShow = EXTERNAL_LOADER;
    private ImagePagerAdapter ipaInternal;
    private ImagePagerAdapter ipaExternal;
    private LoaderManager lm;
    private ViewPager viewPager;

    private final String[] columns = new String[] {
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.DISPLAY_NAME,
        MediaStore.Images.Media.DATA
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      super.onCreateView(inflater, container, savedInstanceState);
      viewPager = new ViewPager(getActivity());
      viewPager.setId(View.generateViewId());
      viewPager.setOffscreenPageLimit(3);
      return viewPager;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      lm = getLoaderManager();
      ipaInternal = new ImagePagerAdapter(getActivity(), getFragmentManager());
      ipaExternal = new ImagePagerAdapter(getActivity(), getFragmentManager());
      lm.initLoader(INTERNAL_LOADER, null, this);
      lm.initLoader(EXTERNAL_LOADER, null, this);
      switch (listToShow) {
        case INTERNAL_LOADER:
          viewPager.setAdapter(ipaInternal);
          break;
        case EXTERNAL_LOADER:
          viewPager.setAdapter(ipaExternal);
          break;
      }

    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
      CursorLoader loader = null;
      switch (id) {
        case INTERNAL_LOADER:
          loader = new CursorLoader(getActivity(),
              MediaStore.Images.Media.INTERNAL_CONTENT_URI, columns, null,
              null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
          break;
        case EXTERNAL_LOADER:
          loader = new CursorLoader(getActivity(),
              MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
              null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
          break;
      }
      return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
      switch (loader.getId()) {
        case INTERNAL_LOADER:
          ipaInternal.swapCursor(data);
          break;
        case EXTERNAL_LOADER:
          ipaExternal.swapCursor(data);
          break;
      }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
      switch (loader.getId()) {
        case INTERNAL_LOADER:
          ipaInternal.swapCursor(null);
          break;
        case EXTERNAL_LOADER:
          ipaExternal.swapCursor(null);
          break;
      }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      inflater.inflate(R.menu.images, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
        case R.id.internal_images:
          listToShow = INTERNAL_LOADER;
          // gallery.setAdapter(ipaInternal);
          viewPager.setAdapter(ipaInternal);
          break;
        case R.id.external_images:
          listToShow = EXTERNAL_LOADER;
          // gallery.setAdapter(ipaExternal);
          viewPager.setAdapter(ipaExternal);
          break;
      }
      return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
      switch (listToShow) {
        case INTERNAL_LOADER:
          menu.findItem(R.id.internal_images).setVisible(false);
          menu.findItem(R.id.external_images).setVisible(true);
          break;
        case EXTERNAL_LOADER:
          menu.findItem(R.id.internal_images).setVisible(true);
          menu.findItem(R.id.external_images).setVisible(false);
          break;
      }
    }

  }

  public static class ImageFragment extends Fragment {
    
    private View view;
    
    public void putView(View v) {
      view = v;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      return view;
    }
  }

}
