package com.example.shridev1;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
/**
 * A class that handles items from the database
 * and makes them printable in recycleview
 * */
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    private Context context;
    private ArrayList ride_id, wherefrom, whereto, pickup, seats, space, car, preferences, environment;
    TextView numberOfPassengers;

       public CustomAdapter(Context context, ArrayList ride_id, ArrayList wherefrom, ArrayList whereto,
                  ArrayList pickup, ArrayList seats, ArrayList space,
                  ArrayList car, ArrayList preferences, ArrayList environment) {
        this.context = context;
        this.ride_id = ride_id;
        this.wherefrom = wherefrom;
        this.whereto = whereto;
        this.pickup = pickup;
        this.seats = seats;
        this.space = space;
        this.car = car;
        this.preferences = preferences;
        this.environment = environment;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_ride, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {


        holder.whereFrom_text.setText(String.valueOf(wherefrom.get(position)));
        holder.whereTo_text.setText(String.valueOf(whereto.get(position)));
        holder.pickup_text.setText(String.valueOf(pickup.get(position)));

        RideDetailsActivity ri = new RideDetailsActivity();
        numberOfPassengers.setText(ri.ridersAmount + "/5");

        holder.mainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view) {


                Intent intent = new Intent(context, RideDetailsActivity.class);
                intent.putExtra("wherefrom", String.valueOf(wherefrom.get(position)));
                intent.putExtra("whereto", String.valueOf(whereto.get(position)));
                intent.putExtra("pickup", String.valueOf(pickup.get(position)));
                intent.putExtra("seats", String.valueOf(seats.get(position)));
                intent.putExtra("space", String.valueOf(space.get(position)));
                intent.putExtra("car", String.valueOf(car.get(position)));
                intent.putExtra("environment", String.valueOf(environment.get(position)));
                intent.putExtra("preferences", String.valueOf(preferences.get(position)));
                intent.putExtra("numberofpassengers", String.valueOf(numberOfPassengers));
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return whereto.size();
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView id_text, whereFrom_text, whereTo_text, pickup_text, seats_text, space_text, car_text, environment_text, preferences_text;
        LinearLayout mainLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
           // id_text = itemView.findViewById(R.id.id_txt);
            whereFrom_text = itemView.findViewById(R.id.whereFrom_txt);
            whereTo_text = itemView.findViewById(R.id.whereTo_txt);
            pickup_text = itemView.findViewById(R.id.pickupPoint_txt);
            seats_text = itemView.findViewById(R.id.carSeats_txt2);
            space_text = itemView.findViewById(R.id.carSpace_txt2);
            car_text = itemView.findViewById(R.id.carType2);
            environment_text = itemView.findViewById(R.id.carEnvironment_txt2);
            preferences_text = itemView.findViewById(R.id.carPreferences_txt2);
            mainLayout = itemView.findViewById(R.id.mainLayout);
            numberOfPassengers = itemView.findViewById(R.id.numberOfPassengers);

        }

    }
}
