

import 'dart:convert';

import 'address.dart';
import 'business.dart';
import 'cart_info.dart';
import 'cart_line_item.dart';
import 'distance_info.dart';
import 'fulfillment.dart';
import 'user_position.dart';

class Order {
  int? id;
  int? userId;
  int? businessId;
  Business? businessInfo;
  CartInfo? cartInfo;
  int? status;
  double? originPrice;
  double? discountPrice;
  double? totalPrice;
  String? consignee;
  String? phone;
  String? address;
  Address? shippingAddress;
  int? payMethod;
  String? remark;
  String? orderNum;
  int? bookedAt;
  int? createdAt;
  String? shippingMethod;
  String paymentStatus; // UNPAID, PAID, PARTIALPAID
  double? amountPaid;
  List<Fulfillment>? fulfillments;
  String? extraData;
  bool? hasComment;
  double? fee;
  UserPosition? shipperPosition;
  DistanceInfo? deliveryDistance;

  Order.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        userId = json['user_id'],
        businessId = json['business_id'],
        businessInfo = json['business_info'] != null ? Business.fromJson(json['business_info']) : null,
        cartInfo = json['cart_info'] != null ? CartInfo.fromJson(json['cart_info']) : null,
        status = json['status'],
        originPrice = double.parse(json['origin_price'].toString()),
        discountPrice = double.parse(json['discount_price'].toString()),
        totalPrice = double.parse(json['total_price'].toString()),
        consignee = json['consignee'],
        phone = json['phone'],
        address = json['address'],
        shippingAddress = (json['shipping_address'] != null) ? Address.fromJson(json['shipping_address']) : null,
        payMethod = json['pay_method'],
        remark = json['remark'],
        orderNum = json['order_num'],
        bookedAt = int.parse(json['booked_at'].toString()),
        createdAt = int.parse(json['created_at'].toString()),
        shippingMethod = json['shipping_method'],
        paymentStatus = json['payment_status'],
        amountPaid = double.parse(json['amount_paid'].toString()),
        fulfillments = (json['fulfillments'] as List).map((e) => Fulfillment.fromJson(e)).toList(),
        extraData = json['extra_data'],
        hasComment = json['has_comment'],
        fee = double.parse(json['fee'].toString()),
        shipperPosition = UserPosition.fromJson(json['shipper_position']),
        deliveryDistance = DistanceInfo.fromJson(json['delivery_distance']);

  Map<String, dynamic> toJson() => {
    'id': id,
    'user_id': userId,
    'business_id': businessId,
    'business_info': businessInfo,
    'cart_info': cartInfo,
    'status': status,
    'origin_price': originPrice,
    'discount_price': discountPrice,
    'total_price': totalPrice,
    'consignee': consignee,
    'phone': phone,
    'address': address,
    'shipping_address': shippingAddress,
    'pay_method': payMethod,
    'remark': remark,
    'order_num': orderNum,
    'booked_at': bookedAt,
    'created_at': createdAt,
    'shipping_method': shippingMethod,
    'payment_status': paymentStatus,
    'amount_paid': amountPaid,
    'fulfillments': fulfillments,
    'extra_data': extraData,
    'has_comment': hasComment,
    'fee': fee,
    'shipper_position': shipperPosition,
    'delivery_distance': deliveryDistance,
  };

  double getSubtotal() {
    double subtotal = 0.0;
    for (CartLineItem lineItem in cartInfo?.productList ?? <CartLineItem>[]) {
      subtotal += lineItem.getTotalPrice();
    }
    return subtotal;
  }

  @override
  String toString() {
    return json.encode(this);
  }
}