import 'dart:math';

import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/models/advertisment_models/special_service_model.dart';
import 'package:mc_common_app/models/chat_models/chat_message_model.dart';
import 'package:mc_common_app/utils/enums.dart';

class AdDetailsModel {
  int? id;
  String? startdate;
  String? enddate;
  Vehicle? vehicle;
  List<SpecialServiceModelForAds>? specialservice;

  // List<Null>? reserved;
  int? statusID;
  String? statuslabel;
  double? adsDurationPrice;
  double? adsDurationDiscount;
  double? adsDurationDiscountPrice;
  String? comment;
  bool? active;
  bool? isPaid;
  bool? isSubscription;
  bool? isVerified;
  double? netPrice;
  double? specialServiceTotalPrice;
  double? taxPrice;
  double? totalPrice;
  String? userID;
  int? vehiclePostingID;
  String? qrCodePath;
  bool? isCustomerAcknowledged;
  int? createdByRole;
  int? totalViews;
  String? createdOn;
  double? priceExcludingDiscount;
  double? reservePrice;
  bool? isMCHandled;
  String? modifiedOn;
  AdPostStatus? adPostStatus;
  AdReserveStatus? adReserveStatus;
  bool? isMyAd;
  bool? isReservedByMe;
  String? phoneNo;
  String? whatsAppNo;
  String? adOwnerName;
  String? adOwnerEmail;
  AdOwnerDetails? adOwnerDetails;
  String? warrantyYears;
  CreatedByRoleEnum? createdByRoleEnum;
  List<ChatMessageModel>? adMessages;

  AdDetailsModel({
    this.id,
    this.startdate,
    this.enddate,
    this.vehicle,
    this.specialservice,
    // this.reserved,
    this.statusID,
    this.statuslabel,
    this.adsDurationPrice,
    this.adsDurationDiscount,
    this.adsDurationDiscountPrice,
    this.comment,
    this.active,
    this.isPaid,
    this.isSubscription,
    this.isVerified,
    this.netPrice,
    this.specialServiceTotalPrice,
    this.taxPrice,
    this.totalPrice,
    this.userID,
    this.vehiclePostingID,
    this.qrCodePath,
    this.isCustomerAcknowledged,
    this.createdByRole,
    this.totalViews,
    this.createdOn,
    this.priceExcludingDiscount,
    this.reservePrice,
    this.isMCHandled,
    this.adPostStatus,
    this.adReserveStatus,
    this.isMyAd,
    this.isReservedByMe,
    this.phoneNo,
    this.whatsAppNo,
    this.adOwnerName,
    this.adOwnerEmail,
    this.adOwnerDetails,
    this.warrantyYears,
    this.createdByRoleEnum,
    this.modifiedOn,
    this.adMessages,
  });

  int getRandomValue({required int min, required int max}) {
    Random random = Random();
    int randomNumber = random.nextInt(max - min);
    return randomNumber;
  }

