import 'dart:convert';

import 'package:http/http.dart';
import 'package:test_sa/models/new_models/mapped_sites.dart';
import 'package:test_sa/models/new_models/site.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';

import '../../controllers/api_routes/api_manager.dart';
import '../../controllers/api_routes/urls.dart';

class SiteProvider extends LoadingListNotifier<Site> {
  @override
  Future getDate() async {
    if (loading ?? false) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.getSiteAutoCompleteWithoutConditionSites);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Site.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}

class MappedSiteProvider extends LoadingListNotifier<MappedSite> {
  @override
  Future getDate() async {
    if (loading ?? false) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.post(URLs.getSites, body: {});
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => MappedSite.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
