/************************************************************
 *	°æÈ¨ËùÓÐ  (c)2011,   hxf<p>	
 *  ÎÄ¼þÃû³Æ	£ºOrderDishAdapter.java<p>
 *
 *  ´´½¨Ê±¼ä	£º2015Äê10ÔÂ17ÈÕ ÏÂÎç6:55:30 
 *  µ±Ç°°æ±¾ºÅ£ºv1.0
 ************************************************************/
package com.example.orderservice.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.example.orderservice.R;
import com.example.orderservice.model.DishInfoBean;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/************************************************************
 *  ÄÚÈÝÕªÒª	£º<p>
 *
 *  ×÷Õß	£ºhxf
 *  ´´½¨Ê±¼ä	£º2015Äê10ÔÂ17ÈÕ ÏÂÎç6:55:30 
 *  µ±Ç°°æ±¾ºÅ£ºv1.0
 *  ÀúÊ·¼ÇÂ¼	:
 *  	ÈÕÆÚ	: 2015Äê10ÔÂ17ÈÕ ÏÂÎç6:55:30 	ÐÞ¸ÄÈË£º
 *  	ÃèÊö	:
 ************************************************************/
public class OrderDishAdapter extends ArrayAdapter<DishInfoBean> {
	
	private int resourceId;
	private Context context;
	
	//±»Ñ¡ÖÐµÄ±êÖ¾
	private int clickItem = -1;
	
	public void setSelectedItem(int position){
		clickItem = position;
	}
	
	/**
	 * ¹¹Ôìº¯Êý£ºOrderDishAdapter
	 * º¯Êý¹¦ÄÜ:
	 * ²ÎÊýËµÃ÷£º
	 * 		@param context
	 * 		@param resource
	 * 		@param objects
	 */
	public OrderDishAdapter(Context context, int resource, List<DishInfoBean> dishInfoList) {
		super(context, resource, dishInfoList);
		this.resourceId = resource;
		this.context = context;
		
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DishInfoBean dishInfoBean = getItem(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
		}
		ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_dish_photo);
		TextView textView = (TextView) convertView.findViewById(R.id.tv_dish_info);
		TextView tvDishPrice = (TextView) convertView.findViewById(R.id.tv_dish_price);

		//-----------------------------------------¡·
		//»ñÈ¡Í¼Æ¬Â·¾¶£¬°ÑÂ·¾¶±£´æÔÚÊý¾Ý¿â£¬È»ºó´ÓÊý¾Ý¿âÈ¡³öÂ·¾¶£¬»ñµÃÍ¼Æ¬µÄÊäÈëÁ÷£¬ÓÃBitmapFactory½âÎö³ÉBitmap Õâ¸ö¹ý³Ì³öÏÖÎÊÌâ
		//-----------------------------------------¡·
		
		//´ÓdishInfoBeanÖÐ»ñÈ¡Í¼Æ¬µÄÂ·¾¶£¨¸ÃÂ·¾¶ÊÇ´æÔÚÊý¾Ý¿âPHOTO×Ö¶ÎÖÐ£©
		String photoPath = dishInfoBean.getPhoto();	
		//ÓÃBitmap½âÎöÊäÈëÁ÷»ñµÃBitmap¶ÔÏó
		Bitmap bitmap = getImageFromAssetsFile(photoPath);	
		//°ÑÍ¼Æ¬ÉèÖÃ¸øImageView
		imageView.setImageBitmap(bitmap);
		
		//ÉèÖÃÑ¡ÖÐÊ±Í¼Æ¬µÄÍ¸Ã÷¶È
		if (clickItem == position) {
			imageView.setImageAlpha(70);
		}else{ //Èç¹û²»¼ÓelseÓï¾äµÄ»°£¬ÄÇÃ´Ñ¡ÖÐÒ»¸öÍ¼Æ¬ºó£¬ÔÙÑ¡ÔñÒ»¸öÍ¼Æ¬Ê±£¬Ç°ÄÇ¸öÍ¼Æ¬Í¸Ã÷¶È»¹ÊÇ±»ÉèÖÃÎª70£¬Ò²¾ÍÊÇËµ±»Ñ¡ÖÐºó²»ÄÜ»Ö¸´
			imageView.setImageAlpha(255);
		}
		
		
		
		//´ÓdishInfoBeanÖÐ»ñµÃCURRENCY_TYPEËù¶ÔÓ¦µÄT_TABLE_DICTIONARY±íÖÐµÄ»õ±ÒÃû³Æ
		String currencyTypeName = dishInfoBean.getCurrencyTypeName();
		//»ñÈ¡²ËµÄ¼Û¸ñ
		float price = dishInfoBean.getPrice();
		//»ñÈ¡²ËµÄÃû×Ö
		String dishName = dishInfoBean.getName();
		//´ÓdishInfoBeanÖÐ»ñµÃUNITËù¶ÔÓ¦µÄT_TABLE_DICTIONARY±íÖÐµÄ²ËµÄµ¥Î»
		String dishUnitName = dishInfoBean.getUnitName();
		
		textView.setText(dishName);
		tvDishPrice.setText(price+currencyTypeName+"/"+dishUnitName);
		return convertView;
	}
	
	/**
     * ´ÓAssetsÖÐ¶ÁÈ¡Í¼Æ¬
     */
    private Bitmap getImageFromAssetsFile(String photoPath)
    {
        Bitmap image = null;
        //Context.getResources() ·µ»Ø Resources¶ÔÏó
        AssetManager am = context.getResources().getAssets();
        try
        {	
            InputStream is = am.open(photoPath);//Ê¹ÓÃÂ·¾¶»ñµÃÊäÈëÁ÷
            image = BitmapFactory.decodeStream(is);
            is.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return image;

    }

}
