package de.avatic.lcc.service.transformer.generic;

import de.avatic.lcc.dto.generic.DimensionDTO;
import de.avatic.lcc.model.excel.ExcelPackaging;
import de.avatic.lcc.model.db.packaging.PackagingDimension;
import de.avatic.lcc.model.db.packaging.PackagingType;
import de.avatic.lcc.model.db.premises.Premise;
import de.avatic.lcc.model.db.utils.DimensionUnit;
import de.avatic.lcc.model.db.utils.WeightUnit;
import de.avatic.lcc.util.exception.badrequest.InvalidArgumentException;
import org.springframework.stereotype.Service;

@Service
public class DimensionTransformer {

    public DimensionDTO toDimensionDTO(PackagingDimension entity) {
        DimensionDTO dto = new DimensionDTO();

        dto.setId(entity.getId());
        dto.setType(entity.getType());
        dto.setLength(entity.getDimensionUnit().convertFromMM(entity.getLength()).doubleValue());
        dto.setWidth(entity.getDimensionUnit().convertFromMM(entity.getWidth()).doubleValue());
        dto.setHeight(entity.getDimensionUnit().convertFromMM(entity.getHeight()).doubleValue());
        dto.setDimensionUnit(entity.getDimensionUnit());
        dto.setWeight(entity.getWeightUnit().convertFromG(entity.getWeight()).doubleValue());
        dto.setWeightUnit(entity.getWeightUnit());
        dto.setContentUnitCount(entity.getContentUnitCount());
        dto.setDeprecated(entity.getDeprecated());

        return dto;
    }

    public PackagingDimension toDimensionEntity(DimensionDTO dto) {

        if (dto.getDimensionUnit() == null) {
            throw new InvalidArgumentException("dimension_unit", "null");
        }

        if (dto.getWeightUnit() == null) {
            throw new InvalidArgumentException("weight_unit", "null");
        }

        var entity = new PackagingDimension();
        entity.setId(dto.getId());
        entity.setType(dto.getType());

        entity.setLength(doDimensionConversion(dto.getLength(), dto.getDimensionUnit()));
        entity.setWidth(doDimensionConversion(dto.getWidth(), dto.getDimensionUnit()));
        entity.setHeight(doDimensionConversion(dto.getHeight(), dto.getDimensionUnit()));
        entity.setDimensionUnit(dto.getDimensionUnit());
        entity.setWeight(doWeightConversion(dto.getWeight(), dto.getWeightUnit()));
        entity.setWeightUnit(dto.getWeightUnit());
        entity.setContentUnitCount(dto.getContentUnitCount());
        entity.setDeprecated(dto.getDeprecated());

        return entity;
    }

    private Integer doWeightConversion(Double value, WeightUnit unit) {
        if (value == null) return null;

        if (value <= 0) {
            return null;
        }

        return unit.convertToG(value);
    }

    private Integer doDimensionConversion(Double value, DimensionUnit unit) {
        if (value == null) return null;

        if (value <= 0) {
            return null;
        }

        return unit.convertToMM(value);
    }

    public PackagingDimension toDimensionEntity(Premise entity) {
        var packaging = new PackagingDimension();

        packaging.setId(null);
        packaging.setType(PackagingType.HU);
        packaging.setLength(entity.getIndividualHuLength());
        packaging.setWidth(entity.getIndividualHuWidth());
        packaging.setHeight(entity.getIndividualHuHeight());
        packaging.setDimensionUnit(entity.getHuDisplayedDimensionUnit());
        packaging.setWeight(entity.getIndividualHuWeight());
        packaging.setWeightUnit(entity.getHuDisplayedWeightUnit());
        packaging.setContentUnitCount(entity.getHuUnitCount());
        packaging.setDeprecated(null);

        return packaging;
    }

    public DimensionDTO toDimensionDTO(Premise entity) {
        DimensionDTO dto = new DimensionDTO();

        dto.setId(null);
        dto.setType(PackagingType.HU);
        dto.setLength(entity.getIndividualHuLength() == null ? null : entity.getHuDisplayedDimensionUnit().convertFromMM(entity.getIndividualHuLength()));
        dto.setWidth(entity.getIndividualHuWidth() == null ? null : entity.getHuDisplayedDimensionUnit().convertFromMM(entity.getIndividualHuWidth()));
        dto.setHeight(entity.getIndividualHuHeight() == null ? null : entity.getHuDisplayedDimensionUnit().convertFromMM(entity.getIndividualHuHeight()));
        dto.setDimensionUnit(entity.getHuDisplayedDimensionUnit());
        dto.setWeight(entity.getIndividualHuWeight() == null ? null : entity.getHuDisplayedWeightUnit().convertFromG(entity.getIndividualHuWeight()));
        dto.setWeightUnit(entity.getHuDisplayedWeightUnit());
        dto.setContentUnitCount(entity.getHuUnitCount());
        dto.setDeprecated(null);

        return dto;
    }

    public PackagingDimension toDimensionEntity(ExcelPackaging packaging, PackagingType type) {
        PackagingDimension dimension = new PackagingDimension();

        if (type == PackagingType.HU) {
            WeightUnit weightUnit = packaging.getHuWeightUnit();
            DimensionUnit unit = packaging.getHuDimensionUnit();

            dimension.setLength(unit.convertToMM(packaging.getHuLength()));
            dimension.setHeight(unit.convertToMM(packaging.getHuHeight()));
            dimension.setWidth(unit.convertToMM(packaging.getHuWidth()));
            dimension.setDimensionUnit(unit);

            dimension.setWeight(weightUnit.convertToG(packaging.getHuWeight()));
            dimension.setWeightUnit(weightUnit);

            dimension.setContentUnitCount(packaging.getHuUnitCount());
            dimension.setType(PackagingType.HU);

            dimension.setDeprecated(false);

        } else if (type == PackagingType.SHU) {
            WeightUnit weightUnit = packaging.getShuWeightUnit();
            DimensionUnit unit = packaging.getShuDimensionUnit();

            dimension.setLength(unit.convertToMM(packaging.getShuLength()));
            dimension.setHeight(unit.convertToMM(packaging.getShuHeight()));
            dimension.setWidth(unit.convertToMM(packaging.getShuWidth()));
            dimension.setDimensionUnit(unit);

            dimension.setWeight(weightUnit.convertToG(packaging.getShuWeight()));
            dimension.setWeightUnit(weightUnit);

            dimension.setContentUnitCount(packaging.getShuUnitCount());
            dimension.setType(PackagingType.SHU);

            dimension.setDeprecated(false);
        }


        return dimension;
    }
}
