package com.example.shridev1;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class RideListActivity extends AppCompatActivity {


    MyDatabaseHelper myDB;
    ArrayList<String> ride_id, whereFrom, whereTo, pickup, seats, space, car, preferences, environment;
    CustomAdapter customAdapter;
    RecyclerView clicker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Ta bort titelbaren längst upp
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_ride_list);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.reView);


        myDB = new MyDatabaseHelper(RideListActivity.this);
        ride_id = new ArrayList<>();
        whereFrom = new ArrayList<>();
        whereTo = new ArrayList<>();
        pickup = new ArrayList<>();
        seats = new ArrayList<>();
        space = new ArrayList<>();
        car = new ArrayList<>();
        preferences = new ArrayList<>();
        environment = new ArrayList<>();

        storeDataInArrays();

        customAdapter = new CustomAdapter(RideListActivity.this, ride_id, whereFrom, whereTo, pickup, seats, space, car, preferences, environment);
        recyclerView.setAdapter(customAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(RideListActivity.this));


        clicker = (RecyclerView) findViewById(R.id.reView);
        clicker.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), RideDetailsActivity.class);
                startActivity(i);
                Log.d("RideListActivity", "Button clicked");
            }
        });

    }
    void storeDataInArrays() {
        Cursor cursor = myDB.readAllData();
        if (cursor.getCount() == 0) {
            Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
        }
        else {
            while (cursor.moveToNext()) {
                ride_id.add(cursor.getString(0));
                whereFrom.add(cursor.getString(1));
                whereTo.add(cursor.getString(2));
                pickup.add("Pickup at " + cursor.getString(3));
                car.add(cursor.getString(4));
                space.add("Space for " + cursor.getString(5) + " bag each");
                seats.add("Amount of seats: " + cursor.getString(6));
                preferences.add("Preferences: " + cursor.getString(7));
                environment.add("Environment: " + cursor.getString(8));
            }
        }
    }

    }
