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.CountryData;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;


@SuppressLint("InflateParams")
@SuppressWarnings("unused")
public class CountryAdapter extends BaseAdapter {
	private Context mContext = null; 
	private LayoutInflater mInflater;
	private int[] mIcon = new int[]{R.drawable.country_twn,R.drawable.country_chi,
				R.drawable.country_koe, R.drawable.country_jpn};
	private String[] mCountry = new String[]{"台灣", "中國", "韓國", "日本"};
	private String[] mRate = new String[]{"1.0", "4.782", "0.0268", "0.288"}; 
	private String[] mLatitude = new String[]{"23 - 26", "02 - 56", "02 - 56", "02 - 56"};
	private String[] mLongitude = new String[]{"124.00 - 126.00", "88.00 - 125.00", "88.00 - 125.00", "88.00 - 125.00"};
	
	private LinkedList<CountryData> mList = new LinkedList<CountryData>();
	
	public CountryAdapter(Context context) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	      mContext = context;
	}
	
	public void getData(Context context){
		if(mList != null){
			mList.clear();
		}
		SQLiteDatabase db = Utilis.getDB(context);
		Cursor cursor = db.rawQuery("select * from qc_country", null);
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						CountryData data = new CountryData();
						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						data.icon = cursor.getString(cursor.getColumnIndex("PIC"));
						data.name = cursor.getString(cursor.getColumnIndex("NAME"));
						data.rate = cursor.getDouble(cursor.getColumnIndex("RATE"));
						data.lamin = cursor.getDouble(cursor.getColumnIndex("LAMIN"));
						data.lamax = cursor.getDouble(cursor.getColumnIndex("LAMAX"));
						data.lomin = cursor.getDouble(cursor.getColumnIndex("LOMIN"));
						data.lomax = cursor.getDouble(cursor.getColumnIndex("LOMAX"));
						mList.add(data);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	
	public int getID(int position){
		try{
			int id = mList.get(position).id;
			return id;
		}catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	
	public int getCount() {
//		return mCountry.length;
		return mList.size();
	}

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

	public long getItemId(int position) {
		return position;
	}
	
	// 取得 Country 圖片 id
	public int getCountryPID(int pos){
		return mIcon[pos];
	}
	
	// 取得 Country 圖片 id
	public String getCountryPic(int pos){
		CountryData data = mList.get(pos);
		return data.icon;
	}
	
	// 取得 Country 圖片 id
	public int getCountryId(int pos){
		CountryData data = mList.get(pos);
		return data.id;
	}
	
	public double getCountryRate(int pos){
		CountryData data = mList.get(pos);
		return data.rate;
	}
	
	// 取得 Country 圖片 id
	public String getCountryName(int pos){
		CountryData data = mList.get(pos);
		return data.name;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_code_country, 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.textLatitude = (TextView) convertView.findViewById(R.id.text_latitude);
			holder.textLongitude = (TextView) convertView.findViewById(R.id.text_longitude);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		CountryData data = mList.get(position);
		
//		holder.icon.setImageDrawable(Utilis.getResourceImage(mContext, data.icon));
		holder.icon.setImageDrawable(Utilis.getResourceImage(mContext, data.icon));
		holder.textName.setText(data.name);
		holder.textRate.setText("匯率 : " + data.rate);
		holder.textLatitude.setText("緯度 : " + data.lamin + "-" + data.lamax);
		holder.textLongitude.setText("經度 : " + data.lomin + "-" + data.lomax);
//		holder.icon.setImageResource(mIcon[position]);
//		holder.textName.setText(mCountry[position]);
//		holder.textRate.setText("匯率 : " + mRate[position]);
//		holder.textLatitude.setText("緯度 : " + mLatitude[position]);
//		holder.textLongitude.setText("經度 : " + mLongitude[position]);
		return convertView;
	}

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