import 'package:hl_lieferservice/dto/article.dart';

class Article {
  Article({
    required this.name,
    required this.articleNumber,
    required this.amount,
    required this.internalId,
    required this.price,
    required this.tax,
    required this.scannable,
    required this.scannedAmount,
    required this.scannedRemovedAmount,
    this.scannedDate,
    this.removeNoteId
  });

  final String name;
  final String articleNumber;
  final int internalId;

  int amount;
  double price;
  double tax;
  bool scannable;
  DateTime? scannedDate;
  String? removeNoteId;
  int scannedAmount;
  int scannedRemovedAmount;

  double getGrossPrice() {
    return price * amount * ((100 + tax) / 100);
  }

  double getGrossPriceScanned() {
    return price * scannedAmount * ((100 + tax) / 100);
  }

  bool unscanned() {
    return scannedAmount == 0;
  }

  int getScannedAmount() {
    return scannedAmount;
  }

  factory Article.fromDTO(ArticleDTO dto) {
    return Article(
      name: dto.name,
      scannedAmount: int.parse(
        dto.scannedAmount != "" ? dto.scannedAmount : "0",
      ),
      scannedRemovedAmount: int.tryParse(dto.scannedRemovedAmount) ?? 0,
      removeNoteId: dto.removeNoteId,
      internalId: int.parse(dto.internalId),
      articleNumber: dto.articleNr,
      amount: int.parse(dto.quantity == "" ? "0" : dto.quantity),
      price: double.parse(dto.price == "" ? "0.0" : dto.price),
      scannable: dto.scannable,
      tax: double.parse(dto.taxRate == "" ? "19" : dto.taxRate),
    );
  }
}
