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

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

// 廠商主檔
@SuppressLint("InflateParams")
@SuppressWarnings("unused")
public class ComberAdapter  extends BaseAdapter {
	private LayoutInflater mInflater;
	private Context mContext;
	private int[] mIcon = new int[]{R.drawable.country_koe};
	
	private String[] mCountry = new String[]{"U21061A"};
	private String[] mRate = new String[]{"US棟2F 106樓A室"}; 
	private String[] mLatitude = new String[]{"13922946917"};
	private String[] mLongitude = new String[]{"100.0"};
	
	private LinkedList<FactData> mList = new LinkedList<FactData>();
	private ArrayList<String> mCountryList = new ArrayList<String>();
	private ArrayList<String> mCountryIcon = new ArrayList<String>();
	
	public ComberAdapter(Context context) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	      mContext = context;
	}
	
	public String getFactNo(int position){
		return mList.get(position).factno;
	}
	
	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_company, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.img_country);
			holder.textName = (TextView) convertView.findViewById(R.id.text_name);
//			holder.textRate = (TextView) convertView.findViewById(R.id.text_rate);
			holder.textFactName = (TextView) convertView.findViewById(R.id.text_latitude);
//			holder.textLongitude = (TextView) convertView.findViewById(R.id.text_longitude);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		FactData data = mList.get(position);
		
		holder.icon.setImageDrawable(Utilis.getResourceImage(mContext, mCountryIcon.get(data.nCountry - 1)));
		
		StringBuffer buf = new StringBuffer();
		if(data.address != null){
			holder.textName.setText(data.factno + " " + data.address);
		}else{
			holder.textName.setText(data.factno);
		}
		
		holder.textFactName.setText(data.factname);
		
//		holder.icon.setImageResource(mIcon[position]);
//		holder.textName.setText(mCountry[position] + "  " + mRate[position]);
//		holder.textRate.setText(mRate[position]);
//		holder.textFactName.setText(mLatitude[position]);
//		holder.textLongitude.setText(mLongitude[position]);
		return convertView;
	}

	/* class ViewHolder */
	private class ViewHolder {
		ImageView icon;
		TextView textName;
//		TextView textRate;
		TextView textFactName;
//		TextView textLongitude;
	}
	

	public void getData(Context context, int nCountryId){
		if(mList != null){
			mList.clear();
		}
		getCountryData(context);
		SQLiteDatabase db = Utilis.getDB(context);
		StringBuffer buf = new StringBuffer();
		buf.append("select * from qfact");
		
		if(nCountryId > 0){
			buf.append(" where COUNTRY=").append(nCountryId);
		}
		
		buf.append(" order by COUNTRY desc");
		
		Cursor cursor = db.rawQuery(buf.toString(), null);
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						FactData data = new FactData();
//						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						data.nTR = cursor.getInt(cursor.getColumnIndex("TR"));
						data.factno = cursor.getString(cursor.getColumnIndex("FACTNO"));
						data.factname = cursor.getString(cursor.getColumnIndex("FACTNAME"));
						data.mobile = cursor.getString(cursor.getColumnIndex("MOBIL"));
						data.email = cursor.getString(cursor.getColumnIndex("EMAIL"));
						data.msn = cursor.getString(cursor.getColumnIndex("MSN"));
						data.gmail = cursor.getString(cursor.getColumnIndex("GMAIL"));
						
						data.dGpsLA = cursor.getDouble(cursor.getColumnIndex("GPSLA"));
						data.dGpsLO = cursor.getDouble(cursor.getColumnIndex("GPSLO"));
						
						data.deptno = cursor.getString(cursor.getColumnIndex("DEPTNO"));
						data.empid = cursor.getString(cursor.getColumnIndex("EMPID"));
						data.dDiscount = cursor.getDouble(cursor.getColumnIndex("DISCOUNT"));
						
						data.address = cursor.getString(cursor.getColumnIndex("ADDRESS"));
						
						data.desc = cursor.getString(cursor.getColumnIndex("PS"));
						data.bankno = cursor.getString(cursor.getColumnIndex("BANKNO"));
						data.bankname = cursor.getString(cursor.getColumnIndex("BANKNAME"));
						
						data.nCountry = cursor.getInt(cursor.getColumnIndex("COUNTRY"));
						data.country = mCountryList.get(data.nCountry - 1);
						
						data.nTradetype = cursor.getInt(cursor.getColumnIndex("TRADETYPE"));
						data.nState = cursor.getString(cursor.getColumnIndex("STATE"));
						
						data.pic = cursor.getString(cursor.getColumnIndex("PIC"));
						data.time = cursor.getString(cursor.getColumnIndex("CREATEDATETIME"));
						 
						mList.add(data);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	public void getCountryData(Context context){
		if(mCountryIcon != null){
			mCountryIcon.clear();
		}
		if(mCountryList != null){
			mCountryList.clear();
		}
		SQLiteDatabase db = Utilis.getDB(context);
		Cursor cursor = db.rawQuery("select NAME,PIC from qc_country order by _ID", null);
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						String pic = cursor.getString(cursor.getColumnIndex("PIC"));
						String name = cursor.getString(cursor.getColumnIndex("NAME"));
						mCountryIcon.add(pic);
						mCountryList.add(name);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}

}
