import 'package:flutter/material.dart';
import 'package:qadirneyriz/services/report/report.dart';
import 'package:qadirneyriz/utils/enums/status.dart';

class ReportState extends ChangeNotifier {
  ReportApi reportApi = ReportApi();
  // set date for filters
  String fromDate = '';
  String toDate = '';

  void setFromDates(String? newFromDate) {
    fromDate = newFromDate ?? '';
    notifyListeners();
  }

  void setToDates(String? newToDate) {
    toDate = newToDate ?? '';
    notifyListeners();
  }

  // is filter Not empty
  bool hasActiveFilters() {
    return selectedLocationId != null ||
        selectedManagersId != null ||
        selectedStatusId != null ||
        selectedSubjectId != null ||
        fromDate.isNotEmpty ||
        toDate.isNotEmpty;
  }

// get filters location meetings

  int? selectedLocationId;
  void selectLocation(int? locationId) {
    selectedLocationId = locationId;
    notifyListeners();
  }

// get filters subjects meetings

  int? selectedSubjectId;
  void selectSubject(int? subjectId) {
    selectedSubjectId = subjectId;
    notifyListeners();
  }
// get filters meeting managers

  int? selectedManagersId;
  void selectManager(int? managerId) {
    selectedManagersId = managerId;
    notifyListeners();
  }

// all meeting status filters

  int? selectedStatusId;
  void selectStatusMeeting(int? statusId) {
    selectedStatusId = statusId;
    notifyListeners();
  }

// download report meeting

  Map<String, Status> statusDownload = {};
  String? messageDownload;

  Future<Status> downloadReport(
      {String? fromDate,
      String? toDate,
      int? location,
      int? subject,
      int? meetingManager,
      required String format,
      int? status}) async {
    statusDownload[format] = Status.loading;
    notifyListeners();
    try {
      final result = await reportApi.downloadReportMeetings(
          fromDate: fromDate,
          toDate: toDate,
          location: location,
          subject: subject,
          meetingManager: meetingManager,
          format: format,
          status: status);

      if (result == null) {
        statusDownload[format] = Status.error;
      } else {
        if (result.isOk) {
          statusDownload[format] = Status.ready;
          messageDownload = result.message ?? '';
        } else {
          statusDownload[format] = Status.error;
        }
      }
    } catch (e) {
      statusDownload[format] = Status.error;
      // print(e);
    }
    // print(statusDownload[format]);
    notifyListeners();
    return statusDownload[format] ?? Status.error;
  }

// private meetings report items

  String fromDatePrivateMeeting = '';
  String toDatePrivateMeeting = '';

  void setFromDatesPrivateMeeting(String? newFromDate) {
    fromDatePrivateMeeting = newFromDate ?? '';
    notifyListeners();
  }

  void setToDatesPrivateMeeting(String? newToDate) {
    toDatePrivateMeeting = newToDate ?? '';
    notifyListeners();
  }

  // is filter Not empty
  bool hasActiveFiltersPrivateMeeting() {
    return selectedLocationIdPrivateMeeting != null ||
        selectedManagersIdPrivateMeeting != null ||
        selectedStatusIdPrivateMeeting != null ||
        selectedSubjectIdPrivateMeeting != null ||
        fromDatePrivateMeeting.isNotEmpty ||
        toDatePrivateMeeting.isNotEmpty;
  }

// get filters location PrivateMeeting

  int? selectedLocationIdPrivateMeeting;
  void selectLocationPrivateMeeting(int? locationId) {
    selectedLocationIdPrivateMeeting = locationId;
    notifyListeners();
  }

// get filters subjects PrivateMeeting

  int? selectedSubjectIdPrivateMeeting;
  void selectSubjectPrivateMeeting(int? subjectId) {
    selectedSubjectIdPrivateMeeting = subjectId;
    notifyListeners();
  }
// get filters PrivateMeeting

  int? selectedManagersIdPrivateMeeting;
  void selectManagerPrivateMeeting(int? managerId) {
    selectedManagersIdPrivateMeeting = managerId;
    notifyListeners();
  }

// all PrivateMeeting status filters

  int? selectedStatusIdPrivateMeeting;
  void selectStatusMeetingPrivateMeeting(int? statusId) {
    selectedStatusIdPrivateMeeting = statusId;
    notifyListeners();
  }
  // download report PrivateMeeting

  Map<String, Status> statusDownloadPrivateMeeting = {};
  String? messageDownloadPrivateMeeting;

  Future<Status> downloadReportPrivateMeeting(
      {String? fromDate,
      String? toDate,
      int? location,
      int? subject,
      int? meetingManager,
      required String format,
      int? status}) async {
    statusDownloadPrivateMeeting[format] = Status.loading;
    notifyListeners();
    try {
      final result = await reportApi.downloadReportPrivateMeetings(
          format: format,
          fromDate: fromDate,
          toDate: toDate,
          location: location,
          subject: subject,
          meetingManager: meetingManager,
          status: status);

      if (result == null) {
        statusDownloadPrivateMeeting[format] = Status.error;
      } else {
        if (result.isOk) {
          statusDownloadPrivateMeeting[format] = Status.ready;
          messageDownloadPrivateMeeting = result.message ?? '';
        } else {
          statusDownloadPrivateMeeting[format] = Status.error;
        }
      }
    } catch (e) {
      statusDownloadPrivateMeeting[format] = Status.error;
    }

    notifyListeners();
    return statusDownloadPrivateMeeting[format] ?? Status.error;
  }
}
