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

class OrderModel {
  late int id;
  late String status;
  late String statusLabel;
  String? channelName;
  int? isGuest;
  String? customerEmail;
  String? customerFirstName;
  String? customerLastName;
  String? shippingMethod;
  String? shippingTitle;
  String? paymentTitle;
  String? shippingDescription;
  String? couponCode;
  int? isGift;
  late int totalItemCount;
  late int totalQtyOrdered;
  String? channelCurrencyCode;
  String? orderCurrencyCode;
  double? grandTotal;
  late String formattedGrandTotal;
  late double totalWeight;
  double? grandTotalInvoiced;
  late String formattedGrandTotalInvoiced;
  double? grandTotalRefunded;
  String? formattedGrandTotalRefunded;
  double? subTotal;
  late String formattedSubTotal;
  double? subTotalInvoiced;
  String? formattedSubTotalInvoiced;
  double? subTotalRefunded;
  String? formattedSubTotalRefunded;
  double? discountPercent;
  late double discountAmount;
  late String formattedDiscountAmount;
  double? discountInvoiced;
  String? formattedDiscountInvoiced;
  double? discountRefunded;
  String? formattedDiscountRefunded;
  double? shippingAmount;
  late String formattedShippingAmount;
  String? shippingInvoiced;
  String? formattedShippingInvoiced;
  String? shippingRefunded;
  String? formattedShippingRefunded;
  late ShippingAddress shippingAddress;
  late BillingAddress billingAddress;
  late List<OrderVendor> vendors;
  late String createdAt;
  late List<InvoiceModel> invoices;
  // List<Null>? shipments;

  OrderModel({
    required this.id,
    required this.status,
    required this.statusLabel,
    this.channelName,
    this.isGuest,
    this.customerEmail,
    this.customerFirstName,
    this.customerLastName,
    this.shippingMethod,
    this.shippingTitle,
    this.paymentTitle,
    this.shippingDescription,
    this.couponCode,
    this.isGift,
    required this.totalItemCount,
    required this.totalQtyOrdered,
    this.channelCurrencyCode,
    this.orderCurrencyCode,
    this.grandTotal,
    required this.formattedGrandTotal,
    required this.totalWeight,
    this.grandTotalInvoiced,
    required this.formattedGrandTotalInvoiced,
    this.grandTotalRefunded,
    this.formattedGrandTotalRefunded,
    this.subTotal,
    required this.formattedSubTotal,
    this.subTotalInvoiced,
    this.formattedSubTotalInvoiced,
    this.subTotalRefunded,
    this.formattedSubTotalRefunded,
    this.discountPercent,
    required this.discountAmount,
    required this.formattedDiscountAmount,
    this.discountInvoiced,
    this.formattedDiscountInvoiced,
    this.discountRefunded,
    this.formattedDiscountRefunded,
    this.shippingAmount,
    required this.formattedShippingAmount,
    this.shippingInvoiced,
    this.formattedShippingInvoiced,
    this.shippingRefunded,
    this.formattedShippingRefunded,
    required this.shippingAddress,
    required this.billingAddress,
    required this.vendors,
    required this.createdAt,
    required this.invoices,
    // this.shipments
  });

  OrderModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    status = json['status'];
    statusLabel = json['status_label'];
    channelName = json['channel_name'];
    isGuest = json['is_guest'];
    customerEmail = json['customer_email'];
    customerFirstName = json['customer_first_name'];
    customerLastName = json['customer_last_name'];
    shippingMethod = json['shipping_method'];
    shippingTitle = json['shipping_title'];
    paymentTitle = json['payment_title'];
    shippingDescription = json['shipping_description'];
    couponCode = json['coupon_code'];
    isGift = json['is_gift'];
    totalItemCount = json['total_item_count'];
    totalQtyOrdered = json['total_qty_ordered'];
    channelCurrencyCode = json['channel_currency_code'];
    orderCurrencyCode = json['order_currency_code'];
    grandTotal = json['grand_total'] + .0 ?? 0.0;
    formattedGrandTotal = json['formatted_grand_total'];
    totalWeight = json['total_weight'] + .0 ?? 0.0;
    grandTotalInvoiced = json['grand_total_invoiced'] + .0 ?? 0.0;
    formattedGrandTotalInvoiced = json['formatted_grand_total_invoiced'];
    grandTotalRefunded = json['grand_total_refunded'] + .0 ?? 0.0;
    formattedGrandTotalRefunded = json['formatted_grand_total_refunded'];
    subTotal = json['sub_total'] + .0 ?? 0.0;
    formattedSubTotal = json['formatted_sub_total'];
    subTotalInvoiced = json['sub_total_invoiced'] + .0 ?? 0.0;
    formattedSubTotalInvoiced = json['formatted_sub_total_invoiced'];
    subTotalRefunded = json['sub_total_refunded'] + .0 ?? 0.0;
    formattedSubTotalRefunded = json['formatted_sub_total_refunded'];
    discountPercent = json['discount_percent'] + .0 ?? 0.0;
    discountAmount = json['discount_amount'] + .0 ?? 0.0;
    formattedDiscountAmount = json['formatted_discount_amount'];
    discountInvoiced = json['discount_invoiced'] + .0 ?? 0.0;
    formattedDiscountInvoiced = json['formatted_discount_invoiced'];
    discountRefunded = json['discount_refunded'] + .0 ?? 0.0;
    formattedDiscountRefunded = json['formatted_discount_refunded'];
    shippingAmount = json['shipping_amount'] + .0 ?? 0.0;
    formattedShippingAmount = json['formatted_shipping_amount'];
    shippingInvoiced = json['shipping_invoiced'];
    formattedShippingInvoiced = json['formatted_shipping_invoiced'];
    shippingRefunded = json['shipping_refunded'];
    formattedShippingRefunded = json['formatted_shipping_refunded'];
    shippingAddress = new ShippingAddress.fromJson(json['shipping_address']);
    billingAddress = new BillingAddress.fromJson(json['billing_address']);
    vendors = OrderVendor.getVendors(json['vendors']);
    createdAt = DateFormat('dd-MM-yyyy, hh:mm').format(DateTime.now());
    invoices = <InvoiceModel>[];
    if (json['invoices'] != null) {
      json['invoices'].forEach((v) {
        invoices.add(new InvoiceModel.fromJson(v));
      });
    }
    /*if (json['shipments'] != null) {
      shipments = <Null>[];
      json['shipments'].forEach((v) {
        shipments!.add(new Null.fromJson(v));
      });
    } */
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['status'] = this.status;
    data['status_label'] = this.statusLabel;
    data['channel_name'] = this.channelName;
    data['is_guest'] = this.isGuest;
    data['customer_email'] = this.customerEmail;
    data['customer_first_name'] = this.customerFirstName;
    data['customer_last_name'] = this.customerLastName;
    data['shipping_method'] = this.shippingMethod;
    data['shipping_title'] = this.shippingTitle;
    data['payment_title'] = this.paymentTitle;
    data['shipping_description'] = this.shippingDescription;
    data['coupon_code'] = this.couponCode;
    data['is_gift'] = this.isGift;
    data['total_item_count'] = this.totalItemCount;
    data['total_qty_ordered'] = this.totalQtyOrdered;
    data['channel_currency_code'] = this.channelCurrencyCode;
    data['order_currency_code'] = this.orderCurrencyCode;
    data['grand_total'] = this.grandTotal;
    data['formatted_grand_total'] = this.formattedGrandTotal;
    data['grand_total_invoiced'] = this.grandTotalInvoiced;
    data['formatted_grand_total_invoiced'] = this.formattedGrandTotalInvoiced;
    data['grand_total_refunded'] = this.grandTotalRefunded;
    data['formatted_grand_total_refunded'] = this.formattedGrandTotalRefunded;
    data['sub_total'] = this.subTotal;
    data['formatted_sub_total'] = this.formattedSubTotal;
    data['sub_total_invoiced'] = this.subTotalInvoiced;
    data['formatted_sub_total_invoiced'] = this.formattedSubTotalInvoiced;
    data['sub_total_refunded'] = this.subTotalRefunded;
    data['formatted_sub_total_refunded'] = this.formattedSubTotalRefunded;
    data['discount_percent'] = this.discountPercent;
    data['discount_amount'] = this.discountAmount;
    data['formatted_discount_amount'] = this.formattedDiscountAmount;
    data['discount_invoiced'] = this.discountInvoiced;
    data['formatted_discount_invoiced'] = this.formattedDiscountInvoiced;
    data['discount_refunded'] = this.discountRefunded;
    data['formatted_discount_refunded'] = this.formattedDiscountRefunded;
    data['shipping_amount'] = this.shippingAmount;
    data['formatted_shipping_amount'] = this.formattedShippingAmount;
    data['shipping_invoiced'] = this.shippingInvoiced;
    data['formatted_shipping_invoiced'] = this.formattedShippingInvoiced;
    data['shipping_refunded'] = this.shippingRefunded;
    data['formatted_shipping_refunded'] = this.formattedShippingRefunded;
    data['shipping_address'] = this.shippingAddress.toJson();
    data['billing_address'] = this.billingAddress.toJson();

