package com.cwbuyer.adapter;

import java.util.ArrayList;
import java.util.LinkedList;

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.TextView;

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


// 成份組合
public class ElementAdapter  extends BaseAdapter {
	private LayoutInflater mInflater;
	
	private LinkedList<ElementData> mList = new LinkedList<ElementData>();
	
	public static class ElementData{
		public int id;
		public ArrayList<String> elements = new ArrayList<String>();
		public ArrayList<String> percentages = new ArrayList<String>();
		public ElementData(){};
	}
	
	public ElementAdapter(Context context) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
//	      makeData();
	}
	
	public ElementAdapter(Context context, int count) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
//	      makeDataSingle();
	}
	
	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_quality where _id=" + id, null);
		}else{
			cursor = db.rawQuery("select * from qc_quality", null);
		}
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						ElementData data = new ElementData();
						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						String strName = null;
						strName = cursor.getString(cursor.getColumnIndex("QUA10"));
						if(strName != null && strName.length() > 0){
							data.elements.add(strName);
							data.percentages.add(cursor.getString(cursor.getColumnIndex("QUA11")));
						}
						strName = cursor.getString(cursor.getColumnIndex("QUA20"));
						if(strName != null && strName.length() > 0){
							data.elements.add(strName);
							data.percentages.add(cursor.getString(cursor.getColumnIndex("QUA21")));
						}
						strName = cursor.getString(cursor.getColumnIndex("QUA30"));
						if(strName != null && strName.length() > 0){
							data.elements.add(strName);
							data.percentages.add(cursor.getString(cursor.getColumnIndex("QUA31")));
						}
						strName = cursor.getString(cursor.getColumnIndex("QUA40"));
						if(strName != null && strName.length() > 0){
							data.elements.add(strName);
							data.percentages.add(cursor.getString(cursor.getColumnIndex("QUA41")));
						}
						strName = cursor.getString(cursor.getColumnIndex("QUA50"));
						if(strName != null && strName.length() > 0){
							data.elements.add(strName);
							data.percentages.add(cursor.getString(cursor.getColumnIndex("QUA51")));
						}
						strName = cursor.getString(cursor.getColumnIndex("QUA60"));
						if(strName != null && strName.length() > 0){
							data.elements.add(strName);
							data.percentages.add(cursor.getString(cursor.getColumnIndex("QUA61")));
						}
						mList.add(data);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	public int getId(int pos){
		return mList.get(pos).id;
	}
	
	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_elements, null);
			holder = new ViewHolder();
			holder.textName1 = (TextView) convertView.findViewById(R.id.text_elem1);
			holder.textName2 = (TextView) convertView.findViewById(R.id.text_elem2);
			holder.textName3 = (TextView) convertView.findViewById(R.id.text_elem3);
			holder.textName4 = (TextView) convertView.findViewById(R.id.text_elem4);
			holder.textName5 = (TextView) convertView.findViewById(R.id.text_elem5);
			holder.textName6 = (TextView) convertView.findViewById(R.id.text_elem6);
			holder.textNumber1 = (TextView) convertView.findViewById(R.id.text_perc1);
			holder.textNumber2 = (TextView) convertView.findViewById(R.id.text_perc2);
			holder.textNumber3 = (TextView) convertView.findViewById(R.id.text_perc3);
			holder.textNumber4 = (TextView) convertView.findViewById(R.id.text_perc4);
			holder.textNumber5 = (TextView) convertView.findViewById(R.id.text_perc5);
			holder.textNumber6 = (TextView) convertView.findViewById(R.id.text_perc6);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.textName1.setText("");
		holder.textName2.setText("");
		holder.textName3.setText("");
		holder.textName4.setText("");
		holder.textName5.setText("");
		holder.textName6.setText("");
		holder.textNumber1.setText("");
		holder.textNumber2.setText("");
		holder.textNumber3.setText("");
		holder.textNumber4.setText("");
		holder.textNumber5.setText("");
		holder.textNumber6.setText("");
		
		ElementData data = mList.get(position);
		
		for(int i = 0; i < data.elements.size(); i ++){
			switch(i){
			case 0:
				holder.textName1.setText(data.elements.get(i));
				holder.textNumber1.setText("" + data.percentages.get(i) + "%");
				break;
			case 1:
				holder.textName2.setText(data.elements.get(i));
				holder.textNumber2.setText("" + data.percentages.get(i) + "%");
				break;
			case 2:
				holder.textName3.setText(data.elements.get(i));
				holder.textNumber3.setText("" + data.percentages.get(i) + "%");
				break;
			case 3:
				holder.textName4.setText(data.elements.get(i));
				holder.textNumber4.setText("" + data.percentages.get(i) + "%");
				break;
			case 4:
				holder.textName5.setText(data.elements.get(i));
				holder.textNumber5.setText("" + data.percentages.get(i) + "%");
				break;
			case 5:
				holder.textName6.setText(data.elements.get(i));
				holder.textNumber6.setText("" + data.percentages.get(i) + "%");
				break;
			}
		}
		return convertView;
	}

	/* class ViewHolder */
	private class ViewHolder {
		TextView textName1;
		TextView textNumber1;
		TextView textName2;
		TextView textNumber2;
		TextView textName3;
		TextView textNumber3;
		TextView textName4;
		TextView textNumber4;
		TextView textName5;
		TextView textNumber5;
		TextView textName6;
		TextView textNumber6;
	}
}