import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.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/modules/loan_module/models/loan_request_model.dart';
import 'dart:convert';

import 'incident_data_model.dart';

class IncidentProvider extends ChangeNotifier {
  Future<bool> addIncidentRequest(Map<String, dynamic> body) async {
    try {
      Response response = await ApiManager.instance.post(URLs.addIncident, body: body);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        String message = (jsonDecode(response.body)["data"] ?? "") + " " + (jsonDecode(response.body)["message"] ?? "");
        Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  }

  bool isLoading = false;

  Future<IncidentDataModel?> getIncidentById(int id) async {
    IncidentDataModel? loanData;
    try {
      Response response = await ApiManager.instance.get(URLs.getIncidentById + "?incidentId=$id");
      if (response.statusCode >= 200 && response.statusCode < 300) {
        loanData = IncidentDataModel.fromJson(json.decode(response.body)["data"]);
      }
    } catch (error) {
      print(error);
    }
    return loanData;
  }

  Future<Map<String, dynamic>?> getOvrTicketDetails(String ticketNumber) async {
    try {
      Response response = await ApiManager.instance.post(URLs.getOvrTicketDetails, body: {"ticketNumber": ticketNumber}, showToast: false);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (json.decode(response.body)["statusCode"] == 404) {
          Fluttertoast.showToast(msg: json.decode(response.body)["message"] ?? "OVR Ticket not found");
          return null;
        }
        return json.decode(response.body)["data"];
      }
    } catch (error) {
      print(error);
    }
    return null;
  }
}
