package com.inspect.infrared.selectArea;

import com.inspect.base.core.exception.ServiceException;
import com.inspect.infrared.domain.InfraredCoordinate;
import com.inspect.infrared.selectArea.immp.LineSelectArea;
import com.inspect.infrared.selectArea.immp.PointSelectArea;
import com.inspect.infrared.selectArea.immp.RectangleSelectArea;

import java.lang.reflect.Field;

public class SelectAreaFactory {
    public static SelectArea create(InfraredCoordinate infraredCoordinate) {
        int[] coordinates = infraredCoordinate.getCoordinates();
        Object selectArea;
        switch (coordinates.length) {
            case 4:
                selectArea = new PointSelectArea(coordinates);
                break;
            case 6:
                selectArea = new LineSelectArea(coordinates);
                break;
            case 10:
                selectArea = new RectangleSelectArea(coordinates);
                break;
            default:
                throw new ServiceException("无此选区类型！");
        }

        initCoordinates((SelectArea) selectArea);
        return (SelectArea) selectArea;
    }

    private static void initCoordinates(SelectArea selectArea) {
        Field[] declaredFields = selectArea.getClass().getDeclaredFields();
        int[] coordinates = selectArea.getCoordinates();

        for (int i = 0; i < declaredFields.length; ++i) {
            Field field = declaredFields[i];
            if (field.getType().equals(Integer.TYPE)) {
                field.setAccessible(true);

                try {
                    field.set(selectArea, coordinates[i]);
                } catch (IllegalAccessException var6) {
                    ;
                }
            }
        }

        selectArea.setWidth(coordinates[coordinates.length - 2]);
        selectArea.setHeight(coordinates[coordinates.length - 1]);
    }
}
