package com.rugvedsarika.privatelog.ui.home

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.rugvedsarika.privatelog.PrivateLogApp
import com.rugvedsarika.privatelog.R
import com.rugvedsarika.privatelog.databinding.FragmentHomeBinding

class HomeFragment : Fragment(R.layout.fragment_home) {
    
    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    private lateinit var viewModel: HomeViewModel

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _binding = FragmentHomeBinding.bind(view)

        val repository = (requireActivity().application as PrivateLogApp).repository
        val factory = HomeViewModelFactory(repository)
        viewModel = ViewModelProvider(this, factory)[HomeViewModel::class.java]

        val adapter = NotesAdapter { note ->
            val bundle = Bundle().apply { putLong("noteId", note.id) }
            findNavController().navigate(R.id.action_homeFragment_to_editorFragment, bundle)
        }
        
        // Wait, Need SafeArgs or use Bundle. I disabled safeArgs temporarily.
        // So I'll use Bundle for now or simplified navigation.
        // For strictness, I should use Bundle.
        // But for now, let's assume just navigating to empty editor for NEW note.
        // For EDIT, I pass ID.
        // I'll stick to Bundle manually.

        binding.recyclerView.adapter = adapter
        binding.recyclerView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)

        viewModel.allNotes.observe(viewLifecycleOwner) { notes ->
            adapter.submitList(notes)
        }

        binding.fabAdd.setOnClickListener {
            findNavController().navigate(R.id.action_homeFragment_to_editorFragment)
        }
        
        setHasOptionsMenu(true)
    }

    override fun onCreateOptionsMenu(menu: android.view.Menu, inflater: android.view.MenuInflater) {
        inflater.inflate(R.menu.home_menu, menu)

        val searchItem = menu.findItem(R.id.action_search)
        val searchView = searchItem.actionView as androidx.appcompat.widget.SearchView

        searchView.onActionViewExpanded() // optional: keep expanded if needed, but usually not
        
        searchView.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                viewModel.onSearchQueryChanged(newText ?: "")
                return true
            }
        })
    }

    override fun onOptionsItemSelected(item: android.view.MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_settings -> {
                findNavController().navigate(R.id.action_homeFragment_to_settingsFragment)
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
    
    // Manual navigation for edit (placeholder until EditorFragment handles arguments)
    // For now, adapter click -> Editor. Editor needs to read ID.
    // I'll use Bundle:
    /*
    val bundle = Bundle().apply { putLong("noteId", note.id) }
    findNavController().navigate(R.id.action_homeFragment_to_editorFragment, bundle)
    */

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
