package com.example.myapplication6;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.button.MaterialButton;

public class goster extends BaseActivity {

    private RecyclerView recyclerEntries;
    private TextView textEmpty;
    private TextView header;
    private bizimdb db;
    private String shelf;
    private EntryListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goster);

        MaterialButton btnSettings = findViewById(R.id.btnSettings);
        if (btnSettings != null) {
            btnSettings.setOnClickListener(v -> startActivity(new Intent(this, SettingsActivity.class)));
        }

        MaterialButton btnBack = findViewById(R.id.btnBack);
        MaterialButton btnNext = findViewById(R.id.btnNext);
        if (btnBack != null) {
            btnBack.setOnClickListener(v -> {
                startActivity(new Intent(goster.this, DashboardActivity.class));
                finish();
            });
        }
        if (btnNext != null) {
            btnNext.setOnClickListener(v -> openAddNew());
        }

        db = new bizimdb(this);
        recyclerEntries = findViewById(R.id.recyclerEntries);
        textEmpty = findViewById(R.id.textEmpty);
        header = findViewById(R.id.textViewHeader);
        findViewById(R.id.btnYeni).setOnClickListener(v -> openAddNew());
        findViewById(R.id.btnTemizle).setOnClickListener(v -> clearShelf());

        Intent i = getIntent();
        shelf = i.getStringExtra(MainActivity.EXTRA_SHELF);
        if (shelf == null || shelf.isEmpty()) {
            shelf = bizimdb.SHELF_MEDIA;
        }

        header.setText(R.string.title_collection);

        recyclerEntries.setLayoutManager(new LinearLayoutManager(this));
        adapter =
                new EntryListAdapter(
                        id -> {
                            Intent ed = new Intent(goster.this, MainActivity.class);
                            ed.putExtra(MainActivity.EXTRA_ENTRY_ID, id);
                            startActivity(ed);
                        });
        recyclerEntries.setAdapter(adapter);

        String b = i.getStringExtra("baslik");
        String t = i.getStringExtra("tur");
        String p = i.getStringExtra("puan");
        String y = i.getStringExtra("yorum");

        if (b != null && !b.trim().isEmpty()) {
            db.ekle(shelf, b, t, p, y);
            Toast.makeText(this, R.string.toast_added, Toast.LENGTH_SHORT).show();
        }

        refreshList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshList();
    }

    private void openAddNew() {
        startActivity(new Intent(goster.this, MainActivity.class));
    }

    private void clearShelf() {
        db.tumunuSil(shelf);
        refreshList();
        Toast.makeText(this, R.string.toast_cleared, Toast.LENGTH_SHORT).show();
    }

    private void refreshList() {
        var list = db.getItems(shelf);
        adapter.setItems(list);
        boolean empty = list.isEmpty();
        textEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
        recyclerEntries.setVisibility(empty ? View.GONE : View.VISIBLE);
    }
}
