import 'dart:convert';

class ItemsDataResponse {
  final List<GymItem> rows;

  ItemsDataResponse({
    required this.rows,
  });

  factory ItemsDataResponse.fromRawJson(String str) =>
      ItemsDataResponse.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory ItemsDataResponse.fromJson(Map<String, dynamic> json) =>
      ItemsDataResponse(
        rows: List<GymItem>.from(json["rows"].map((x) => GymItem.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "rows": List<dynamic>.from(rows.map((x) => x.toJson())),
      };
}

class GymItem {
  final String id;
  final String externalId;
  final String title;
  final String description;
  final int count;
  final double price;
  final String categoryId;
  final List<GymImage> images;
  final String supplierId;
  final String supplierName;
  int localCount = 0;

  GymItem({
    required this.id,
    required this.externalId,
    required this.title,
    required this.description,
    required this.count,
    required this.price,
    required this.categoryId,
    required this.images,
    required this.supplierId,
    required this.supplierName,
  });

  factory GymItem.fromRawJson(String str) => GymItem.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymItem.fromJson(Map<String, dynamic> json) {
    return GymItem(
      id: json["id"],
      externalId: json["ExternalId"],
      title: json["title"],
      description: json["description"],
      count: json["count"],
      price: json["price"] / 100,
      categoryId: json["categoryId"],
      images:
          List<GymImage>.from(json["images"].map((x) => GymImage.fromJson(x))),
      supplierId: json["supplier"] == null ? '' : json["supplier"]["id"] ?? '',
      supplierName: json["supplier"] == null
          ? ''
          : json["supplier"]["title"] ?? "Поставщик",
    );
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "ExternalId": externalId,
        "title": title,
        "description": description,
        "count": count,
        "price": price,
        "categoryId": categoryId,
        "images": List<dynamic>.from(images.map((x) => x.toJson())),
        "supplier":
            supplierId == '' ? null : {"id": supplierId, "title": supplierName},
      };
}

class GymImage {
  final String id;
  final dynamic deletedAt;
  final String url;

  GymImage({
    required this.id,
    required this.deletedAt,
    required this.url,
  });

  factory GymImage.fromRawJson(String str) =>
      GymImage.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymImage.fromJson(Map<String, dynamic> json) => GymImage(
        id: json["id"],
        deletedAt: json["deletedAt"],
        url: json["url"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "deletedAt": deletedAt,
        "url": url,
      };
}

class CategoryDataResponse {
  final List<GymCategory> rows;

  CategoryDataResponse({
    required this.rows,
  });

  factory CategoryDataResponse.fromRawJson(String str) =>
      CategoryDataResponse.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory CategoryDataResponse.fromJson(Map<String, dynamic> json) =>
      CategoryDataResponse(
        rows: List<GymCategory>.from(
            json["rows"].map((x) => GymCategory.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "rows": List<dynamic>.from(rows.map((x) => x.toJson())),
      };
}

class GymCategory {
  final String id;
  final String name;

  GymCategory({
    required this.id,
    required this.name,
  });

  factory GymCategory.fromRawJson(String str) =>
      GymCategory.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymCategory.fromJson(Map<String, dynamic> json) => GymCategory(
        id: json["id"],
        name: json["name"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
      };
}

class GymHistoryItem {
  String id;
  final String date;
  final String sum;
  final String photo;

  GymHistoryItem({
    required this.id,
    required this.date,
    required this.sum,
    required this.photo,
  });

  factory GymHistoryItem.fromRawJson(String str) =>
      GymHistoryItem.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymHistoryItem.fromJson(Map<String, dynamic> json) => GymHistoryItem(
        id: json["id"],
        date: json["date"],
        sum: json["sum"],
        photo: json["photo"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "date": date,
        "sum": sum,
        "photo": photo,
      };
}

class GymHistoryItemDetail {
  String id;
  final String date;
  final String sum;
  String? payUrl;
  final String receiver;
  final String email;
  final String address;
  final List<GymHistoryItemDetailProvider> providers;

  GymHistoryItemDetail({
    required this.id,
    required this.date,
    required this.sum,
    this.payUrl,
    required this.providers,
    required this.receiver,
    required this.email,
    required this.address,
  });

  factory GymHistoryItemDetail.fromRawJson(String str) =>
      GymHistoryItemDetail.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymHistoryItemDetail.fromJson(Map<String, dynamic> json) =>
      GymHistoryItemDetail(
        id: json["id"],
        date: json["date"],
        sum: json["sum"],
        receiver: json["receiver"],
        email: json["email"],
        address: json["address"],
        payUrl: json["pay_url"] as String?,
        providers: List<GymHistoryItemDetailProvider>.from(json["providers"]
            .map((x) => GymHistoryItemDetailProvider.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "date": date,
        "sum": sum,
        "pay_url": payUrl,
        "providers": List<dynamic>.from(providers.map((x) => x.toJson())),
        "receiver": receiver,
        "email": email,
        "address": address,
      };
}

class GymHistoryItemDetailProvider {
  final String id;
  final String name;
  String status;
  final List<GymHistoryItemDetailItem> items;

  GymHistoryItemDetailProvider({
    required this.id,
    required this.name,
    required this.status,
    required this.items,
  });

  factory GymHistoryItemDetailProvider.fromRawJson(String str) =>
      GymHistoryItemDetailProvider.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymHistoryItemDetailProvider.fromJson(Map<String, dynamic> json) {
    return GymHistoryItemDetailProvider(
      id: json["id"],
      name: json["name"],
      status: json["status"],
      items: List<GymHistoryItemDetailItem>.from(
          json["items"].map((x) => GymHistoryItemDetailItem.fromJson(x))),
    );
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "status": status,
        "items": List<dynamic>.from(items.map((x) => x.toJson())),
      };
}

class GymHistoryItemDetailItem {
  final String photo;
  final String id;
  final int count;
  final String name;
  final String price;

  GymHistoryItemDetailItem({
    required this.photo,
    required this.id,
    required this.count,
    required this.price,
    required this.name,
  });

  factory GymHistoryItemDetailItem.fromRawJson(String str) =>
      GymHistoryItemDetailItem.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymHistoryItemDetailItem.fromJson(Map<String, dynamic> json) =>
      GymHistoryItemDetailItem(
        photo: json["photo"],
        id: json["id"],
        count: json["count"],
        price: json["price"],
        name: json["name"],
      );

  Map<String, dynamic> toJson() => {
        "photo": photo,
        "id": id,
        "count": count,
        "price": price,
        "name": name,
      };
}
