package com.cwbuyer.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Html;
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;
// 定價策略
@SuppressLint({ "InflateParams", "UseSparseArrays" })
public class PricingAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	
	private LinkedList<PriceData> mList = new LinkedList<PriceData>();
	
	private HashMap<Integer,Integer> mCountryIdList = new HashMap<Integer,Integer>();
	private ArrayList<String> mCountryList = new ArrayList<String>();
	private ArrayList<String> mCountryIcon = new ArrayList<String>();
	
	private int mCountryId = 0;
	
	public static class PriceData{
		public int id;
		public int nCountry;
		public String country;
		public double rate;
		public double p0;  // 進價
		public double p1;  // 成本
		public double p2;  // 中盤價
		public double p3;  // 批發價
		public double p4;  // 網路價
		public double p5;  // 零售價
	}
	
	public PricingAdapter(Context context) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	      getCountryData(context);
	}
	
	public PricingAdapter(Context context, int nCountryId) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	      getCountryData(context);
	      mCountryId = nCountryId;
	}
	
	public int getId(int pos){
		return mList.get(pos).id;
	}
	
	public PriceData getData(int pos){
		return mList.get(pos);
	}
	
	public void getData(Context context){
		if(mList != null){
			mList.clear();
		}
		SQLiteDatabase db = Utilis.getDB(context);
		
		Cursor cursor = null;
		if(mCountryId > 0){
			cursor = db.rawQuery("select * from qc_defpri where COUNTRY=" + mCountryId + " order by country,p0", null);
		}else{
			cursor = db.rawQuery("select * from qc_defpri order by country,p0", null);
		}
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						PriceData data = new PriceData();
						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						data.nCountry = cursor.getInt(cursor.getColumnIndex("COUNTRY"));
						int index = mCountryIdList.get(data.nCountry);
						if(index < 0){
							data.country = "NO";
							data.rate = 1;
						}else{
							data.country = mCountryList.get(index);
						}
						
						data.p0 = cursor.getDouble(cursor.getColumnIndex("P0"));
						data.p1 = cursor.getDouble(cursor.getColumnIndex("P1"));
						data.p2 = cursor.getDouble(cursor.getColumnIndex("P2"));
						data.p3 = cursor.getDouble(cursor.getColumnIndex("P3"));
						data.p4 = cursor.getDouble(cursor.getColumnIndex("P4"));
						data.p5 = cursor.getDouble(cursor.getColumnIndex("P5"));
						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 _ID,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++){
						int id = cursor.getInt(cursor.getColumnIndex("_ID"));
						mCountryIdList.put(id, 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){
				e.printStackTrace();
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	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_pricing, null);
			holder = new ViewHolder();
			holder.textCountry = (TextView) convertView.findViewById(R.id.text_country);
			holder.textInPrice = (TextView) convertView.findViewById(R.id.text_inprice);
			holder.textRetailPrice = (TextView) convertView.findViewById(R.id.text_retailprice);
			holder.textNetPrice = (TextView) convertView.findViewById(R.id.text_netprice);
			holder.textP2 = (TextView) convertView.findViewById(R.id.text_p2);
			holder.textP3 = (TextView) convertView.findViewById(R.id.text_p3);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		PriceData data = mList.get(position);
		holder.textCountry.setText("國別: " + data.country);
		holder.textInPrice.setText("進價 : " + data.p0);
		holder.textP2.setText(Html.fromHtml("中盤價: <font color='#14CC29'>" + data.p2 + "</font>"));
		holder.textP3.setText(Html.fromHtml("批發價: <font color='#14CC29'>" + data.p3 + "</font>"));
		holder.textNetPrice.setText(Html.fromHtml("網路價 : <font color='#BB0000'>" + data.p4 + "</font>"));
		holder.textRetailPrice.setText(Html.fromHtml("零售價 : <font color='#BB0000'>" + data.p5 + "</font>"));
		return convertView;
	}

	/* class ViewHolder */
	private class ViewHolder {
		TextView textCountry;
		TextView textInPrice;
		TextView textNetPrice;
		TextView textRetailPrice;
		TextView textP2;
		TextView textP3;
	}
}