import 'package:cheminova/models/profile_response.dart';
import 'package:cheminova/services/api_client.dart';
import 'package:cheminova/services/api_urls.dart';
import 'package:cheminova/services/secure__storage_service.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../screens/login_screen.dart';

class UserprofileProvider extends ChangeNotifier {
  final _apiClient = ApiClient();
  ProfileResponse? profileResponse;
  bool _isLoading = false;

  bool get isLoading => _isLoading;

  void setLoading(bool loading) {
    _isLoading = loading;
    notifyListeners();
  }

  Future<void> getProfile() async {
    setLoading(true);
    try {
      Response response = await _apiClient.get(ApiUrls.profileUrl);
      setLoading(false);
      if (response.statusCode == 200) {
        profileResponse = ProfileResponse.fromJson(response.data);
      } else {}
    } catch (e) {
      setLoading(false);
    }
  }

  Future<void> logOut(BuildContext context) async {
    Response response = await _apiClient.get(ApiUrls.logOutUrl);
    if (response.statusCode == 200) {
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(response.data['message'].toString())));
      SecureStorageService().clear();
      Navigator.pushReplacement(
          context, MaterialPageRoute(builder: (context) => const LoginPage()));
    }
  }
}
