import 'dart:convert';

import 'package:flutter/foundation.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 'asset_analytics_model.dart';
import 'traf_request_model.dart';

class TrafRequestProvider extends ChangeNotifier {
  bool isLoading = false;

  TrafRequestDataModel? trafRequestDataModel;

  Future<bool> addTraf(Map<String, dynamic> body) async {
    try {
      Response response = await ApiManager.instance.post(URLs.addTRAF, body: body);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // trafRequestDataModel = TrafRequestDataModel.fromJson(json.decode(response.body)["data"]);
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  }

  Future<int> getTRAFById(int trafId) async {
    trafRequestDataModel = null;
    try {
      isLoading = true;
      notifyListeners();
      Response response = await ApiManager.instance.get("${URLs.getTRAFById}?tRAFId=$trafId");
      if (response.statusCode >= 200 && response.statusCode < 300) {
        trafRequestDataModel = TrafRequestDataModel.fromJson(json.decode(response.body)["data"]);
      }
      isLoading = false;
      notifyListeners();
      return 0;
    } catch (error) {
      isLoading = false;
      notifyListeners();
      return -1;
    }
  }

  Future<AssetAnalytics?> getAssetAnalytics(int assetId) async {
    try {
    Response response = await ApiManager.instance.get("${URLs.getAssetAnalytics}?assetId=$assetId");
    if (response.statusCode >= 200 && response.statusCode < 300) {
      return AssetAnalytics.fromJson(json.decode(response.body)["data"]);
    }
    } catch (error) {
      print(error);
    }
    return null;
  }
}