    /* if (this.invoices != null) {
      data['invoices'] = this.invoices!.map((v) => v.toJson()).toList();
    }
    if (this.shipments != null) {
      data['shipments'] = this.shipments!.map((v) => v.toJson()).toList();
    } */
    return data;
  }
}

class ShippingAddress {
  int? id;
  String? email;
  String? firstName;
  String? lastName;
  late List<String> address1;
  String? country;
  String? countryName;
  String? state;
  String? city;
  String? postcode;
  String? phone;

  ShippingAddress({
    this.id,
    this.email,
    this.firstName,
    this.lastName,
    required this.address1,
    this.country,
    this.countryName,
    this.state,
    this.city,
    this.postcode,
    this.phone,
  });

  ShippingAddress.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    email = json['email'];
    firstName = json['first_name'];
    lastName = json['last_name'];
    address1 = json['address1'].cast<String>();
    country = json['country'];
    countryName = json['country_name'];
    state = json['state'];
    city = json['city'];
    postcode = json['postcode'];
    phone = json['phone'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['email'] = this.email;
    data['first_name'] = this.firstName;
    data['last_name'] = this.lastName;
    data['address1'] = this.address1;
    data['country'] = this.country;
    data['country_name'] = this.countryName;
    data['state'] = this.state;
    data['city'] = this.city;
    data['postcode'] = this.postcode;
    data['phone'] = this.phone;
    return data;
  }
}

class BillingAddress {
  int? id;
  String? email;
  String? firstName;
  String? lastName;
  List<String>? address1;
  String? country;
  String? countryName;
  String? state;
  String? city;
  String? postcode;
  String? phone;

  BillingAddress(
      {this.id, this.email, this.firstName, this.lastName, this.address1, this.country, this.countryName, this.state, this.city, this.postcode, this.phone});

  BillingAddress.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    email = json['email'];
    firstName = json['first_name'];
    lastName = json['last_name'];
    address1 = json['address1'].cast<String>();
    country = json['country'];
    countryName = json['country_name'];
    state = json['state'];
    city = json['city'];
    postcode = json['postcode'];
    phone = json['phone'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['email'] = this.email;
    data['first_name'] = this.firstName;
    data['last_name'] = this.lastName;
    data['address1'] = this.address1;
    data['country'] = this.country;
    data['country_name'] = this.countryName;
    data['state'] = this.state;
    data['city'] = this.city;
    data['postcode'] = this.postcode;
    data['phone'] = this.phone;
    return data;
  }
}
