import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.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/medical_gas_inspection/models/delivery_notes_form_model.dart';
import 'package:test_sa/modules/medical_gas_inspection/models/medical_gas_data_model.dart';
import 'package:test_sa/modules/medical_gas_inspection/models/medical_gas_form_model.dart';
import 'package:test_sa/modules/medical_gas_inspection/models/medical_gas_supplier_model.dart';

class MedicalGasInspectionProvider extends ChangeNotifier {
  final pageItemNumber = 10;
  final searchPageItemNumber = 10;
  int pageNo = 1;

  void reset() {
    pageNo = 1;
    stateCode = null;
  }

  int? stateCode;
  bool isDetailLoading = false;
  bool nextPage = false;
  bool isLoading = false;
  bool isGasInfoLoading = false;

  Future<MedicalGasDataModel?> getMedicalGasById({int? requestId}) async {
    MedicalGasDataModel? assetDeliveryDataModel = MedicalGasDataModel();

    try {
      isLoading = true;
      notifyListeners();
      final response = await ApiManager.instance.get(URLs.getMedicalGasById + "?medicalGasRequestId=$requestId");
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        assetDeliveryDataModel = MedicalGasDataModel.fromJson(json.decode(response.body)["data"]);
      } else {
        assetDeliveryDataModel = null;
      }
      isLoading = false;
      notifyListeners();
      return assetDeliveryDataModel;
    } catch (e) {
      log("getMedicalGas details [error] : $e");
      isLoading = false;
      assetDeliveryDataModel = null;
      notifyListeners();
      return assetDeliveryDataModel;
    }
  }

  Future<bool> saveMedicalGas({
    required MedicalGasFormModel model,
  }) async {
    isLoading = true;
    try {
      final response = await ApiManager.instance.post(URLs.addMedicalGasRequest, body: model.toJson());
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        isLoading = false;
        notifyListeners();
        return true;
      } else {
        isLoading = false;
        notifyListeners();
        return false;
      }
    } catch (error) {
      isLoading = false;
      notifyListeners();
      return false;
    }
  }

  Future<MedicalGasSupplierModel?> getMedicalGasInfo({int? supplierId, int? itemTypeId, num? siteId}) async {
    MedicalGasSupplierModel? supplierInfo;
    Map<String, dynamic> payload = {"supplierId": supplierId, "itemTypeId": itemTypeId, "siteId": siteId};
    try {
      isGasInfoLoading = true;
      notifyListeners();
      final response = await ApiManager.instance.post("${URLs.getMedicalGasInfoBySupplier}", body: payload);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        final jsonData = json.decode(response.body)["data"];
        supplierInfo = MedicalGasSupplierModel.fromJson(jsonData);
      }
      isGasInfoLoading = false;
      notifyListeners();
      return supplierInfo;
    } catch (e) {
      log("get Supplier List [error] : $e");
      isGasInfoLoading = false;
      notifyListeners();
      return supplierInfo;
    }
  }
  Future<bool> updatedDeliveryNotes({
    required DeliveryNotesFormModel model,
  }) async {
    isLoading = true;
    try {
      final response = await ApiManager.instance.post(URLs.updateDeliveryNote, body: model.toJson());
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        isLoading = false;
        notifyListeners();
        return true;
      } else {
        isLoading = false;
        notifyListeners();
        return false;
      }
    } catch (error) {
      isLoading = false;
      notifyListeners();
      return false;
    }
  }
}
