package com.herprogramacion.trickmarket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.internal.widget.AdapterViewCompat;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.TextView;
import java.util.ArrayList;
import static com.herprogramacion.trickmarket.Products.productos;
public class BuyPage extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
    Spinner combobox;
    EditText txtcantidad,txtcosteproducto;
    TextView datos,txt5;
    double costeproducto,subtotal;
    ArrayList<Double> costes;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buy_page);
        combobox=(Spinner)findViewById(R.id.combobox);
        txtcantidad=(EditText)findViewById(R.id.cantidad);
        txtcosteproducto=(EditText)findViewById(R.id.costeproducto);
        ArrayAdapter adapter=new ArrayAdapter<Product>(this,android.R.layout.simple_spinner_dropdown_item);
        for(Product[] p:productos){
            Products product=new Products();
            String lista=product.toString();
            adapter.add(lista);
        }
        combobox.setAdapter(adapter);
        combobox.setOnItemSelectedListener(this);
        datos=(TextView)findViewById(R.id.datos);
        String[] Datos=new String[5];
        Datos=datos.getText().toString().split(",");
        String nombre=Datos[0];
        String caract=Datos[1];
        String precio=Datos[2];
        String rating=Datos[3];
        String foto=Datos[4];
        int coste=Integer.parseInt(precio.substring(1,precio.length()-6));
        String cantidad=txtcantidad.getText().toString();
        int cant=Integer.parseInt(cantidad);
        costeproducto=cant*coste;
        costes.add(costeproducto);
        for(int i=0;i<costes.size();i++){
            subtotal+=costes.get(i);
        }
        String subto=String.valueOf(subtotal);
        txt5.setText(subto);

    }
    @Override
    public void onItemSelected(AdapterView<?> parent,View view,int position,long id){
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent){
    }
    public class ItemSelectedListener implements AdapterViewCompat.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
            String selected=parent.getItemAtPosition(pos).toString();
            datos.setText(selected);
        }
        public void onNothingSelected(AdapterView parent){
            //no hacer nada
        }
        @Override
        public void onItemSelected(AdapterViewCompat<?> parent,View view, int position,long id){
        }
        @Override
        public void onNothingSelected(AdapterViewCompat<?> parent){
        }
    }
}