import 'package:flutter/material.dart';
import 'package:initial_folder/models/update_incomplete_profile_model.dart';
import 'package:initial_folder/models/user_info_incomplete_model.dart';
import 'package:initial_folder/services/user_info_service.dart';

enum ResultState { Loading, Success, NoData, HasData, Error }

class IncompleteProfileProvider with ChangeNotifier {
  final UserInfoService userInfoService;

  IncompleteProfileProvider({
    required this.userInfoService,
  });

  UpdateIncompleteProfileModel? _updateIncompleteProfileModel;
  UserInfoIncompleteModel? _userInfoIncompleteModel;
  ResultState? _state;
  bool? _isUserInfoComplete;
  String _message = '';

  UpdateIncompleteProfileModel? get updateIncompleteProfileModel =>
      _updateIncompleteProfileModel;
  UserInfoIncompleteModel? get userInfoIncompleteModel =>
      _userInfoIncompleteModel;
  ResultState? get state => _state;
  bool? get isUserInfoComplete => _isUserInfoComplete;
  String get message => _message;

  Future<bool> getUserInfoIncomplete() async {
    try {
      _state = ResultState.Loading;
      notifyListeners();
      UserInfoIncompleteModel userInfoIncomplete =
          await userInfoService.getUserInfoIncomplete();

      if (userInfoIncomplete.data == null) {
        _state = ResultState.NoData;
        _isUserInfoComplete = null;
        notifyListeners();
        return false;
      } else {
        if (userInfoIncomplete.status == 202) {
          _isUserInfoComplete = false;
        } else if (userInfoIncomplete.status == 200) {
          _isUserInfoComplete = true;
        }
        notifyListeners();
        _userInfoIncompleteModel = userInfoIncomplete;
        return true;
      }
    } catch (e) {
      _state = ResultState.Error;
      notifyListeners();
      _message = 'Error --> $e';
      _userInfoIncompleteModel = null;
      return false;
    }
  }

  Future<bool> updateIncompleteProfile({
    String? fullname,
    String? phone,
    String? email,
    String? newPassword,
    String? newConfirmPassword,
  }) async {
    try {
      _state = ResultState.Loading;
      notifyListeners();
      _updateIncompleteProfileModel =
          await UserInfoService().updateIncompleteProfile(
        fullname: fullname,
        phone: phone,
        email: email,
        newPassword: newPassword,
        newConfirmPassword: newConfirmPassword,
      );
      if (_updateIncompleteProfileModel != null) {
        if (_updateIncompleteProfileModel!.status == 200) {
          _state = ResultState.Success;
          notifyListeners();
          return true;
        }
      }

      return false;
    } catch (e) {
      print("Exception: $e");
      _updateIncompleteProfileModel = null;
      return false;
    }
  }
}
