import 'package:json_annotation/json_annotation.dart';
import 'package:mypoint_flutter_app/extensions/date_format.dart';
import 'package:mypoint_flutter_app/extensions/datetime_extensions.dart';
import 'package:mypoint_flutter_app/extensions/string_extension.dart';
import 'package:mypoint_flutter_app/screen/voucher/models/product_brand_model.dart';
import 'package:mypoint_flutter_app/screen/voucher/models/product_content_model.dart';
import 'package:mypoint_flutter_app/screen/voucher/models/product_customer_info_model.dart';
import 'package:mypoint_flutter_app/screen/voucher/models/product_item_model.dart';
import 'package:mypoint_flutter_app/screen/voucher/models/product_media_item.dart';
import 'package:mypoint_flutter_app/screen/voucher/models/product_price_model.dart';
import 'package:mypoint_flutter_app/screen/voucher/models/product_properties_model.dart';
import '../../flash_sale/preview_flash_sale_model.dart';
import 'media_type.dart';
import 'my_product_status_type.dart';

part 'product_model.g.dart';
@JsonSerializable()
class ProductModel {
  final int? id;
  @JsonKey(name: 'like_id')
  int? likeId;
  @JsonKey(name: 'quantity_available')
  final int? quantityAvailable;
  final ProductContentModel? content;
  final ProductPriceModel? price;
  final ProductBrandModel? brand;
  @JsonKey(name: 'voucher_properties')
  final ProductPropertiesModel? properties;
  final List<ProductMediaItem>? media;
  @JsonKey(name: 'preview_campaign_flash_sale')
  final PreviewFlashSale? previewFlashSale;
  @JsonKey(name: 'customer_product_info')
  final ProductCustomerInfoModel? customerInfoModel;
  @JsonKey(name: 'product_item')
  final ProductItemModel? itemModel;
  @JsonKey(name: 'expire_time')
  final String? expireTime;

  ProductModel({
    this.id,
    this.likeId,
    this.quantityAvailable,
    this.content,
    this.price,
    this.brand,
    this.properties,
    this.media,
    this.previewFlashSale,
    this.customerInfoModel,
    this.itemModel,
    this.expireTime,
  });

  String? get name {
    return content?.name;
  }

  String get expire {
    final ex = (isMyProduct ? itemModel?.expireTime : expireTime) ?? "";
    return ex.toDate()?.toFormattedString() ?? "";
  }

  bool get expired {
    if (customerInfoModel?.status == MyProductStatusType.expired) return true;
    final expireDate = expire.toDateFormat('dd/MM/yyyy');
    if (expireDate == null) return false;
    return expireDate!.isBefore(DateTime.now());
  }

  int? get amountToBePaid {
    if (previewFlashSale?.isFlashSalePrice == true) {
      return previewFlashSale?.price;
    }
    return price?.value;
  }

  bool get isMyProduct {
    return customerInfoModel != null;
  }

  bool get inStock {
    return (quantityAvailable ?? 1) != 0;
  }

  bool get liked {
    return (likeId ?? 0) != 0;
  }

  ProductMediaItem? get banner {
    if (media == null) return null;
    return media!.firstWhere((item) => item.type == MediaType.banner16_9, orElse: () => media!.first);
  }

  factory ProductModel.fromJson(Map<String, dynamic> json) => _$ProductModelFromJson(json);

  Map<String, dynamic> toJson() => _$ProductModelToJson(this);
}