package com.cwbuyer.adapter;

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

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

@SuppressLint("InflateParams")
public class MaccAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	
	private LinkedList<AccData> mList = new LinkedList<AccData>();
	
	public MaccAdapter(Context context) {
	      /* °Ñ¼Æªì©l¤Æ */
	      mInflater = LayoutInflater.from(context);
	      mContext = context;
	}
	
	
	public void getData(Context context){
		if(mList != null){
			mList.clear();
		}
		SQLiteDatabase db = Utilis.getDB(context);
		
		Cursor cursor = null;
		cursor = db.rawQuery("select * from qc_macc order by QNOM", null);
		
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						AccData data = new AccData();
						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						data.icon = cursor.getString(cursor.getColumnIndex("PIC"));
						data.acc_nom = cursor.getString(cursor.getColumnIndex("QNOM"));
						data.acc_txt = cursor.getString(cursor.getColumnIndex("TXT"));
						data.acc_iokey = cursor.getString(cursor.getColumnIndex("IOKEY"));
						mList.add(data);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	public int getID(int position){
		return mList.get(position).id;
	}
	
	public String getAccNo(int position){
		return mList.get(position).acc_nom;
	}

	public String getAccName(int pos){
		return mList.get(pos).acc_txt;
	}
	
	public String getAccIO(int position){
		return mList.get(position).acc_iokey;
	}

	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_code_acc, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.img_acc);
			holder.textNo = (TextView) convertView.findViewById(R.id.text_accno);
			holder.textName = (TextView) convertView.findViewById(R.id.text_accname);
			holder.textIO = (TextView) convertView.findViewById(R.id.text_accio);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		AccData data = mList.get(position);
		try{
			holder.icon.setImageDrawable(Utilis.getResourceImage(mContext, data.icon));
		}catch(Exception e){
			e.printStackTrace();
		}
		holder.textNo.setText(data.acc_nom);
		holder.textName.setText(data.acc_txt);
		holder.textIO.setText(data.acc_iokey);
		
		return convertView;
	}

	/* class ViewHolder */
	private class ViewHolder {
		ImageView icon;
		TextView textNo;
		TextView textName;
		TextView textIO;
	}
}
