package com.example.waoperations.PutAway;


import java.text.NumberFormat;

import com.example.waoperations.R;
import com.example.waoperations.R.color;
import com.example.waoperations.R.drawable;
import com.example.waoperations.R.id;
import com.example.waoperations.R.layout;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PutAwayAdapter extends CursorAdapter {
	   private PutAwayDatabaseHelper databaseHelper;
        public PutAwayAdapter(Context context, Cursor cursor) {
                super(context, cursor);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
    		if (view == null) {
                LayoutInflater mInflater = (LayoutInflater)
                        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                view = mInflater.inflate(R.layout.drawer_list_item, null);
            }
            Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/opensans-light.ttf");  
            Typeface fontbold = Typeface.createFromAsset(context.getAssets(), "fonts/opensans-semibold.ttf");  

                TextView clientTextView = (TextView) view.findViewById(R.id.cliente);
                clientTextView.setText("ítem: "+cursor.getString(2));
                clientTextView.setTextColor(Color.BLACK);
                clientTextView.setTypeface(fontbold); 
                clientTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                
                double qtystored=Double.parseDouble(cursor.getString(13));
                double qtytostore=Double.parseDouble(cursor.getString(4));
                double remaining = qtytostore-qtystored;
                NumberFormat nf = NumberFormat.getInstance();
                nf.setMaximumFractionDigits(0);// set as you need
                    if(qtystored==0){
                	 TextView qtyTextView = (TextView) view.findViewById(R.id.nombre);
                     qtyTextView.setText("Ubicaciónes disponibles: "+cursor.getString(12));
                     qtyTextView.setTextColor(Color.GRAY);
                     qtyTextView.setTypeface(fontbold); 
                     qtyTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    }else{
                     TextView qtyTextView = (TextView) view.findViewById(R.id.nombre);
                     qtyTextView.setText("Ubicaciónes usadas: "+cursor.getString(3));
                     qtyTextView.setTextColor(Color.GRAY);
                     qtyTextView.setTypeface(fontbold); 
                     qtyTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    }
                     TextView profile = (TextView) view.findViewById(R.id.puesto);
                     profile.setText("Pendiente: "+nf.format(remaining)+" "+cursor.getString(5));
                     profile.setTextColor(Color.LTGRAY);
                     profile.setTypeface(fontbold); 
                     profile.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                      
                     TextView email = (TextView) view.findViewById(R.id.email);
                      email.setText("Cantidad ubicada: "+nf.format(qtystored)+" "+cursor.getString(5));
                      email.setTextColor(Color.BLACK);
                      email.setTypeface(fontbold); 
                      email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                   	if(qtystored==0){
                         	view.setBackgroundResource(R.color.white);
                    ImageView image = (ImageView) view.findViewById(R.id.icon);
                              image.setImageResource(R.drawable.empty);
                   	}
                   	else if(qtytostore-qtystored==0){
                   		view.setBackgroundResource(R.color.GreenYellow);
                        ImageView image = (ImageView) view.findViewById(R.id.icon);
                        image.setImageResource(R.drawable.up);
                    	}else{
                    		view.setBackgroundResource(R.color.IndianRed);
                        ImageView image = (ImageView) view.findViewById(R.id.icon);
                        image.setImageResource(R.drawable.down);
                    	}

        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.putawaylist_item, parent, false);
                return view;
        }

}