package com.example.waoperations.Stats;


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 StatsAdapter extends CursorAdapter {
        
        public StatsAdapter(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("Usuario: "+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("Fecha: "+cursor.getString(3));
                qtyTextView.setTextColor(Color.DKGRAY);
                qtyTextView.setTypeface(fontbold); 
                qtyTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
               
                TextView profile = (TextView) view.findViewById(R.id.email);
              
                profile.setTextColor(Color.BLACK);
                profile.setTypeface(fontbold); 
                profile.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                
                if(cursor.getString(4).equals("10")||cursor.getString(4).equals("11")){
                    profile.setText("RecepciÃ³n: "+cursor.getString(5));	
                    }
                    if(cursor.getString(4).equals("20")||cursor.getString(4).equals("21")){
                    profile.setText("Acomodo: "+cursor.getString(5));		
                    }
                    if(cursor.getString(4).equals("30")||cursor.getString(4).equals("31")){
                    profile.setText("Alisto: "+cursor.getString(5));		
                    }
                    if(cursor.getString(4).equals("40")||cursor.getString(4).equals("41")){
                    profile.setText("Envío: "+cursor.getString(5));		
                    }
                    if(cursor.getString(4).equals("50")||cursor.getString(4).equals("51")){
                    profile.setText("Inventario: "+cursor.getString(5));		
                    }
                    if(cursor.getString(4).equals("60")||cursor.getString(4).equals("61")){
                        profile.setText("Reabastecimiento: "+cursor.getString(5));		
                      }
                    
                    
                if(cursor.getString(6).equals("0")){}
                else if(cursor.getString(4).equals("60")){
                    TextView email = (TextView) view.findViewById(R.id.puesto);
                    email.setText("Pendientes: "+cursor.getString(6));
                    email.setTextColor(Color.DKGRAY);
                    email.setTypeface(fontbold); 
                    email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);	
                }
                else{    
                Double porcentaje=Double.valueOf(cursor.getString(7))/Double.valueOf(cursor.getString(6))*100;
                NumberFormat nf3 = NumberFormat.getInstance();
	 	        nf3.setMaximumFractionDigits(0);// set as you need
	 	        
                TextView email = (TextView) view.findViewById(R.id.puesto);
                email.setText("Completado: "+cursor.getString(7)+"/"+cursor.getString(6)+"("+nf3.format(porcentaje)+"%)");
                email.setTextColor(Color.DKGRAY);
                email.setTypeface(fontbold); 
                email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
         
               if(porcentaje>80){
               		view.setBackgroundResource(R.color.GreenYellow);
                    ImageView image = (ImageView) view.findViewById(R.id.icon);
                    image.setImageResource(R.drawable.up);
                	}
               else if(porcentaje>50){
           		view.setBackgroundResource(R.color.YellowGreen);
               ImageView image = (ImageView) view.findViewById(R.id.icon);
               image.setImageResource(R.drawable.down);
          }
               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.pickinglist_item, parent, false);
                return view;
        }

}