package com.cwbuyer.adapter;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
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.TypeData;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

public class CategoryAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	
	private LinkedList<TypeData> mList = new LinkedList<TypeData>();
	
	private ArrayList<String> mCountryList = new ArrayList<String>();
	
	private String[] mSeason = new String[]{"春天","夏天","秋天","冬天", "四季"};
	
	private int mCountryId = -1;
	private int mSeasonId = -1;
	
	public CategoryAdapter(Context context) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	      mContext = context;
	}
	
	public CategoryAdapter(Context context, int nCountry, int nSeason) {
	      /* 參數初始化 */
	      mInflater = LayoutInflater.from(context);
	      mContext = context;
	      mCountryId = nCountry;
	      mSeasonId = nSeason;
	}
	
	public void getData(Context context){
		if(mList != null){
			mList.clear();
		}
		getCountryData(context);
		SQLiteDatabase db = Utilis.getDB(context);
		
		Cursor cursor = null;
		if(mCountryId >= 0 && mSeasonId >= 0){
			try{
				cursor = db.rawQuery("select * from qc_type where COUNTRY=" + mCountryId + " and SEASON=" + mSeasonId + " order by GOODTYPE,COUNTRY,SEASON", null);
				if(cursor != null){
					if(cursor.getCount() <= 0){
						try{
							cursor.close();
							cursor = null;
						}catch(Exception e){
							e.printStackTrace();
						}finally{
							cursor = db.rawQuery("select * from qc_type order by GOODTYPE,COUNTRY,SEASON", null);
						}
					}
				}else{
					cursor = db.rawQuery("select * from qc_type order by GOODTYPE,COUNTRY,SEASON", null);
				}
			}catch(Exception e){
				e.printStackTrace();
				cursor = db.rawQuery("select * from qc_type order by GOODTYPE,COUNTRY,SEASON", null);
			}
		}else{
			cursor = db.rawQuery("select * from qc_type order by GOODTYPE,COUNTRY,SEASON", null);
		}
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						TypeData data = new TypeData();
						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						data.icon = cursor.getString(cursor.getColumnIndex("PIC"));
						data.desc = cursor.getString(cursor.getColumnIndex("GOODTYPE"));
						data.nCountry = cursor.getInt(cursor.getColumnIndex("COUNTRY"));
						data.country = mCountryList.get(data.nCountry - 1);
						data.nSeason = cursor.getInt(cursor.getColumnIndex("SEASON"));
						data.season = mSeason[data.nSeason];
						data.nAddcost = cursor.getInt(cursor.getColumnIndex("ADDCOST"));
						mList.add(data);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	public void getCountryData(Context context){
		if(mCountryList != null){
			mCountryList.clear();
		}
		SQLiteDatabase db = Utilis.getDB(context);
		Cursor cursor = db.rawQuery("select NAME 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 name = cursor.getString(cursor.getColumnIndex("NAME"));
						mCountryList.add(name);
						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 double getAddCost(int position){
		return mList.get(position).nAddcost;
	}

	
	public int getCount() {
//		return mCountry.length;
		return mList.size();
	}

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

	public long getItemId(int position) {
		return position;
	}
	
	// 類別
	public String getCategoryName(int pos){
//		return mCountry[pos];
		return mList.get(pos).desc;
	}
	

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_code_category, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.img_country);
			holder.textName = (TextView) convertView.findViewById(R.id.text_name);
			holder.textSeason = (TextView) convertView.findViewById(R.id.text_rate);
			holder.textCountry = (TextView) convertView.findViewById(R.id.text_latitude);
			holder.textAddCost = (TextView) convertView.findViewById(R.id.text_longitude);
			holder.textSeason.setTextColor(Color.BLACK);
			holder.textAddCost.setTextColor(Color.rgb(187, 0, 0));
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		TypeData data = mList.get(position);
		try{
			holder.icon.setImageDrawable(Utilis.getResourceImage(mContext, data.icon));
		}catch(Exception e){
			e.printStackTrace();
		}
		holder.textName.setText(data.desc);
		holder.textSeason.setText(data.season);
		holder.textCountry.setText(data.country);
		holder.textAddCost.setText("管銷: " + data.nAddcost);
		
//		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 textSeason;
		TextView textCountry;
		TextView textAddCost;
	}
}
