import 'dart:convert';

import 'package:http/http.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/new_models/task_request/task_request_model.dart';
import 'package:test_sa/models/new_models/task_request/task_type_model.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';
import '../../controllers/api_routes/api_manager.dart';
import '../../controllers/api_routes/urls.dart';


class TaskTypeProvider extends LoadingListNotifier<TaskType> {
  @override
  Future getData({int? id}) async {
    print('get data called...');
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.getTaskTypeByUserUrl);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => TaskType.fromJson(item)).toList();
        print('item lenght i got is ${items.length}');

      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
class TaskEvaluatorUserProvider extends LoadingListNotifier<TaskEvaluatorUser> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.getTaskEvaluatorUser);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List userListJson = json.decode(response.body);

        items = userListJson.map((item) => TaskEvaluatorUser.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}

class TaskJobTypeOfAlertProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.taskJobTypeOfAlert);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();

      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
class TaskJobRiskLevelProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.taskJobRiskLevel);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
class TaskJobResourceProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.taskJobResource);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
class TaskJobActionNeededProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.taskJobActionNeeded);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
class TaskJobImpactStatusProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.taskJobImpactStatus);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}




