package com.cwbuyer.adapter;

import java.util.ArrayList;
import java.util.LinkedList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

@SuppressLint("InflateParams")
public class WashAdapter  extends BaseAdapter {
	private LayoutInflater mInflater;
	
	private int[] mIcon = new int[] {
			R.drawable.img_wash_1,
			R.drawable.img_wash_2,
			R.drawable.img_wash_3,
			R.drawable.img_wash_4,
			R.drawable.img_wash_5,
			R.drawable.img_wash_6};
	
	LinkedList<WashData> mList = new LinkedList<WashData>();
	
    public static class WashData{
    	public int id ;
    	public ArrayList<String> mPics = new ArrayList<String>();
    }
	
	public WashAdapter(Context context) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
//	      makeData();
	}
	
	public WashAdapter(Context context, int count) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	}
	
	public int getId(int pos){
		return mList.get(pos).id;
	}
	
	public void getData(Context context, int id){
		if(mList != null){
			mList.clear();
		}
		SQLiteDatabase db = Utilis.getDB(context);
		Cursor cursor = null;
		if(id > 0){
			cursor = db.rawQuery("select * from qc_wash where _id=" + id, null);
		}else{
			cursor = db.rawQuery("select * from qc_wash", null);
		}
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						WashData data = new WashData();
						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						String strImage1 = null;
						strImage1 = cursor.getString(cursor.getColumnIndex("WASH10"));
						if(strImage1 != null && strImage1.length() > 0){
							data.mPics.add(strImage1);
						}
						strImage1 = cursor.getString(cursor.getColumnIndex("WASH20"));
						if(strImage1 != null && strImage1.length() > 0){
							data.mPics.add(strImage1);
						}
						strImage1 = cursor.getString(cursor.getColumnIndex("WASH30"));
						if(strImage1 != null && strImage1.length() > 0){
							data.mPics.add(strImage1);
						}
						strImage1 = cursor.getString(cursor.getColumnIndex("WASH40"));
						if(strImage1 != null && strImage1.length() > 0){
							data.mPics.add(strImage1);
						}
						strImage1 = cursor.getString(cursor.getColumnIndex("WASH50"));
						if(strImage1 != null && strImage1.length() > 0){
							data.mPics.add(strImage1);
						}
						strImage1 = cursor.getString(cursor.getColumnIndex("WASH60"));
						if(strImage1 != null && strImage1.length() > 0){
							data.mPics.add(strImage1);
						}
						mList.add(data);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	public int getCount() {
		return mList.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_washing, null);
			holder = new ViewHolder();
			holder.image1 = (ImageView)convertView.findViewById(R.id.img_1);
			holder.image2 = (ImageView)convertView.findViewById(R.id.img_2);
			holder.image3 = (ImageView)convertView.findViewById(R.id.img_3);
			holder.image4 = (ImageView)convertView.findViewById(R.id.img_4);
			holder.image5 = (ImageView)convertView.findViewById(R.id.img_5);
			holder.image6 = (ImageView)convertView.findViewById(R.id.img_6);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
//		ArrayList<Integer> list = mList.get(position);
		
		holder.image1.setVisibility(View.INVISIBLE);
		holder.image2.setVisibility(View.INVISIBLE);
		holder.image3.setVisibility(View.INVISIBLE);
		holder.image4.setVisibility(View.INVISIBLE);
		holder.image5.setVisibility(View.INVISIBLE);
		holder.image6.setVisibility(View.INVISIBLE);
		// 先隱藏圖片
		WashData data = mList.get(position);
		
		int size = data.mPics.size();
		switch(size){
		case 6:
			holder.image6.setVisibility(View.VISIBLE);
		case 5:
			holder.image5.setVisibility(View.VISIBLE);
		case 4:
			holder.image4.setVisibility(View.VISIBLE);
		case 3:
			holder.image3.setVisibility(View.VISIBLE);
		case 2:
			holder.image2.setVisibility(View.VISIBLE);
		case 1:
			holder.image1.setVisibility(View.VISIBLE);
			break;
		}
		// 需要去抓取對應的圖片才能決定位置
		for(int i = 0; i < data.mPics.size(); i++){
			String name = data.mPics.get(i);
			// 取出來是 draw/wash1 or 2...
			switch(i){
			case 0:
//				holder.image1.setImageResource(getImageIndex(name));
				holder.image1.setImageBitmap(Utilis.getLimitBitmap(name, 200, 45));
				break;
			case 1:
				holder.image2.setImageBitmap(Utilis.getLimitBitmap(name, 200, 45));
				break;
			case 2:
				holder.image3.setImageBitmap(Utilis.getLimitBitmap(name, 200, 45));
				break;
			case 3:
				holder.image4.setImageBitmap(Utilis.getLimitBitmap(name, 200, 45));
				break;
			case 4:
				holder.image5.setImageBitmap(Utilis.getLimitBitmap(name, 200, 45));
				break;
			case 5:
				holder.image6.setImageBitmap(Utilis.getLimitBitmap(name, 200, 45));
				break;
			}
		}
		return convertView;
	}
	
	@SuppressWarnings("unused")
	private int getImageIndex(String drawname){
		int index = 0;
		if(drawname.indexOf("img_wash_1") >= 0){
			index = 0;
		}else if(drawname.indexOf("img_wash_2") >= 0){
			index = 1;
		}else if(drawname.indexOf("img_wash_3") >= 0){
			index = 2;
		}else if(drawname.indexOf("img_wash_4") >= 0){
			index = 3;
		}else if(drawname.indexOf("img_wash_5") >= 0){
			index = 4;
		}else if(drawname.indexOf("img_wash_6") >= 0){
			index = 5;
		}
		return mIcon[index];
	}

	/* class ViewHolder */
	private class ViewHolder {
		ImageView image1;
		ImageView image2;
		ImageView image3;
		ImageView image4;
		ImageView image5;
		ImageView image6;
	}
}
