import 'dart:convert';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';

import '../../../Helper/Logger.dart';
import '../../../Model/Request/Orders/Cancelorderrequest.dart';
import '../../../Model/Response/Summary/Cancelorderresponse.dart';
import '../../../Model/Response/Summary/Getsummarysresponse.dart';
import '../../../Model/Response/Summary/Ordersummaryresponse.dart';



class OrderSummaryProvider {




  // Fetch deliveries/orders from API
  Future<GetDeliveries?> getDelivery(String url) async {
    try {
      final response = await http.get(Uri.parse(url));

      if (response.statusCode == 200) {

        print(url);
        print(response);
        final data = json.decode(response.body);
        return GetDeliveries.fromJson(data);
      } else {
        logger.e('Failed to load orders. Status code: ${response.statusCode}');
        return null;
      }
    } catch (e) {
      logger.e('Error in getDelivery: $e');
      return null;
    }
  }

  Future<OrderSummary?> getOrderSummary(String url) async {
    try {
      final response = await http.get(Uri.parse(url));

      if (response.statusCode == 200) {

        print(url);
        print(response.body);
        final data = json.decode(response.body);
        return OrderSummary.fromJson(data);

      } else {
        logger.e('Failed to load order summary. Status code: ${response.statusCode}');
        return null;
      }
    } catch (e) {
      logger.e('Error in getOrderSummary: $e');
      return null;
    }
  }

  Future<CancelOrderResponse?> updateOrders(CancelOrderRequest data,String urldata,) async {
    CancelOrderResponse? updateOrder;
    try {

      final url = Uri.parse('$urldata');

      final response = await put(url,
          body: json.encode({
            "orderheaderid": data.orderheaderid,
            "orderstatus": "${data.orderstatus}",
            "cancelled": "${data.cancelled}"
          }),
          headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            // 'Authorization': '$token',
          });

      print('updateOrderresponsebody ${response.body}');
      print('updateOrderurlsssssssssss ${urldata.toString()}');
      Map<String, dynamic> parsedJson = json.decode(response.body.toString());

      updateOrder = CancelOrderResponse.fromJson(parsedJson);
      print('updateOrderinprovider${updateOrder}');

    } catch (e) {
      print(e.toString());
      print("error");
    }
    return updateOrder;
  }


}