package com.example.cuida.ui.schedule;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.cuida.R;
import java.util.Calendar;

public class ScheduleAppointmentFragment extends Fragment {

    private ScheduleViewModel scheduleViewModel;
    private DatePicker datePicker;
    private RecyclerView recyclerTimeSlots;
    private Button btnConfirm;
    private TimeSlotAdapter timeSlotAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_schedule_appointment, container, false);

        scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class);

        datePicker = root.findViewById(R.id.datePicker);
        recyclerTimeSlots = root.findViewById(R.id.recycler_time_slots);
        btnConfirm = root.findViewById(R.id.btn_confirm_appointment);

        setupDatePicker();
        setupRecyclerView();
        setupObservers();

        btnConfirm.setOnClickListener(v -> scheduleViewModel.confirmAppointment());

        return root;
    }

    private void setupDatePicker() {
        Calendar today = Calendar.getInstance();
        datePicker.init(today.get(Calendar.YEAR), today.get(Calendar.MONTH),
                today.get(Calendar.DAY_OF_MONTH), (view, year, monthOfYear, dayOfMonth) -> {
                    scheduleViewModel.setDate(year, monthOfYear, dayOfMonth);
                });

        // Set initial valid date in VM
        scheduleViewModel.setDate(today.get(Calendar.YEAR), today.get(Calendar.MONTH),
                today.get(Calendar.DAY_OF_MONTH));

        // Prevent past dates
        datePicker.setMinDate(System.currentTimeMillis() - 1000);
    }

    private void setupRecyclerView() {
        timeSlotAdapter = new TimeSlotAdapter();
        timeSlotAdapter.setOnTimeSlotSelectedListener(time -> scheduleViewModel.setTime(time));

        recyclerTimeSlots.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recyclerTimeSlots.setAdapter(timeSlotAdapter);
    }

    private void setupObservers() {
        scheduleViewModel.getTimeSlots().observe(getViewLifecycleOwner(), slots -> {
            timeSlotAdapter.setTimeSlots(slots);
        });

        scheduleViewModel.getSaveSuccess().observe(getViewLifecycleOwner(), success -> {
            if (success) {
                Toast.makeText(getContext(), "Consulta agendada com sucesso!", Toast.LENGTH_SHORT).show();
                NavController navController = Navigation.findNavController(getView());
                navController.popBackStack();
            }
        });
    }
}
