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

import '../form_widget.dart';

class FieldMonthsRangepicker extends FormWidgetField {
  FieldMonthsRangepicker(
      {required this.name,
      required this.label,
      required this.filterValue,
      required this.onClear,
      required this.onSelected,
      required this.entries});

  final String name;
  final String label;
  final String? filterValue;
  final VoidCallback onClear;
  final void Function(DateRangePickerSelectionChangedArgs? args) onSelected;
  final List<String> entries;

  @override
  Widget get(theme) {
    return SfDateRangePicker(
      minDate: DateTime(2024, 1),
      maxDate: DateTime(2024, 12, 31),
      allowViewNavigation: false,
      showNavigationArrow: false,
      headerHeight: 0,
      view: DateRangePickerView.year,
      selectionMode: DateRangePickerSelectionMode.range,
      onSelectionChanged: onSelected,
    );
  }

  static List<String> getMonths(DateTime createdDate, int length) {
    DateFormat dateFormat = DateFormat.MMMM('de_DE');

    List<String> years = [];

    int currentYear = createdDate.year;
    int currentMonth = createdDate.month;
    for (int i = 0; i < length; i++) {
      createdDate = DateTime(currentYear, currentMonth + i);
      years.add(dateFormat.format(createdDate));

      if (currentMonth + i == 1) {
        currentYear += 1;
      }
    }
    return years;
  }
}
