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

class PrivateMeetingSummaryState extends ChangeNotifier {
  // send add meeting minute
  PrivateMeetingsApi meetingsApi = PrivateMeetingsApi();
  Status statusMinuteMeeting = Status.empty;
  String? messageMinuteMeeting;
  Map? errorsMinuteMeeting;

  Future<Status> addMinuteMeeting(
      {required int id,
      required String description,
      required List<PlatformFile> meetingFiles}) async {
    statusMinuteMeeting = Status.loading;
    notifyListeners();
    try {
      final result = await meetingsApi.addMeetingMinuteApi(
          id: id, description: description, meetingFiles: meetingFiles);
      if (result.isOk) {
        statusMinuteMeeting = Status.ready;
        messageMinuteMeeting = result.message;
      } else if (result.isOk == false) {
        // print(result.isOk);
        errorsMinuteMeeting = result.errors;
        messageMinuteMeeting = result.message;
        statusMinuteMeeting = Status.error;
      } else {
        statusMinuteMeeting = Status.error;
      }
      notifyListeners();
    } catch (e) {
      statusMinuteMeeting = Status.error;
      // print(e);
    }
    notifyListeners();
    // print(statusMinuteMeeting);
    return statusMinuteMeeting;
  }

  List<PlatformFile>? selectedFiles;

  Future<void> pickFiles() async {
    final result = await FilePicker.platform.pickFiles(
      allowMultiple: true,
    );

    if (result != null) {
      selectedFiles = result.files;

      notifyListeners();
    }
  }

  void removeFile(int index) {
    selectedFiles!.removeAt(index);
    notifyListeners();
  }

// download summary

  Status statusDownload = Status.empty;
  String? messageDownload;

  Future<Status> downloadSummary({required int id}) async {
    statusDownload = Status.loading;
    notifyListeners();
    try {
      final result = await meetingsApi.downloadSummary(id: id);

      if (result == null) {
        statusDownload = Status.error;
      } else {
        if (result.isOk) {
          statusDownload = Status.ready;
          messageDownload = result.message ?? '';
        } else {
          statusDownload = Status.error;
        }
      }
    } catch (e) {
      statusDownload = Status.error;
    }

    notifyListeners();
    return statusDownload;
  }

// get file string
  Map<int, Status> stringsFilsStatus = {};
  Map<int, List<String>> filesStringModel = {};
  Map<int, List<String>?> messageStringFiles = {};
  Map? errorsStringFiles;
  Future<Map<int, Status>> getStringFiles({required int id}) async {
    if (filesStringModel[id] != null && filesStringModel[id]!.isNotEmpty) {
      try {
        filesStringModel[id] = await meetingsApi.getListStringFils(id: id);
        print('${filesStringModel[id]}');

        stringsFilsStatus[id] = Status.ready;
        print('${filesStringModel}  filesStringModel[id]');
      } catch (e) {
        stringsFilsStatus[id] = Status.error;
        print('$e');
      }
    } else {
      stringsFilsStatus[id] = Status.ready;
      notifyListeners();
      try {
        filesStringModel[id] = await meetingsApi.getListStringFils(id: id);
        print('${filesStringModel[id]}');

        stringsFilsStatus[id] = Status.ready;
        print('${filesStringModel}  filesStringModel[id]');
      } catch (e) {
        stringsFilsStatus[id] = Status.error;
        print('$e');
      }
    }

    notifyListeners();
    print('${stringsFilsStatus} stringsFilsStatus');

    return stringsFilsStatus;
  }

// delete file of summary
  Status statusDeleteFile = Status.empty;
  String? messageDeleteFile;
  Map? errorsDeleteFile;

  Future<Status> deleteFileSummary({
    required int id,
    required String text,
  }) async {
    statusDeleteFile = Status.loading;
    notifyListeners();
    try {
      final result = await meetingsApi.deleteFileSummary(id: id, text: text);
      if (result.isOk) {
        statusDeleteFile = Status.ready;
        messageDeleteFile = result.message;
      } else if (result.isOk == false) {
        print(result.isOk);
        errorsDeleteFile = result.errors;
        messageDeleteFile = result.message;
        statusDeleteFile = Status.error;
      }
      notifyListeners();
    } catch (e) {
      statusDeleteFile = Status.error;
      print(e);
    }
    notifyListeners();
    print(statusDeleteFile);
    return statusDeleteFile;
  }

}
