import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../Helper/Constants/Apiconstants.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';
import '../../Provider/Summary/ordersprovider.dart';




class OrderSummaryRepository{
  OrderSummaryProvider orderSummaryProvider = OrderSummaryProvider();
  DateTime? now;
  int moduleId = 0;
  int tenantId = 0;
  int locationId = 0;
  int roleId = 0;
  int orderheaderId =0;

  DateTime getDate(DateTime d) => DateTime(d.year, d.month, d.day);



  Future<GetDeliveries?> getOrdersToday({String? keyword}) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var keywordParam = keyword ?? '';
    roleId = prefs.getInt('roleId')??0;
    tenantId = prefs.getInt('tenantId')??0;
    locationId = prefs.getInt('locationId')??0;
    var fromdate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var todate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var result = await orderSummaryProvider.getDelivery('${ApiConstants.getDeliveries}/?tenantid=$tenantId&fromdate=$fromdate&todate=$todate''&status=delivered&pageno=1&pagesize=30&keyword=$keywordParam&locationid=$locationId',);
    return result;
  }

  Future<GetDeliveries?> getOrdersWeek({String? keyword}) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    roleId = prefs.getInt('roleid')??0;
    tenantId = prefs.getInt('tenantId')??0;
    locationId = prefs.getInt('locationId')??0;
    final date = DateTime.now();
    var keywordParam = keyword ?? '';

    DateTime start = getDate(date.subtract(Duration(days: date.weekday - 1,)));
    DateTime week = getDate(
        date.add(Duration(days: DateTime.daysPerWeek - date.weekday)));


    var fromdate = DateFormat("yyyy-MM-dd")
        .format(DateFormat("yyyy-MM-dd", "en_US").parse(start.toString()));
    var todate= DateFormat("yyyy-MM-dd")
        .format(DateFormat("yyyy-MM-dd", "en_US").parse(week.toString()));
    var result = await orderSummaryProvider.getDelivery('${ApiConstants.getDeliveries}/?tenantid=$tenantId&fromdate=$fromdate'
        '&todate=$todate&status=delivered&pageno=1&pagesize=30&keyword=$keywordParam&locationid=$locationId');
    return result;
  }

  Future<GetDeliveries?> getOrdersMonth({String? keyword}) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    roleId = prefs.getInt('roleid')??0;
    tenantId = prefs.getInt('tenantId')??0;
    locationId = prefs.getInt('locationId')??0;
    now = DateTime.now();
    var keywordParam = keyword ?? '';
    DateTime lastDayOfMonth = new DateTime(now!.year, now!.month + 1, 0);
    DateTime firstDayOfMonth = new DateTime(now!.year, now!.month, 1);
    var fromdate  = DateFormat("yyyy-MM-dd").format(
        DateFormat("yyyy-MM-dd", "en_US").parse(firstDayOfMonth.toString()));
    var todate  = DateFormat("yyyy-MM-dd").format(
        DateFormat("yyyy-MM-dd", "en_US").parse(lastDayOfMonth.toString()));
    var result = await orderSummaryProvider.getDelivery('${ApiConstants.getDeliveries}/?tenantid=$tenantId&fromdate='
        '$fromdate&todate=$todate&status=delivered&pageno=1&pagesize=30&keyword=$keywordParam&locationid=$locationId');
    return result;
  }


  Future<OrderSummary?> getOrderSummary() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    tenantId = prefs.getInt('tenantId')!;
    locationId = prefs.getInt('locationId')??0;
    var result = await orderSummaryProvider.getOrderSummary('${ApiConstants.getOrderSummary}/?tenantid=$tenantId&locationid=$locationId');
    return result;
  }

  Future<CancelOrderResponse?>updateOrders(CancelOrderRequest data) async {
    var result = await orderSummaryProvider.updateOrders(data,'${ApiConstants.updateOrderStatus}');
    return result;
  }
}
