package com.example.waoperations.Aduanas;


import android.R.string;

//import java.text.NumberFormat;

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

import com.example.waoperations.R;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class AduanasAdapter extends CursorAdapter {
        
		private AduanasDatabaseHelper databaseHelper;
		private double PesoBultos;
		private String factor;
		
        public AduanasAdapter(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");  
            factor="0";
              
            TextView ItemTextView = (TextView) view.findViewById(R.id.ItemId);
            ItemTextView.setText("Item: "+cursor.getString(1));
            ItemTextView.setTextColor(Color.BLACK);
            ItemTextView.setTypeface(fontbold); 
            ItemTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
            
            TextView InventarioTextView = (TextView) view.findViewById(R.id.InventarioID);
            InventarioTextView.setText("Número de Movimiento: "+cursor.getString(11));
            InventarioTextView.setTextColor(Color.BLACK);
            InventarioTextView.setTypeface(fontbold); 
            InventarioTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            
            TextView descriptionTextView = (TextView) view.findViewById(R.id.descripcion);
            descriptionTextView.setText("Descripción: "+ cursor.getString(2));
            descriptionTextView.setTextColor(Color.BLACK);
            descriptionTextView.setTypeface(fontbold); 
            descriptionTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            
            String qty = "0";
            if(cursor.getString(3).equals(""))
            {
            	qty = "0";
            }
            else
            {
            	qty = cursor.getString(3);
            }

            TextView qtyTextView = (TextView) view.findViewById(R.id.Cantidad);
            qtyTextView.setText("Cantidad bultos: "+qty);
            qtyTextView.setTextColor(Color.BLACK);
            qtyTextView.setTypeface(fontbold); 
            qtyTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            
            TextView AlmacenTextView = (TextView) view.findViewById(R.id.Almacen);
            AlmacenTextView.setText("Almacén: "+cursor.getString(4));
            AlmacenTextView.setTextColor(Color.BLACK);
            AlmacenTextView.setTypeface(fontbold); 
            AlmacenTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

            TextView EmpresaTextView = (TextView) view.findViewById(R.id.Empresa);
            EmpresaTextView.setText("Consignatario: "+cursor.getString(5));
            EmpresaTextView.setTextColor(Color.BLACK);
            EmpresaTextView.setTypeface(fontbold); 
            EmpresaTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

            TextView BLTextView = (TextView) view.findViewById(R.id.BL);
            BLTextView.setText("BL: "+cursor.getString(7));
            BLTextView.setTextColor(Color.BLACK);
            BLTextView.setTypeface(fontbold); 
            BLTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            
            TextView ConfigIdTextView = (TextView) view.findViewById(R.id.ConfigID);
            ConfigIdTextView.setText("Configuración: "+cursor.getString(8));
            ConfigIdTextView.setTextColor(Color.BLACK);
            ConfigIdTextView.setTypeface(fontbold); 
            ConfigIdTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            
            TextView YearIdTextView = (TextView) view.findViewById(R.id.YearID);
            YearIdTextView.setText("Año: "+cursor.getString(9));
            YearIdTextView.setTextColor(Color.BLACK);
            YearIdTextView.setTypeface(fontbold); 
            YearIdTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            
            TextView  DUATextView = (TextView) view.findViewById(R.id.DUA);
            DUATextView.setText("DUA de Ingreso: "+cursor.getString(10));
            DUATextView.setTextColor(Color.BLACK);
            DUATextView.setTypeface(fontbold); 
            DUATextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

            TextView FechaIngresoTextView = (TextView) view.findViewById(R.id.FechaIngreso);
            FechaIngresoTextView.setText("Fecha de Ingreso: "+cursor.getString(12));
            FechaIngresoTextView.setTextColor(Color.BLACK);
            FechaIngresoTextView.setTypeface(fontbold); 
            FechaIngresoTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            
            try{
            	double constantPeso = 0.453592;
            	double cant = Double.parseDouble(cursor.getString(3));
            	double peso = Double.parseDouble(cursor.getString(6));
            	
            	if(peso == 0.0)
            	{
            		peso = 1.46916528;
            	}
             	
            	PesoBultos = (cant*peso*constantPeso);
  
            	if(PesoBultos == 0){
            		peso=1.46916528;
                	PesoBultos = (cant*peso*constantPeso);
            	}
            }catch(Exception exc){}
            
            DecimalFormat format = new DecimalFormat("#######.##");
            TextView PesoTextView = (TextView) view.findViewById(R.id.Peso);
            PesoTextView.setText("Peso: " +format.format(PesoBultos));
            PesoTextView.setTextColor(Color.BLACK);
            PesoTextView.setTypeface(fontbold); 
            PesoTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
       
               
        }

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

}