import 'package:flutter/material.dart';


class PayoutVolumesController extends ChangeNotifier {
  DateTimeRange _range;

  PayoutVolumesController({DateTime? now})
      : _range = _defaultRange(now ?? DateTime.now());

  DateTimeRange get range => _range;

  void setRange(DateTimeRange range) {
    final normalized = _normalizeRange(range);
    if (_isSameRange(_range, normalized)) return;
    _range = normalized;
    notifyListeners();
  }

  static DateTimeRange _defaultRange(DateTime now) {
    final local = now.toLocal();
    final start = DateTime(local.year, local.month, 1);
    final end = DateTime(
      local.year,
      local.month,
      local.day,
    );
    return DateTimeRange(start: start, end: end);
  }

  static DateTimeRange _normalizeRange(DateTimeRange range) {
    final start = DateTime(range.start.year, range.start.month, range.start.day);
    final end = DateTime(
      range.end.year,
      range.end.month,
      range.end.day,
    );
    return DateTimeRange(start: start, end: end);
  }

  static bool _isSameRange(DateTimeRange a, DateTimeRange b) {
    return a.start.isAtSameMomentAs(b.start) &&
        a.end.isAtSameMomentAs(b.end);
  }
}
