import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../../logic/date.helper.dart';
import '../../repositories/beet.repositories.dart';

class Footer extends StatelessWidget {
  final Function(DateTime) onNewDaySelected;
  final BeetRepository beetRepository;
  final DateTime date;

  const Footer({
    required this.beetRepository,
    required this.date,
    required this.onNewDaySelected,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        IconButton(
          icon: const Icon(Icons.calendar_today),
          onPressed: () {
            _showDatePicker(context);
          },
        ),
        const SizedBox(width: 10),
        Text(DateFormat('dd.MM.yyyy').format(
          DateHelper.transformToCurrentYear(date),
        ))
      ]),
      Slider(
          value: DateHelper.getDayOfYear(date).toDouble(),
          min: 1,
          max: 365,
          activeColor: Colors.green,
          inactiveColor: Colors.lightGreen,
          onChanged: (value) {
            onNewDaySelected(
              DateHelper.getDateTimeByDayOfYear(value.toInt()),
            );
          })
    ]);
  }

  void _showDatePicker(BuildContext context) async {
    final selectedDate = await showDatePicker(
      context: context,
      initialDate: date,
      firstDate: DateTime(DateTime.now().year),
      lastDate: DateTime(DateTime.now().year + 1),
    );

    if (selectedDate != null) {
      onNewDaySelected(selectedDate);
    }
  }
}
