import { ProductProduct } from "../../../odoo/models/product.product";

export class CartItem {

    product: ProductProduct;
    price: number;
    quantity: number;
    subtotal: number;

    constructor(
        product?: ProductProduct,
        price?: number,
        quantity?: number,
        subtotal?: number
    ) {
        this.product = product;
        this.price = price ? price : 0;
        this.quantity = quantity ? quantity : 0;
        this.subtotal = subtotal ? subtotal : 0;
    }

    /**
     * 
     */
    getProduct(): ProductProduct {
        return this.product;
    }

    /**
     * 
     */
    getProductName(): string {
        return this.getProduct() ? this.getProduct().name : "n/a";
    }

    /**
     * 
     */
    getImage(): string {
        return this.getProduct().image_medium;
    }

    /**
     * 
     */
    getPrice(): number {
        return this.price;
    }

    /**
     * 
     */
    getQuantity(): number {
        return this.quantity;
    }

    /**
     * 
     */
    getSubtotal(): number {
        return this.subtotal;
    }

    /**
     * 
     * @param product 
     */
    setProduct(product: ProductProduct): void {
        this.product = product;
    }

    /**
     * 
     * @param price 
     */
    setPrice(price: number): void {
        this.price = price;
    }

    /**
     * 
     * @param quantity 
     */
    setQuantity(quantity: number): void {
        this.quantity = quantity;
    }

    /**
     * 
     * @param subtotal 
     */
    setSubtotal(subtotal: number): void {
        this.subtotal = subtotal;
    }

    /**
     * 
     */
    hasImage(): boolean {
        return !!this.getProduct().image_medium;
    }

    /**
     * 
     * @param quantity 
     */
    updateQuantity(quantity: number): void {
        this.setQuantity(this.getQuantity() + quantity);
        this.computeSubtotal();
    }

    /**
     * 
     * @param price 
     */
    updatePrice(price: number): void {
        this.setPrice(price);
        this.computeSubtotal();
    }

    /**
     * 
     */
    computeSubtotal(): void {
        this.setSubtotal(this.getPrice() * this.getQuantity());
    }
}