package BWP.MobileSale.BranchOrder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import BWP.MobileSale.R;
import BWP.MobileSale.Util.DateTimeUtil;
import BWP.MobileSale.Util.StringUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BranchOrderListViewAdapter extends BaseAdapter {

	List<Long> itemsid;
	List<BranchOrderListView> Items;
	private LayoutInflater inflater;

	// public BranchOrderListViewAdapter(List<BranchOrderListView> items) {
	// this.Items = items;
	// }

	public BranchOrderListViewAdapter(List<BranchOrderListView> items,
			Context context) {

		super();
		this.Items = items;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		itemsid=new ArrayList<Long>();
		setItemsId();
	}

	private void setItemsId()
	{
		for (Iterator iterator = Items.iterator(); iterator.hasNext();) {
			BranchOrderListView item = (BranchOrderListView) iterator.next();
			itemsid.add(item.getID());
		}
	}
	
	@Override
	public int getCount() {
		return Items.size();

	}

	@Override
	public Object getItem(int position) {
		return Items.get(position);

	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater
					.inflate(R.layout.branchorder_listinner, null);
		}

		TextView txtID = (TextView) convertView.findViewById(R.id.TextView_ID);
		txtID.setText(Long.toString(Items.get(position).getID()));

		TextView txtBuyerName = (TextView) convertView
				.findViewById(R.id.TextView_Buyer);
		txtBuyerName.setText(Items.get(position).getBuyerName());

		TextView txtBillDate = (TextView) convertView
				.findViewById(R.id.TextView_BillDate);
		txtBillDate.setText(DateTimeUtil.FormatDate(Items.get(position)
				.getBillDate()));

		TextView txtDiffReadyMoney = (TextView) convertView.findViewById(R.id.TextView_DiffReadyMoney);
		if(Items.get(position).getDiffReadyMoney()!=null)
		{
			txtDiffReadyMoney.setText(Items.get(position).getDiffReadyMoney().toString());
		}
		else
		{
			txtDiffReadyMoney.setText("");
		}
		

		TextView txtTodayLoadMoney = (TextView) convertView.findViewById(R.id.TextView_TodayLoadMoney);
		if (Items.get(position).getTodayLoadMoney() != null) {
			txtTodayLoadMoney.setText(Items.get(position).getTodayLoadMoney().toString());
		} else {
			txtTodayLoadMoney.setText("");
		}

		return convertView;
	}

	public void addItem(BranchOrderListView item) {
		
		if (!itemsid.contains(item.getID())) {			
			Items.add(item);
			itemsid.add(item.getID());
		}

	}

	public void AddRefreshItem(BranchOrderListView item) {
		if (!itemsid.contains(item.getID())){
			Items.add(0, item);
			itemsid.add(item.getID());
		}
		
	}

}
