/*
 *  Copyright (c) 2012 Chute Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.chute.android.photopickerplustutorial.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;

import com.chute.android.photopickerplus.util.intent.GridActivityIntentWrapper;
import com.chute.android.photopickerplustutorial.R;
import com.chute.android.photopickerplustutorial.adapter.GridAdapter;
import com.chute.android.photopickerplustutorial.intent.PhotoPickerPlusIntentWrapper;

public class PhotoPickerPlusTutorialActivity extends FragmentActivity {

	public static final String TAG = PhotoPickerPlusTutorialActivity.class.getSimpleName();
	private GridView grid;
	/**
	 * PhotoPicker+ component enables choosing multiple photos or a single photo
	 * in a grid. If you wish to enable multi-picking functionality, set
	 * isMultipicker=true, otherwise set isMultiPicker=false.
	 */
	private final boolean isMultiPicker = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo_picker_plus);

		findViewById(R.id.btnPhotoPicker).setOnClickListener(new OnPhotoPickerClickListener());
		grid = (GridView) findViewById(R.id.grid);
		
		int orientation = getResources().getConfiguration().orientation;
		if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
			grid.setNumColumns(5);
		}

	}

	private class OnPhotoPickerClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			PhotoPickerPlusIntentWrapper wrapper = new PhotoPickerPlusIntentWrapper(
					PhotoPickerPlusTutorialActivity.this);
			wrapper.setMultiPicker(isMultiPicker);
			wrapper.startActivityForResult(PhotoPickerPlusTutorialActivity.this,
					PhotoPickerPlusIntentWrapper.REQUEST_CODE);

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		final GridActivityIntentWrapper wrapper = new GridActivityIntentWrapper(data);
		grid.setAdapter(new GridAdapter(PhotoPickerPlusTutorialActivity.this, wrapper.getMediaCollection()));
		Log.d("debug" , wrapper.getMediaCollection().toString());

		// String path;
		// Uri uri = Uri.parse(wrapper.getMediaCollection().get(0).getUrl());
		// if (uri.getScheme().contentEquals("http")) {
		// path = uri.toString();
		// } else {
		// path = uri.getPath();
		// }
		// ALog.d("The Path or url of the file " + path);
	}
}

@@ -1126,16 +1126,45 @@
 ivity;%0A%0A
+import java.util.ArrayList;%0A%0A
 import a
@@ -1176,32 +1176,32 @@
 d.app.Activity;%0A
-
 import android.c
@@ -1744,16 +1744,65 @@
 Wrapper;
+%0Aimport com.chute.sdk.v2.model.AccountMediaModel;
 %0A%0Apublic
@@ -1957,16 +1957,86 @@
 Name();%0A
+%09private static final String KEY_SELECTED_ITEMS = %22keySelectedItems%22;%0A
 %09private
@@ -2312,16 +2312,102 @@
  = true;
+%0A%09private GridAdapter adapter;%0A%09private ArrayList%3CAccountMediaModel%3E accountMediaList;
 %0A%0A%09@Over
@@ -2693,16 +2693,368 @@
 rid);%0A%09%09
+if (savedInstanceState != null) %7B%0A%09%09%09accountMediaList = savedInstanceState.getParcelableArrayList(KEY_SELECTED_ITEMS);%0A%09%09%09adapter = new GridAdapter(PhotoPickerPlusTutorialActivity.this, accountMediaList);%0A%09%09%7D else %7B%0A%09%09%09adapter = new GridAdapter(PhotoPickerPlusTutorialActivity.this, new ArrayList%3CAccountMediaModel%3E());%0A%09%09%7D%0A%09%09grid.setAdapter(adapter);%0A
 %0A%09%09int o
@@ -3909,112 +3909,102 @@
 ;%0A%09%09
-grid.setAdapter(new GridAdapter(PhotoPickerPlusTutorialActivity.this, wrapper.getMediaCollection())
+accountMediaList = wrapper.getMediaCollection();%0A%09%09adapter.changeData(accountMediaList
 );%0A%09%09
+// 
 Log.
@@ -4012,17 +4012,16 @@
 (%22debug%22
- 
 , wrappe
@@ -4273,16 +4273,16 @@
 %0A%09%09// %7D%0A
-
 %09%09// ALo
@@ -4327,11 +4327,193 @@
 ath);%0A%09%7D
+%0A%0A%09@Override%0A%09protected void onSaveInstanceState(Bundle outState) %7B%0A%09%09outState.putParcelableArrayList(KEY_SELECTED_ITEMS, accountMediaList);%0A%09%09super.onSaveInstanceState(outState);%0A%09%7D
 %0A%7D%0A

save asset grid state on orientation change