/**
 * 图片选择类,支持多图片选择
 * 
 * 本程序遵守GPL开放源码协议
 * 您可以任意使用本程序,但必须标明出处
 * 作者:Jacky Huang (黄伟)
 * 日期:2013.01.20
 * 邮箱:huangweichinese@163.com
 */
package com.image.prettify.shelly;

import com.image.prettify.adapter.ImageGridAdapter;
import com.image.utils.ImageUtil;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class PictDetail extends Activity implements OnItemClickListener, OnItemLongClickListener {

	private static String msPath;
	private static Boolean mMulti = false;
	private ImageGridAdapter iga;
	/**
	 * @return the msPath
	 */
	public static String getMsPath() {
		return msPath;
	}

	/**
	 * @param msPath the msPath to set
	 */
	public static void setMsPath(String msPath) {
		PictDetail.msPath = msPath;
	}

	/**
	 * @return the mMulti
	 */
	public static Boolean getmMulti() {
		return mMulti;
	}

	/**
	 * @param mMulti the mMulti to set
	 */
	public static void setmMulti(Boolean mMulti) {
		PictDetail.mMulti = mMulti;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pict_detail);
		GridView gridview=(GridView)findViewById(R.id.gridview1);
		iga = new ImageGridAdapter(this, msPath);
		iga.setMtype(mMulti);
		gridview.setAdapter(iga);
		gridview.setOnItemClickListener(this);
		//
		gridview.setOnItemLongClickListener(this);
		//
		RelativeLayout rlt = (RelativeLayout) findViewById(R.id.detopt1);
		if (!mMulti) {
			rlt.setVisibility(View.GONE);
		} else {
			rlt.setVisibility(View.VISIBLE);
		}
	}

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

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		ImgShow.setmPicPath(iga.getPicPath(position));
		Intent intent = new Intent();
		intent.setClass(PictDetail.this, ImgShow.class);
		startActivity(intent);
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		//
		if (mMulti) {
			ImageView igv = (ImageView) view;
			igv.setDrawingCacheEnabled(true);
			//Bitmap bm = Bitmap.createBitmap(igv.getDrawingCache());
			BitmapFactory.Options option = new BitmapFactory.Options();
			option.inSampleSize = 4;
			option.inPurgeable = true;
			Bitmap bm = BitmapFactory.decodeFile(iga.getPicPath(position), option);
			ImageUtil igu = new ImageUtil();
			igv.setDrawingCacheEnabled(false);
			if (!iga.getSelected(position)) {
				Bitmap bmp = igu.readBitMap(this, R.drawable.dago);
				Bitmap tmpBitmap = igu.doodle(bm, bmp, 0, 0);
				igv.setImageBitmap(tmpBitmap);
			} else {
				igv.setImageBitmap(bm);
			}
			iga.setSelected(position, !iga.getSelected(position));
		} else {
			//Toast.makeText(PictDetail.this, ""+position,Toast.LENGTH_SHORT).show();//显示信息;
			Intent intent = new Intent(); 
            intent.putExtra("spath", iga.getPicPath(position)); 
            setResult(RESULT_OK, intent); 
            finish(); 
		}
	}

	public void onRetClick(View v) {
		this.finish();
	}
	
	public void onOKClick(View v) {
		this.finish();
	}
	
}