package com.inspect.infrared.domain;

import com.inspect.base.core.utils.StringUtils;
import com.inspect.infrared.InfraredImage;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class InfraredCoordinate {
    private String coordinate;
    private int[] coordinates;

    public InfraredCoordinate(String coordinate) {
        this.coordinate = coordinate;
        if (!StringUtils.isEmpty(coordinate)) {
            String[] split = coordinate.split(StringUtils.COMMA);
            this.coordinates = new int[split.length];

            for (int j = 0; j < this.coordinates.length; ++j) {
                this.coordinates[j] = Integer.parseInt(split[j]);
            }
        }

    }

    public void convert(InfraredImage infraredImage) {
        if (StringUtils.isEmpty(this.coordinate)) {
            this.coordinates = new int[]{0, 0, infraredImage.getWidth() - 1, 0, infraredImage.getWidth() - 1, infraredImage.getHeight() - 1, 0, infraredImage.getHeight() - 1, infraredImage.getWidth(), infraredImage.getHeight()};
        } else {
            for (int i = 0; i < this.coordinates.length - 2; i += 2) {
                this.coordinates[i] = this.coordinates[i] * infraredImage.getWidth() / this.coordinates[this.coordinates.length - 2];
                this.coordinates[i + 1] = this.coordinates[i + 1] * infraredImage.getHeight() / this.coordinates[this.coordinates.length - 1];
            }

        }
    }

}
