package de.avatic.lcc.model.db.packaging;

import de.avatic.lcc.model.db.utils.DimensionUnit;
import de.avatic.lcc.model.db.utils.WeightUnit;

public class PackagingDimension {


    private Integer id;

    private PackagingType type;

    private Integer length;

    private Integer width;

    private Integer height;

    private DimensionUnit dimensionUnit;

    private Integer weight;

    private WeightUnit weightUnit;

    private Integer contentUnitCount;

    private Boolean isDeprecated;

    public static PackagingDimension getEmpty(PackagingType type) {
        var dimension = new PackagingDimension();

        dimension.setType(type);
        dimension.setLength(null);
        dimension.setWidth(null);
        dimension.setHeight(null);
        dimension.setDimensionUnit(DimensionUnit.MM);

        dimension.setWeight(null);
        dimension.setWeightUnit(WeightUnit.KG);

        dimension.setContentUnitCount(null);

        return dimension;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public PackagingType getType() {
        return type;
    }

    public void setType(PackagingType type) {
        this.type = type;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public DimensionUnit getDimensionUnit() {
        return dimensionUnit;
    }

    public void setDimensionUnit(DimensionUnit dimensionUnit) {
        this.dimensionUnit = dimensionUnit;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public WeightUnit getWeightUnit() {
        return weightUnit;
    }

    public void setWeightUnit(WeightUnit weightUnit) {
        this.weightUnit = weightUnit;
    }

    public Integer getContentUnitCount() {
        return contentUnitCount;
    }

    public void setContentUnitCount(Integer contentUnitCount) {
        this.contentUnitCount = contentUnitCount;
    }

    public Boolean getDeprecated() {
        return isDeprecated;
    }

    public void setDeprecated(Boolean deprecated) {
        isDeprecated = deprecated;
    }

    public PackagingDimension withTolerance(double tolerance) {
        PackagingDimension dimensions = new PackagingDimension();

        dimensions.setType(type);
        dimensions.setLength(Double.valueOf(length.doubleValue() * (1.0+tolerance)).intValue());
        dimensions.setWidth(Double.valueOf(width.doubleValue() * (1.0+tolerance)).intValue());
        dimensions.setHeight(Double.valueOf(height.doubleValue() * (1.0+tolerance)).intValue());
        dimensions.setDimensionUnit(dimensionUnit);
        dimensions.setWeight(weight);
        dimensions.setWeightUnit(weightUnit);
        dimensions.setContentUnitCount(contentUnitCount);
        dimensions.setDeprecated(isDeprecated);

        return dimensions;
    }

    public double getVolume(DimensionUnit unit) {
        var lengthInX = unit.convertFromMM(length);
        var widthInX = unit.convertFromMM(width);
        var heightInX = unit.convertFromMM(height);

        return  lengthInX.doubleValue() * widthInX.doubleValue() * heightInX.doubleValue();
    }

    public LoadCarrierType getLoadCarrierType() {
        return getVolume(DimensionUnit.MM) > 80000000 ? LoadCarrierType.LLC : LoadCarrierType.SLC;
    }

    public double getRoundedHeight(DimensionUnit unit) {
        return Math.ceil(unit.convertFromMM(height).doubleValue());
    }

    public double getFloorArea(DimensionUnit unit) {
        var lengthInX = unit.convertFromMM(length);
        var widthInX = unit.convertFromMM(width);
        return  lengthInX.doubleValue() * widthInX.doubleValue();
    }

    public double getWeight(WeightUnit weightUnit) {
       return weightUnit.convertFromG(this.weight);
    }
}
