package com.sourcefellows.oneintention;

import java.math.BigDecimal;

public class Product {

    private String id;
    private BigDecimal taxValue;
    private BigDecimal netPrice;

    public Product(BigDecimal taxValue, BigDecimal netPrice) {
        this.taxValue = taxValue;
        this.netPrice = netPrice;
    }

    public static Product fromString(String data) {

        int beginIdx = data.indexOf("<tax-value>");
        int endIdx = data.indexOf("</tax-value>");
        String taxValueString = data.substring(beginIdx + 11, endIdx);
        BigDecimal taxValue = new BigDecimal(taxValueString);
        beginIdx = data.indexOf("<price-value>");
        endIdx = data.indexOf("</price-value>");
        String productPriceString = data.substring(beginIdx + 13, endIdx);
        BigDecimal productPrice = new BigDecimal(productPriceString);

        Product product = new Product(taxValue, productPrice);
        product.setTaxValue(new BigDecimal(-1));

        boolean valid = product.setTaxValue(new BigDecimal(-1));
        if (!valid) {

        }

        return product;
    }

    public BigDecimal getNetPrice() {
        return netPrice;
    }

    public BigDecimal getTaxValue() {
        return taxValue;
    }

    public boolean setTaxValue(BigDecimal taxValue) {
        if (taxValue.signum() == -1) {
            return false;
        }
        this.taxValue = taxValue;
        return true;
    }
}
