package com.example.profile;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.annotation.NonNull;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.EditText;
import android.view.inputmethod.EditorInfo;
import android.util.Log;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;

public class Result extends AppCompatActivity {
    DrawerLayout drawerLayout;
    private static final String TAG = "Result";
    private EditText mNodeNameEditText;
    private DatabaseReference mDatabaseRef;

    private RecyclerView mRecyclerView;
    private RecyclerViewAdapter mAdapter;
    private ArrayList<Item> mData;

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

        drawerLayout = findViewById(R.id.drawer_activity);

        mNodeNameEditText = findViewById(R.id.node_name_edit_text);

        // Set up RecyclerView
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mData = new ArrayList<>();
        mAdapter = new RecyclerViewAdapter(Result.this, mData);
        mRecyclerView.setAdapter(mAdapter);

        // Set default node of the database
        mDatabaseRef = FirebaseDatabase.getInstance().getReference("users");

        mNodeNameEditText.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (i == EditorInfo.IME_ACTION_DONE) {
                String nodeName = mNodeNameEditText.getText().toString().trim();
                if (!nodeName.isEmpty()) {
                    retrieveData(nodeName); // Retrieve data for entered node name
                }
                return true;
            }
            return false;
        });

    }


    private void retrieveData(String nodeName) {
        DatabaseReference myRef = mDatabaseRef.child(nodeName); // Get reference for entered node name
    // Add the rest of your Click methods and existing code
        myRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                    String name = childSnapshot.getKey();
                    String data = childSnapshot.getValue().toString();

                    TextView nameTextView = new TextView(Result.this);
                    nameTextView.setText(name);
                    // Add any TextView attributes you need here

                    TextView dataTextView = new TextView(Result.this);
                    dataTextView.setText(data);
                    // Add any TextView attributes you need here

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.w(TAG, "Failed to read value.", databaseError.toException());
            }
        });
    }
}