package com.example.cmpt362_group30_maze_game

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.cmpt362_group30_maze_game.leaderboard.LeaderboardViewModel

class HardFragment : Fragment() {

    private lateinit var viewModel: LeaderboardViewModel
    private lateinit var listView: ListView
    private lateinit var adapter: LeaderboardAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for the fragment
        return inflater.inflate(R.layout.fragment_hard, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        listView = view.findViewById<ListView>(R.id.listViewHard)

        // Initialize Adapter with empty list first
        adapter = LeaderboardAdapter(requireContext(), emptyList())
        listView.adapter = adapter

        // Initialize ViewModel
        viewModel = ViewModelProvider(this)[LeaderboardViewModel::class.java]

        // Observe the LiveData for Difficulty 2 (Hard)
        viewModel.queryTopTenByDifficulty(2).observe(viewLifecycleOwner) { entries ->
            // When data comes back from Firestore, update the adapter
            adapter.replaceData(entries)
        }
    }
}