package com.m2sdl.undercooked;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.m2sdl.undercooked.domain.Booking;
import com.m2sdl.undercooked.services.FirebaseBackendService;
import com.m2sdl.undercooked.utils.ActivityUtils;

import java.util.Calendar;

public class BookingActivity extends AppCompatActivity {
    private final FirebaseBackendService db = new FirebaseBackendService();
    private long dateSelected;
    private int numberOfPeopleSelected;
    private CalendarView calendarView;
    private SeekBar seekBar;
    private TextView numberOfPeopleText;
    private Button confirmButton;
    private double latitude;
    private double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking);
        latitude = ActivityUtils.getExtraAsDouble(this, ActivityUtils.LATITUDE_KEY);
        longitude = ActivityUtils.getExtraAsDouble(this, ActivityUtils.LONGITUDE_KEY);
        initialSetup();
        setupCalendarView();
        setupSeekBar();
        setupConfirmButton();
    }

    private void initialSetup() {
        calendarView = findViewById(R.id.calendarView);
        seekBar = findViewById(R.id.seekBar);
        numberOfPeopleText = findViewById(R.id.numberOfPeopleText);
        confirmButton = findViewById(R.id.button3);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        calendarView.setMinDate(calendar.getTimeInMillis());
        dateSelected = calendar.getTimeInMillis();

        numberOfPeopleSelected = 2;
        numberOfPeopleText.setText(String.valueOf(numberOfPeopleSelected));
        seekBar.setProgress(numberOfPeopleSelected - 1);
    }

    private void setupCalendarView() {
        calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, dayOfMonth, 0, 0, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            dateSelected = calendar.getTimeInMillis();
        });
    }

    private void setupSeekBar() {
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                numberOfPeopleSelected = progress + 1;
                numberOfPeopleText.setText(String.valueOf(numberOfPeopleSelected));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    private void setupConfirmButton() {
        confirmButton.setOnClickListener(v -> {
            Booking booking = new Booking(dateSelected, numberOfPeopleSelected, latitude, longitude);

            Log.d("BOOKING", "Date: " + booking.getDate() +
                    " | People: " + booking.getNumberOfPeople());

            db.addBooking(booking);

            Toast.makeText(BookingActivity.this, "Réservation effectuée !", Toast.LENGTH_SHORT).show();

            finish();
        });
    }
}
