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

class HomeState extends ChangeNotifier {
  HomeApi homeApi = HomeApi();

  Status todayMettingsStatus = Status.empty;
  TodayMeetingModel? todayMeetingsModel;
  Future<Status> getTodayMeetings({bool refresh = false}) async {
    todayMettingsStatus = Status.loading;
    notifyListeners();
    if (refresh) {
      todayMettingsStatus = Status.loading;
      notifyListeners();
    }

    if (todayMeetingsModel != null) {
      todayMettingsStatus = Status.ready;
      try {
        todayMeetingsModel = await homeApi.getTodayMeetings();
        if (todayMeetingsModel != null) {
          todayMettingsStatus = Status.ready;
        } else {
          todayMettingsStatus = Status.empty;
        }
      } catch (e) {
        todayMettingsStatus = Status.error;
      }
      notifyListeners();
    } else {
      try {
        todayMeetingsModel = await homeApi.getTodayMeetings();
        if (todayMeetingsModel != null) {
          todayMettingsStatus = Status.ready;
        } else {
          todayMettingsStatus = Status.empty;
        }
        notifyListeners();
      } catch (e) {
        todayMettingsStatus = Status.error;
      }
    }
    notifyListeners();
    return todayMettingsStatus;
  }

  // log out
  Status statusLogOut = Status.empty;
  String? messageLogOut;
  Map? errorsLogOut;

  Future<Status> logOut() async {
    statusLogOut = Status.loading;
    notifyListeners();
    try {
      final result = await homeApi.logOutApi();
      if (result == null) {
        statusLogOut = Status.error;
      } else {
        if (result.isOk) {
          statusLogOut = Status.ready;
          messageLogOut = result.message;
        } else if (result.isOk == false) {
          errorsLogOut = result.errors;
          messageLogOut = result.message;
          statusLogOut = Status.error;
        } else {
          statusLogOut = Status.error;
        }
      }
      notifyListeners();
    } catch (e) {
      statusLogOut = Status.error;
      // print(e);
    }
    notifyListeners();
    // print(statusLogOut);
    return statusLogOut;
  }

// edit profile
  Status statusEditProfile = Status.empty;
  String? messageEditProfile;
  Map? errorsEditProfile;

  Future<Status> editProfile({String? name, String? pass}) async {
    statusEditProfile = Status.loading;
    notifyListeners();
    try {
      final result = await homeApi.editProfile(name: name, password: pass);
      if (result == null) {
        statusEditProfile = Status.error;
      } else {
        if (result.isOk) {
          statusEditProfile = Status.ready;
          messageEditProfile = result.message;
        } else if (result.isOk == false) {
          errorsEditProfile = result.errors;
          messageEditProfile = result.message;
          statusEditProfile = Status.error;
        } else {
          statusEditProfile = Status.error;
        }
      }
      notifyListeners();
    } catch (e) {
      statusEditProfile = Status.error;
      // print(e);
    }
    notifyListeners();
    print(statusEditProfile);
    return statusEditProfile;
  }
}
