package com.cwbuyer.adapter;

import java.util.LinkedList;
import android.annotation.SuppressLint;
import android.content.Context;
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.main.ManageAccount.AccountData;
import com.pwbuyer.main.R;

@SuppressWarnings("unused")
public class AccountAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private int[] mIcon = new int[]{R.drawable.m1,
				R.drawable.m2, R.drawable.m3,
				R.drawable.m4, R.drawable.m5,
				R.drawable.m6, R.drawable.m6};
	
	private String[] mAccountName = new String[]{"現金", "臺灣銀行", "現金(人民幣)", "悠遊卡", "iCash卡", "現金(美金)", "信用卡-台新卡"};
	private String[] mRate = new String[]{"1:1", "1:1", "4.4:1", "1:1", "1:1", "1:1", "30:1"}; 
	private String[] mInitMoney = new String[]{"0"};
	private String[] mMoney = new String[]{"0"};
	
	public LinkedList<AccountData> mList = null;
	
	public AccountAdapter(Context context) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	      mList = new LinkedList<AccountData>();
	}
	
	public int getCount() {
		return mList.size();
	}
	
	public void setData(LinkedList<AccountData> list){
		mList = list;
		if(mList == null){
			mList = new LinkedList<AccountData>();
		}
	}

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

	public long getItemId(int position) {
		return position;
	}
	
	@SuppressLint("InflateParams")
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_manage_account, 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.textInitMoney = (TextView) convertView.findViewById(R.id.text_latitude);
			holder.textMoney = (TextView) convertView.findViewById(R.id.text_longitude);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try{
			AccountData data = mList.get(position);
			if(data != null){
				holder.icon.setImageResource(getIcon(data.nPic));
				holder.textName.setText(data.strText);
				holder.textRate.setText("匯率: " + data.strRate + ":1");
				holder.textInitMoney.setText("初始 : " + data.nInit);
				holder.textMoney.setText("$ : " + data.nMoney);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return convertView;
	}
	
	private int getIcon(int pos){
		int id = R.drawable.m1;
		switch(pos){
		case 1:
			id = R.drawable.m1;
			break;
		case 2:
			id = R.drawable.m2;
			break;
		case 3:
			id = R.drawable.m3;
			break;
		case 4:
			id = R.drawable.m4;
			break;
		case 5:
			id = R.drawable.m5;
			break;
		case 6:
			id = R.drawable.m6;
			break;
		}
		return id;
	}

	/* class ViewHolder */
	private class ViewHolder {
		ImageView icon;
		TextView textName;
		TextView textRate;
		TextView textInitMoney;
		TextView textMoney;
	}
}
