package com.example.waoperations.Picking;


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 PickingAdapter extends CursorAdapter {
        
        public PickingAdapter(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("Posición: "+cursor.getString(2));
                clientTextView.setTextColor(Color.BLACK);
                clientTextView.setTypeface(fontbold); 
                clientTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
      
                TextView qtyTextView = (TextView) view.findViewById(R.id.nombre);
                qtyTextView.setText("ítem: "+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("Cantidad: "+cursor.getString(7)+" de "+cursor.getString(5)+" "+cursor.getString(6));
                profile.setTextColor(Color.BLACK);
                profile.setTypeface(fontbold); 
                profile.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                
                TextView email = (TextView) view.findViewById(R.id.email);
                email.setText("Descripción: "+cursor.getString(4));
                email.setTextColor(Color.LTGRAY);
                email.setTypeface(fontbold); 
                email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                
                
                
                
                Boolean completo =Double.parseDouble(cursor.getString(7))==Double.parseDouble(cursor.getString(5));
                if(cursor.getString(15).equals("1") && completo==false){
            	view.setBackgroundResource(R.color.SkyBlue);
                ImageView image = (ImageView) view.findViewById(R.id.icon);
                image.setImageResource(R.drawable.down);
                email.setTextColor(Color.GRAY);
            	email.setText("REABASTECIMIENTO PENDIENTE/ APLIQUE PARA COMENZAR");
                }
                else if(cursor.getString(8).equals("1")){
                	if(Double.parseDouble(cursor.getString(7))<Double.parseDouble(cursor.getString(5))){
                	view.setBackgroundResource(R.color.IndianRed);
                	
                    ImageView image = (ImageView) view.findViewById(R.id.icon);
                    image.setImageResource(R.drawable.down);
                	}
                	else{
                    view.setBackgroundResource(R.color.GreenYellow);
                	
                    ImageView image = (ImageView) view.findViewById(R.id.icon);
                    image.setImageResource(R.drawable.up);
                	}}
                else{
                 	view.setBackgroundResource(R.color.white);
                 	  ImageView image = (ImageView) view.findViewById(R.id.icon);
                      image.setImageResource(R.drawable.empty);
                }
   
        }

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

}