  AdDetailsModel.fromJson(Map<String, dynamic> json, bool isMyAds) {
    id = json['id'];
    startdate = json['startdate'];
    enddate = json['enddate'];
    vehicle = json['vehicle'] != null ? Vehicle.fromJson(json['vehicle']) : null;
    if (json['specialservice'] != null) {
      specialservice = <SpecialServiceModelForAds>[];
      json['specialservice'].forEach((v) {
        specialservice!.add(SpecialServiceModelForAds.fromJson(v));
      });
    }
    statusID = json['statusID'];
    statuslabel = json['statuslabel'];
    adsDurationPrice = json['adsDurationPrice'];
    adsDurationDiscount = json['adsDurationDiscount'];
    adsDurationDiscountPrice = json['adsDurationDiscountPrice'];
    comment = json['comment'];
    active = json['active'];
    isPaid = json['isPaid'];
    isSubscription = json['isSubscription'];
    isVerified = json['isVerified'];
    netPrice = json['netPrice'];
    specialServiceTotalPrice = json['specialServiceTotalPrice'];
    taxPrice = json['taxPrice'];
    totalPrice = json['totalPrice'];
    userID = json['userID'];
    vehiclePostingID = json['vehiclePostingID'];
    qrCodePath = json['qrCodePath'];
    isCustomerAcknowledged = json['isCustomerAcknowledged'];
    createdByRole = json['createdByRole'];
    totalViews = json['totalViews'];
    createdOn = json['createdOn'];
    priceExcludingDiscount = json['priceExcludingDiscount'];
    reservePrice = json['reservePrice'];
    isMCHandled = json['isMCHandled'];
    modifiedOn = json['modifiedOn'];
    phoneNo = json['vehicle'] != null ? (json['vehicle']['mobileNo'] ?? "") : "";
    whatsAppNo = json['vehicle'] != null ? (json['vehicle']['whatsAppNo'] ?? "") : "";
    warrantyYears = json['vehicle'] != null ? (json['vehicle']['warantyYears'] != null ? ((json['vehicle']['warantyYears']).toString()) : "") : "";
    adOwnerName = json['vehicle'] != null ? (json['vehicle']['adOwnerName'] ?? "") : "";
    adOwnerEmail = json['vehicle'] != null ? (json['vehicle']['adOwnerEmail'] ?? "") : "";
    adOwnerDetails = (json['vehicle'] != null && json['vehicle']['aDsUser'] != null) ? (AdOwnerDetails.fromJson(json['vehicle']['aDsUser'])) : null;
    adPostStatus = (json['statusID'] as int).toAdPostEnum();
    adReserveStatus = AdReserveStatus.defaultStatus;
    createdByRoleEnum = (json['createdByRole'] as int).toCreatedByRoleEnum();
    isMyAd = isMyAds;
    isReservedByMe = false;
  }
}

class Vehicle {
  int? id;
  int? cityID;
  String? cityName;
  double? demandAmount;
  bool? isActive;
  bool? isFinanceAvailable;
  int? status;
  String? statusText;
  Category? category;
  Category? color;
  Condition? condition;
  Mileage? mileage;
  Condition? model;
  ModelYear? modelyear;
  Condition? sellertype;
  Condition? transmission;
  AdsDuration? duration;
  List<GenericImageModel>? image;
  List<DamageReport>? damagereport;
  String? vehicleDescription;
  String? vehicleTitle;
  int? vehicleType;
  String? vehicleVIN;
  int? countryID;
  String? currency;

  Vehicle(
      {this.id,
      this.cityID,
      this.cityName,
      this.demandAmount,
      this.isActive,
      this.isFinanceAvailable,
      this.status,
      this.statusText,
      this.category,
      this.color,
      this.condition,
      this.mileage,
      this.model,
      this.modelyear,
      this.sellertype,
      this.transmission,
      this.duration,
      this.image,
      this.damagereport,
      this.vehicleDescription,
      this.vehicleTitle,
      this.vehicleType,
      this.vehicleVIN,
      this.countryID,
      this.currency});

  Vehicle.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    cityID = json['cityID'];
    cityName = json['cityName'];
    demandAmount = json['demandAmount'];
    isActive = json['isActive'];
    isFinanceAvailable = json['isFinanceAvailable'];
    status = json['status'];
    statusText = json['statustext'];
    category = json['category'] != null ? Category.fromJson(json['category']) : null;
    color = json['color'] != null ? Category.fromJson(json['color']) : null;
    condition = json['condition'] != null ? Condition.fromJson(json['condition']) : null;
    mileage = json['mileage'] != null ? Mileage.fromJson(json['mileage']) : null;
    model = json['model'] != null ? Condition.fromJson(json['model']) : null;
    modelyear = json['modelyear'] != null ? ModelYear.fromJson(json['modelyear']) : null;
    sellertype = json['sellertype'] != null ? Condition.fromJson(json['sellertype']) : null;
    transmission = json['transmission'] != null ? Condition.fromJson(json['transmission']) : null;
    duration = json['duration'] != null ? AdsDuration.fromJson(json['duration']) : null;
    if (json['image'] != null) {
      image = <GenericImageModel>[];
      json['image'].forEach((v) {
        image!.add(GenericImageModel.fromJson(v));
      });
    }
    if (json['damagereport'] != null) {
      damagereport = <DamageReport>[];
      json['damagereport'].forEach((v) {
        damagereport!.add(DamageReport.fromJson(v));
      });
    }
    vehicleDescription = json['vehicleDescription'];
    vehicleTitle = json['vehicleTitle'];
    vehicleType = json['vehicleType'];
    vehicleVIN = json['vehicleVIN'];
    countryID = json['countryID'];
    currency = json['currency'];
  }
}

