package com.usai.redant.raimage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Gallery.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

import com.usai.redant.redantmobile.R;
import com.usai.redant.util.RAUtil;

import java.io.File;
import java.util.ArrayList;

public class PhotoPreviewActivity extends Activity implements
		ViewSwitcher.ViewFactory
{
	private ImageSwitcher mSwitcher;

	View current_view	= null;
	ArrayList<String> pic_file;

//	
//
//	@Override
//	protected void onDestroy()
//	{
//		Intent intent = new Intent();
//		setResult(Activity.RESULT_OK, intent);
//		super.onDestroy();
//	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_photo_preview);

		mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
		mSwitcher.setFactory(this);
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

		pic_file = (ArrayList<String>) getIntent().getSerializableExtra(
				"pic_list");

		findViewById(R.id.hsv);
		HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);

		LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);

		for (int i = 0; i < pic_file.size(); i++)
		{
			ImageView imgview = new ImageView(this);
			//
			// Bitmap thumb = thumbs.get(i);
			// if (thumb == null)
			// {
			Log.d("getView", "create thumb position:" + i);

			// thumbs.put(i, thumb);
			// }
			// Thread thread = new Thread()

			// i.setImageResource(mThumbIds[position]);
			imgview.setAdjustViewBounds(true);
			LinearLayout.LayoutParams searchParams = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
			searchParams.leftMargin = 40;
			searchParams.topMargin = 10;
			searchParams.bottomMargin = 10;
			imgview.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imgview.setLayoutParams(searchParams);
			imgview.setBackgroundResource(R.drawable.picture_frame);
			imgview.setTag(i);
			Loadtask t = new Loadtask();
			t.execute(imgview, null);
			imgview.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					current_view = v;
					Integer idx = (Integer) v.getTag();
					Uri uri = Uri.fromFile(new File(pic_file.get(idx)));
					mSwitcher.setImageURI(uri);

					// v.setSelected(true);
					// TODO Auto-generated method stub

				}
			});

			ll.addView(imgview);

			if (i == 0)
				current_view = imgview;

		}
		mSwitcher.setImageURI(Uri.fromFile(new File(pic_file.get(0))));
		// hsv.inflate(context, resource, root)

		// Gallery g = (Gallery) findViewById(R.id.gallery);
		// g.setAdapter(new ImageAdapter(this));
		// g.setOnItemSelectedListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.preview, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// TODO Auto-generated method stub
		switch (item.getItemId())
		{
			case R.id.action_delete:
			{
				LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);
				// ImageView iv= (ImageView) ll.findViewWithTag(current_path);
				ll.removeView(current_view);
				mSwitcher.setImageURI(null);
				Integer idx = (Integer) current_view.getTag();
				new File(pic_file.get(idx)).delete();
				RAUtil.updateGallery(pic_file.get(idx));
				current_view = null;
				// // pic_file.remove(idx);
			}
				break;
			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	public View makeView()
	{
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		return i;
	}

	// HashMap<Integer, Bitmap> thumbs = new HashMap<Integer, Bitmap>();
	// private Integer[] mThumbIds = {
	// };

	// private Integer[] mImageIds = {
	// };
	public class Loadtask extends AsyncTask<ImageView, Integer, Boolean>
	{
		// int err_code = ERR_CODE_NONE;
		ImageView iv		= null;
		Bitmap thumb	= null;

		// int netconnect;

		@Override
		protected Boolean doInBackground(ImageView... params)
		{
			iv = params[0];
			Integer idx = (Integer) iv.getTag();
			thumb = ThumbnailUtils.extractThumbnail(
					BitmapFactory.decodeFile(pic_file.get(idx)), 160, 120);
			return true;
		}

		@Override
		protected void onPostExecute(final Boolean success)
		{
			iv.setImageBitmap(thumb);
		}

		@Override
		protected void onCancelled()
		{

		}
	}
}
