package com.rugvedsarika.landarea.ui

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.fragment.app.Fragment
import com.rugvedsarika.landarea.databinding.FragmentGeometryBinding
import com.rugvedsarika.landarea.logic.GeometryCalculator
import java.util.Locale

class GeometryFragment : Fragment() {

    private var _binding: FragmentGeometryBinding? = null
    private val binding get() = _binding!!

    private val shapes = listOf("Rectangle", "Triangle", "Trapezium", "Circle")

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

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

    private fun setupSpinner() {
        val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_dropdown_item_1line, shapes)
        binding.spinnerShape.setAdapter(adapter)
        binding.spinnerShape.setOnItemClickListener { _, _, position, _ ->
            updateInputVisibility(shapes[position])
            calculate()
        }
    }

    private fun updateInputVisibility(shape: String) {
        when (shape) {
            "Rectangle" -> {
                binding.input1Layout.hint = "Length"
                binding.input2Layout.hint = "Width"
                binding.input1Layout.visibility = View.VISIBLE
                binding.input2Layout.visibility = View.VISIBLE
                binding.input3Layout.visibility = View.GONE
            }
            "Triangle" -> {
                binding.input1Layout.hint = "Base"
                binding.input2Layout.hint = "Height"
                binding.input1Layout.visibility = View.VISIBLE
                binding.input2Layout.visibility = View.VISIBLE
                binding.input3Layout.visibility = View.GONE
            }
            "Trapezium" -> {
                binding.input1Layout.hint = "Side A"
                binding.input2Layout.hint = "Side B"
                binding.input3Layout.hint = "Height"
                binding.input1Layout.visibility = View.VISIBLE
                binding.input2Layout.visibility = View.VISIBLE
                binding.input3Layout.visibility = View.VISIBLE
            }
            "Circle" -> {
                binding.input1Layout.hint = "Radius"
                binding.input1Layout.visibility = View.VISIBLE
                binding.input2Layout.visibility = View.GONE
                binding.input3Layout.visibility = View.GONE
            }
        }
    }

    private fun setupInputs() {
        val watcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                calculate()
            }
            override fun afterTextChanged(s: Editable?) {}
        }
        binding.etInput1.addTextChangedListener(watcher)
        binding.etInput2.addTextChangedListener(watcher)
        binding.etInput3.addTextChangedListener(watcher)
    }

    private fun calculate() {
        val shape = binding.spinnerShape.text.toString()
        val in1 = binding.etInput1.text.toString().toDoubleOrNull() ?: 0.0
        val in2 = binding.etInput2.text.toString().toDoubleOrNull() ?: 0.0
        val in3 = binding.etInput3.text.toString().toDoubleOrNull() ?: 0.0

        val area = when (shape) {
            "Rectangle" -> GeometryCalculator.calculateRectangle(in1, in2)
            "Triangle" -> GeometryCalculator.calculateTriangle(in1, in2)
            "Trapezium" -> GeometryCalculator.calculateTrapezium(in1, in2, in3)
            "Circle" -> GeometryCalculator.calculateCircle(in1)
            else -> 0.0
        }

        binding.tvShapeResult.text = String.format(Locale.getDefault(), "%.2f", area)
    }

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