import 'package:flutter/material.dart';
import 'package:qadirneyriz/models/meetings/meetings_model.dart';
import 'package:qadirneyriz/services/meetings/meetings.dart';
import 'package:qadirneyriz/utils/enums/status.dart';

class MeetingsState extends ChangeNotifier {
  // ذخیره فیلترهای قبلی برای مقایسه
  String? previousFromDate;
  String? previousToDate;
  int? previousLocationId;
  int? previousSubjectId;
  int? previousManagersId;
  int? previousStatusId;
  void setAllFiltersForThen() {
    previousFromDate = fromDate;
    previousToDate = toDate;
    previousLocationId = selectedLocationId;
    previousSubjectId = selectedSubjectId;
    previousManagersId = selectedManagersId;
    previousStatusId = selectedStatusId;
  }

  bool isAnyChangesInFilters() {
    if (previousFromDate != fromDate ||
        previousToDate != toDate ||
        previousLocationId != selectedLocationId ||
        previousSubjectId != selectedSubjectId ||
        previousManagersId != selectedManagersId ||
        previousStatusId != selectedStatusId) {
      return true;
    } else {
      return false;
    }
  }

  // api meetings
  MeetingsApi meetingsApi = MeetingsApi();

  // meetings list
  Status statusMeetings = Status.loading;
  Status paginationMeetings = Status.ready;
  MeetingsModel? meetingsModel;
  int pageMeetings = 1;
  int limitMeetings = 10;
  bool pageEndedMeetings = false;

  Future<Status> getMeetings(
      {bool refresh = false,
      String? fromDate,
      String? toDate,
      int? location,
      int? subject,
      int? meetingManager,
      int? meetingStatus}) async {
    if (refresh) {
      statusMeetings = Status.loading;
      notifyListeners();
    }

    if (meetingsModel != null && meetingsModel!.data!.isNotEmpty && !refresh) {
      statusMeetings = Status.ready;
      notifyListeners();
      pageMeetings = 1;
      pageEndedMeetings = false;
      paginationMeetings = Status.ready;
      meetingsModel = await meetingsApi.getMeetings(
          page: pageMeetings,
          count: limitMeetings,
          fromDate: fromDate,
          toDate: toDate,
          location: location,
          subject: subject,
          meetingManager: meetingManager,
          status: meetingStatus);
    } else {
      pageMeetings = 1;
      pageEndedMeetings = false;
      paginationMeetings = Status.ready;

      try {
        statusMeetings = Status.loading;
        notifyListeners();
        meetingsModel = await meetingsApi.getMeetings(
            page: pageMeetings,
            count: limitMeetings,
            fromDate: fromDate,
            toDate: toDate,
            location: location,
            subject: subject,
            meetingManager: meetingManager,
            status: meetingStatus);

        if (meetingsModel != null && meetingsModel!.data!.isNotEmpty) {
          if (meetingsModel!.data!.isNotEmpty) {
            statusMeetings = Status.ready;
          } else if (!meetingsModel!.hasData() && meetingsModel == null) {
            statusMeetings = Status.empty;
          } else if (meetingsModel!.data!.length < limitMeetings) {
            pageEndedMeetings = true;
            statusMeetings = Status.empty;
          }
        } else {
          statusMeetings = Status.empty;
        }
      } catch (e) {
        statusMeetings = Status.error;
      }
      notifyListeners();
    }
    notifyListeners();
    // print(statusMeetings);
    return statusMeetings;
  }

  Future<void> nextPageMeetings(
      {String? fromDate,
      String? toDate,
      int? location,
      int? subject,
      int? meetingManager,
      int? meetingStatus}) async {
    if (pageEndedMeetings == false && paginationMeetings == Status.ready) {
      paginationMeetings = Status.loading;
      notifyListeners();

      int p = pageMeetings;
      pageMeetings = p + 1;
      try {
        final adsPaginationModel = await meetingsApi.getMeetings(
            page: pageMeetings,
            count: limitMeetings,
            fromDate: fromDate,
            toDate: toDate,
            location: location,
            subject: subject,
            meetingManager: meetingManager,
            status: meetingStatus);

        if (adsPaginationModel.hasData()) {
          meetingsModel!.data!.addAll(adsPaginationModel.data!);

          if (adsPaginationModel.data!.length < limitMeetings) {
            pageEndedMeetings = true;
            paginationMeetings = Status.empty;
          } else {
            paginationMeetings = Status.ready;
          }
        } else if (!adsPaginationModel.hasData()) {
          pageEndedMeetings = true;
          paginationMeetings = Status.empty;
        }
      } catch (e) {
        pageEndedMeetings = true;
        paginationMeetings = Status.empty;
      }
      notifyListeners();
    }
  }

// set date for filters
  String fromDate = '';
  String toDate = '';

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

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

// clear filters
  void clearFilters() {
    selectedLocationId = null;
    selectedManagersId = null;
    selectedStatusId = null;
    selectedSubjectId = null;
    fromDate = '';
    toDate = '';
    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();
  }

// cancel meeting
  Status statusCancelMeeting = Status.empty;
  String? messageCancelMeeting;
  Map? errorsCancelMeeting;

  Future<Status> cancelMeeting({
    required int id,
  }) async {
    statusCancelMeeting = Status.loading;
    notifyListeners();
    try {
      final result = await meetingsApi.cancelMeetingApi(
        id: id,
      );
      if (result.isOk) {
        statusCancelMeeting = Status.ready;
        messageCancelMeeting = result.message;
      } else if (result.isOk == false) {
        // print(result.isOk);
        errorsCancelMeeting = result.errors;
        messageCancelMeeting = result.message;
        statusCancelMeeting = Status.error;
      } else {
        statusCancelMeeting = Status.error;
      }
      notifyListeners();
    } catch (e) {
      statusCancelMeeting = Status.error;
      // print(e);
    }
    notifyListeners();
    // print(statusCancelMeeting);
    return statusCancelMeeting;
  }

// accept meeting
  Status statusAcceptMeeting = Status.empty;
  String? messageAcceptMeeting;
  Map? errorsAcceptMeeting;

  Future<Status> acceptMeeting({
    required int id,
  }) async {
    statusAcceptMeeting = Status.loading;
    notifyListeners();
    try {
      final result = await meetingsApi.acceptMeetingApi(
        id: id,
      );
      if (result.isOk) {
        statusAcceptMeeting = Status.ready;
        messageAcceptMeeting = result.message;
      } else if (result.isOk == false) {
        // print(result.isOk);
        errorsAcceptMeeting = result.errors;
        messageAcceptMeeting = result.message;
        statusAcceptMeeting = Status.error;
      } else {
        statusAcceptMeeting = Status.error;
      }
      notifyListeners();
    } catch (e) {
      statusAcceptMeeting = Status.error;
      // print(e);
    }
    notifyListeners();
    // print(statusAcceptMeeting);
    return statusAcceptMeeting;
  }
}
