package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;

public class UsersActivity extends AppCompatActivity {

    private DatabaseHelper db;
    private ListView listViewUsers;
    private Button btnBack;

    private ArrayList<Integer> userIds = new ArrayList<>();
    private ArrayList<String> userNames = new ArrayList<>();
    private ArrayList<String> userEmails = new ArrayList<>();
    private ArrayList<Integer> recipeCounts = new ArrayList<>();

    private CustomUsersAdapter adapter;

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

        // Oturum kontrolü
        SharedPreferences pref = getSharedPreferences("UserSession", Context.MODE_PRIVATE);
        if (!pref.contains("userId")) {
            Intent intent = new Intent(UsersActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        setContentView(R.layout.activity_users);

        db = new DatabaseHelper(this);
        listViewUsers = findViewById(R.id.listViewUsers);
        btnBack = findViewById(R.id.btnUsersBack);

        adapter = new CustomUsersAdapter();
        listViewUsers.setAdapter(adapter);

        kullanicilariYukle();

        btnBack.setOnClickListener(v -> finish());
    }

    private void kullanicilariYukle() {
        userIds.clear();
        userNames.clear();
        userEmails.clear();
        recipeCounts.clear();

        Cursor c = db.getAllUsers();
        if (c != null) {
            while (c.moveToNext()) {
                // columns: u.id (0), u.name (1), u.email (2), tarif_sayisi (3)
                userIds.add(c.getInt(0));
                userNames.add(c.getString(1));
                userEmails.add(c.getString(2));
                recipeCounts.add(c.getInt(3));
            }
            c.close();
        }
        adapter.notifyDataSetChanged();
    }

    class CustomUsersAdapter extends BaseAdapter {

        @Override public int getCount() { return userNames.size(); }
        @Override public Object getItem(int i) { return null; }
        @Override public long getItemId(int i) { return 0; }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.user_item_layout, null);
            }

            TextView txtName = convertView.findViewById(R.id.txtUserName);
            TextView txtEmail = convertView.findViewById(R.id.txtUserEmail);
            TextView txtCount = convertView.findViewById(R.id.txtUserRecipeCount);

            txtName.setText(userNames.get(position));
            txtEmail.setText(userEmails.get(position));
            txtCount.setText(recipeCounts.get(position) + " Tarif");

            return convertView;
        }
    }
}
