package com.emoon.balance.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.emoon.balance.Model.BalanceType;
import com.emoon.balance.Model.Transaction;
import com.emoon.balance.R;
import com.emoon.balance.Util.Util;
import com.zhan.library.CircularView;

import java.util.List;

/**
 * Created by zhanyap on 2016-03-12.
 */
public class TransactionAdapter extends ArrayAdapter<Transaction>{
    private Context context;
    private List<Transaction> transactionList;

    static class ViewHolder {
        public CircularView icon;
        public TextView name, points, unitType;
    }

    public TransactionAdapter(Context context, List<Transaction> transactionList){
        super(context, R.layout.item_transaction, transactionList);
        this.context = context;
        this.transactionList = transactionList;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // Avoid un-necessary calls to findViewById() on each row, which is expensive!
        ViewHolder viewHolder;

        /*
         * If convertView is not null, we can reuse it directly, no inflation required!
         * We only inflate a new View when the convertView is null.
         */
        if (convertView == null) {

            // Create a ViewHolder and store references to the two children views
            viewHolder = new ViewHolder();

            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.item_transaction, parent, false);

            viewHolder.icon = (CircularView) convertView.findViewById(R.id.transactionIcon);
            viewHolder.points = (TextView) convertView.findViewById(R.id.transactionPointsPerUnit);
            viewHolder.unitType = (TextView) convertView.findViewById(R.id.transactionUnitType);
            viewHolder.name = (TextView) convertView.findViewById(R.id.earnBurnName);

            // The tag can be any Object, this just happens to be the ViewHolder
            convertView.setTag(viewHolder);
        }else {
            // Get the ViewHolder back to get fast access to the Views
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // getting Transaction data for the row
        Transaction transaction = transactionList.get(position);

        if(transaction.getEarnBurn().getType().equalsIgnoreCase(BalanceType.BURN.toString())) {
            //viewHolder.icon.setColorFilter(Color.parseColor(context.getResources().getString(0 + R.color.blue)));
            viewHolder.icon.setCircleColor(R.color.blue);
        }else {
            //viewHolder.icon.setColorFilter(Color.parseColor(context.getResources().getString(0 + R.color.red)));
            viewHolder.icon.setCircleColor(R.color.red);
        }
        viewHolder.icon.setIconResource(Util.getIconID(this.context, transaction.getEarnBurn().getIcon()));
        viewHolder.icon.setIconColor(R.color.white);

        viewHolder.name.setText(transaction.getEarnBurn().getName());
        viewHolder.points.setText(String.valueOf(transaction.getUnitCost()));
        viewHolder.unitType.setText(transaction.getCostType());

        return convertView;
    }
}

@@ -331,16 +331,57 @@
 ceType;%0A
+import com.emoon.balance.Model.IconType;%0A
 import c
@@ -2679,34 +2679,32 @@
 ) %7B%0A            
-//
 viewHolder.icon.
@@ -2711,89 +2711,49 @@
 setC
-olorFilter(Color.parseColor(context.getResources().getString(0 + R.color.blue)));
+ircleColor(R.color.blue);%0A        %7Delse %7B
 %0A   
@@ -2792,36 +2792,35 @@
 leColor(R.color.
-blue
+red
 );%0A        %7Delse
@@ -2807,37 +2807,128 @@
 .red);%0A        %7D
-else 
+%0A%0A        if(transaction.getEarnBurn().getIconType().equalsIgnoreCase(IconType.ICON.toString()))
 %7B%0A            //
@@ -2925,18 +2925,16 @@
         
-//
 viewHold
@@ -2948,85 +2948,84 @@
 .set
-ColorFilter(Color.parseColor(context.getResources().getString(0 + R.color.red
+IconResource(Util.getIconID(this.context, transaction.getEarnBurn().getIcon(
 )));
@@ -3048,38 +3048,36 @@
 wHolder.icon.set
-Circle
+Icon
 Color(R.color.re
@@ -3066,35 +3066,37 @@
 onColor(R.color.
-red
+white
 );%0A        %7D%0A   
@@ -3095,78 +3095,175 @@
    %7D
-%0A        viewHolder.icon.setIconResource(Util.getIconID(this.context, 
+else if(transaction.getEarnBurn().getIconType().equalsIgnoreCase(IconType.NUMBER.toString()))%7B%0A            viewHolder.icon.setText(%22%22+Util.getFirstCharacterFromString(
 tran
@@ -3283,34 +3283,52 @@
 rnBurn().get
-Icon
+Name().toUpperCase
 ()));%0A
+    
         view
@@ -3334,36 +3334,36 @@
 wHolder.icon.set
-Icon
+Text
 Color(R.color.wh
@@ -3367,16 +3367,26 @@
 .white);
+%0A        %7D
 %0A%0A      

Fix transactionAdapter