class Category {
  int? id;
  String? label;
  String? labelN;
  bool? isActive;

  Category({this.id, this.label, this.labelN, this.isActive});

  Category.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    label = json['label'];
    labelN = json['labelN'];
    isActive = json['isActive'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['label'] = label;
    data['labelN'] = labelN;
    data['isActive'] = isActive;
    return data;
  }
}

class Condition {
  int? id;
  int? vehicleBrandID;
  String? label;
  String? labelN;

  Condition({this.id, this.label, this.labelN, this.vehicleBrandID});

  Condition.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    vehicleBrandID = json['vehicleBrandID'];
    label = json['label'];
    labelN = json['labelN'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['label'] = label;
    data['labelN'] = labelN;
    return data;
  }
}

class Mileage {
  int? id;
  String? mileageStart;
  String? mileageEnd;
  String? label;

  Mileage({this.id, this.mileageStart, this.mileageEnd, this.label});

  Mileage.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    mileageStart = json['mileageStart'];
    mileageEnd = json['mileageEnd'];
    label = json['label'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['mileageStart'] = mileageStart;
    data['mileageEnd'] = mileageEnd;
    data['label'] = label;
    return data;
  }
}

class ModelYear {
  int? id;
  String? label;

  ModelYear({this.id, this.label});

  ModelYear.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    label = json['label'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['label'] = label;
    return data;
  }
}

class AdsDuration {
  int? id;
  String? label;
  int? days;
  double? price;
  ModelYear? country;

  AdsDuration({this.id, this.label, this.days, this.price, this.country});

  AdsDuration.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    label = json['label'];
    days = json['days'];
    price = json['price'];
    country = json['country'] != null ? ModelYear.fromJson(json['country']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['label'] = label;
    data['days'] = days;
    data['price'] = price;
    if (country != null) {
      data['country'] = country!.toJson();
    }
    return data;
  }
}

class GenericImageModel {
  int? id;
  String? imageName;
  String? imageUrl;
  bool? isActive;

  GenericImageModel({this.id, this.imageName, this.imageUrl, this.isActive});

  GenericImageModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    imageName = json['imageName'];
    imageUrl = json['imageUrl'];
    isActive = json['isActive'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['imageName'] = imageName;
    data['imageUrl'] = imageUrl;
    data['isActive'] = isActive;
    return data;
  }
}

class DamageReport {
  int? id;
  String? comment;
  String? imageUrl;
  bool? isActive;
  int? vehicleDamagePartID;
  String? partName;

  DamageReport({this.id, this.comment, this.imageUrl, this.isActive, this.vehicleDamagePartID, this.partName});

  DamageReport.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    comment = json['comment'];
    imageUrl = json['imageUrl'];
    isActive = json['isActive'];
    vehicleDamagePartID = json['vehicleDamagePartID'];
    partName = json['partName'];
    comment = json['comment'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['comment'] = comment;
    data['imageUrl'] = imageUrl;
    data['isActive'] = isActive;
    data['vehicleDamagePartID'] = vehicleDamagePartID;
    data['partName'] = partName;
    return data;
  }
}

class AdOwnerDetails {
  String? name;
  String? email;
  String? mobileNo;
  String? profilePic;

  AdOwnerDetails({this.name, this.email, this.mobileNo, this.profilePic});

  AdOwnerDetails.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    email = json['email'];
    mobileNo = json['mobileNo'];
    profilePic = json['profilePic'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['name'] = name;
    data['email'] = email;
    data['mobileNo'] = mobileNo;
    data['profilePic'] = profilePic;
    return data;
  }
}
