import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:test_sa/controllers/api_routes/api_manager.dart';
import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/models/employee.dart';
import 'package:test_sa/models/user.dart';

class NurseProvider extends ChangeNotifier {
  //reset provider data
  void reset() {
    _items = null;
    _stateCode = null;
  }

  int _stateCode;

  int get stateCode => _stateCode;
  List<Employee> _items;

  List<Employee> get nursesList => _items;
  bool _loading;

  bool get isLoading => _loading;

  set isLoading(bool isLoading) {
    _loading = isLoading;
    notifyListeners();
  }

  Future<int> getData() async {
    if (_loading == true) return -2;
    _loading = true;
    notifyListeners();
    Response response;
    try {
      response = await ApiManager.instance.get(URLs.getNurses);
      _stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body);
        _items = categoriesListJson.map((type) => Employee.fromJson(type)).toList();
        _items.sort((a, b) => a.name.compareTo(b.name));
      }
      _loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      _loading = false;
      _stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
