import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/date_util.dart';
import 'package:hmg_patient_app_new/features/lab/models/Range.dart';

class DateRangeSelectorRangeViewModel extends ChangeNotifier {
  List months = [
    'Jan',
    'Feb',
    'Mar',
    'April',
    'May',
    'Jun',
    'July',
    'Aug',
    'Sep',
    'Oct',
    'Nov',
    'Dec'
  ];

  Range? _currentlySelectedRange;

  Range? get currentlySelectedRange => _currentlySelectedRange;

  set currentlySelectedRange(Range? value) {
    _currentlySelectedRange = value;
  }

  DateTime? _toDate;

  DateTime? get toDate => _toDate;

  set toDate(DateTime? value) {
    _toDate = value;

  }

  DateTime? _fromDate;

  DateTime? get fromDate => _fromDate;

  set fromDate(DateTime? value) {
    _fromDate = value;


  }

  DateRangeSelectorRangeViewModel();

  get getCurrentYear => DateTime.now().year;

  calculateDatesFromRange() {
    _toDate = DateTime.now().provideDateOnly();
    switch (_currentlySelectedRange) {
      case Range.WEEKLY:
        _fromDate = _toDate!.subtract(Duration(days: 7));
      case Range.LAST_MONTH:
        _fromDate = _toDate!.subtract(Duration(days: 30));
      case Range.LAST_6MONTH:
        _fromDate = _toDate!.subtract(Duration(days: (30 * 6)));
      case Range.THIS_YEAR:
        _toDate = DateTime(_toDate!.year, DateTime.december, 31);
        _fromDate = DateTime(_toDate!.year, DateTime.january, 01);
      default:
    }
    notifyListeners();
  }

  getDateString(DateTime? date){
    if(date == null) return "-";

    String year = date.year.toString().substring(2);
    return '${date.day} ${months[date.month-1]},$year';
  }

  flush(){
    toDate = null;
    fromDate = null;
    currentlySelectedRange = null;
    notifyListeners();
  }

  resetCurrentlySelectedRange(){
    currentlySelectedRange = null;
    notifyListeners();
  }

  /// Update both fromDate and toDate (for single date selection in V2)
  void updateSelectedDate(DateTime? date) {
    _fromDate = date;
    _toDate = date;
    _currentlySelectedRange = null;
    notifyListeners();
  }

}
