package com.rugvedsarika.easymath.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.fragment.app.activityViewModels
import com.rugvedsarika.easymath.R
import com.rugvedsarika.easymath.databinding.FragmentHomeBinding
import com.rugvedsarika.easymath.viewmodel.MathViewModel

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    
    private val viewModel: MathViewModel by activityViewModels {
        val app = requireActivity().application as com.rugvedsarika.easymath.EasyMathApp
        com.rugvedsarika.easymath.viewmodel.MathViewModelFactory(app.repository, app.audioManager)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.currentUser.observe(viewLifecycleOwner) { user ->
            binding.textUserName.text = user?.name ?: "Math Wizard!"
        }

        binding.cardPractice.setOnClickListener {
            findNavController().navigate(R.id.action_home_to_practice)
        }

        binding.cardLearn.setOnClickListener {
            findNavController().navigate(R.id.action_home_to_learn)
        }

        binding.cardSpeed.setOnClickListener {
             findNavController().navigate(R.id.action_home_to_speed)
        }

        binding.cardProgress.setOnClickListener {
             findNavController().navigate(R.id.action_home_to_progress)
        }

        binding.cardExam.setOnClickListener {
            findNavController().navigate(R.id.action_home_to_exam)
        }

        binding.cardAchievements.setOnClickListener {
            findNavController().navigate(R.id.action_home_to_achievements)
        }

        binding.cardDual.setOnClickListener {
            findNavController().navigate(R.id.action_home_to_dual)
        }

        binding.iconSettings.setOnClickListener {
            showSettingsDialog()
        }
    }

    private fun showSettingsDialog() {
        val app = requireActivity().application as com.rugvedsarika.easymath.EasyMathApp
        val audioManager = app.audioManager
        
        val dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_settings, null)
        val switchSound = dialogView.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.switchSound)
        val switchTts = dialogView.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.switchTts)

        switchSound.isChecked = audioManager.isSoundEnabled
        switchTts.isChecked = audioManager.isTtsEnabled

        switchSound.setOnCheckedChangeListener { _, isChecked ->
            audioManager.isSoundEnabled = isChecked
        }

        switchTts.setOnCheckedChangeListener { _, isChecked ->
            audioManager.isTtsEnabled = isChecked
        }

        androidx.appcompat.app.AlertDialog.Builder(requireContext())
            .setView(dialogView)
            .setPositiveButton("Close", null)
            .show()
    }

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