package com.example.pap_findu.ui.map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.pap_findu.R;
import com.example.pap_findu.databinding.FragmentMapBinding;

public class MapFragment extends Fragment {

    private FragmentMapBinding binding;
    private com.google.android.gms.maps.GoogleMap mMap;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        binding = FragmentMapBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        // Initialize Map
        com.google.android.gms.maps.SupportMapFragment mapFragment = (com.google.android.gms.maps.SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);
        if (mapFragment != null) {
            mapFragment.getMapAsync(googleMap -> {
                mMap = googleMap;
                // Check permissions and enable My Location
                if (androidx.core.content.ContextCompat.checkSelfPermission(requireContext(),
                        android.Manifest.permission.ACCESS_FINE_LOCATION) == android.content.pm.PackageManager.PERMISSION_GRANTED) {
                    mMap.setMyLocationEnabled(true);

                    // Move camera to finding User or Default
                    com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(requireActivity())
                            .getLastLocation()
                            .addOnSuccessListener(location -> {
                                if (location != null) {
                                    com.google.android.gms.maps.model.LatLng current = new com.google.android.gms.maps.model.LatLng(
                                            location.getLatitude(), location.getLongitude());
                                    mMap.moveCamera(com.google.android.gms.maps.CameraUpdateFactory
                                            .newLatLngZoom(current, 15f));
                                } else {
                                    // Default to Escola Profissional de Vila do Conde
                                    com.google.android.gms.maps.model.LatLng school = new com.google.android.gms.maps.model.LatLng(
                                            41.3536, -8.7424);
                                    mMap.moveCamera(
                                            com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(school, 15f));
                                }
                            });
                } else {
                    // Request permissions (Simplified for brevity, usually should use
                    // RequestPermissionLauncher)
                    requestPermissions(new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, 100);
                }

                mMap.setOnMapLongClickListener(latLng -> {
                    android.content.Intent intent = new android.content.Intent(getContext(),
                            com.example.pap_findu.AddZoneActivity.class);
                    intent.putExtra("lat", latLng.latitude);
                    intent.putExtra("lng", latLng.longitude);
                    startActivity(intent);
                });
            });
        }

        // Implement SOS Button logic
        binding.sosButton.setOnClickListener(v -> {
            android.content.Intent intent = new android.content.Intent(android.content.Intent.ACTION_DIAL);
            intent.setData(android.net.Uri.parse("tel:112"));
            startActivity(intent);
        });

        // Add dummy listeners for other interactive elements
        binding.zoomInButton.setOnClickListener(v -> {
            // Placeholder: Zoom In logic would go here if we had a real map
            android.widget.Toast.makeText(getContext(), "Zoom In", android.widget.Toast.LENGTH_SHORT).show();
        });

        binding.zoomOutButton.setOnClickListener(v -> {
            // Placeholder: Zoom Out logic
            android.widget.Toast.makeText(getContext(), "Zoom Out", android.widget.Toast.LENGTH_SHORT).show();
        });

        binding.navigationFab.setOnClickListener(v -> {
            android.widget.Toast.makeText(getContext(), "Centrar Localização", android.widget.Toast.LENGTH_SHORT)
                    .show();
        });

        binding.messagesFab.setOnClickListener(v -> {
            android.content.Intent intent = new android.content.Intent(getContext(),
                    com.example.pap_findu.ChatActivity.class);
            startActivity(intent);
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
