package games.rednblack.editor.view.stage.tools.transformStrategy;

import com.badlogic.gdx.math.Vector2;
import games.rednblack.editor.renderer.components.DimensionsComponent;
import games.rednblack.editor.renderer.components.NinePatchComponent;
import games.rednblack.editor.renderer.components.TransformComponent;
import games.rednblack.editor.utils.runtime.SandboxComponentRetriever;
import games.rednblack.editor.view.ui.followers.NormalSelectionFollower;
import games.rednblack.h2d.common.command.TransformCommandBuilder;

/**
 * Created by Sasun Poghosyan on 4/13/2016.
 */
public class NinePatchStrategy extends AbstractTransformStrategy {

    @Override
    public void calculate(float mouseDx, float mouseDy, int anchor, int entity, TransformCommandBuilder transformCommandBuilder, Vector2 mousePointStage, float lastTransformAngle, float lastEntityAngle) {
        TransformComponent transformComponent = SandboxComponentRetriever.get(entity, TransformComponent.class);
        DimensionsComponent dimensionsComponent = SandboxComponentRetriever.get(entity, DimensionsComponent.class);

        float newX = transformComponent.x;
        float newY = transformComponent.y;
        float newWidth = dimensionsComponent.width;
        float newHeight = dimensionsComponent.height;

        NinePatchComponent ninePatchComponent = SandboxComponentRetriever.get(entity, NinePatchComponent.class);
        float minWidth = ninePatchComponent.ninePatch.getTotalWidth();
        float minHeight = ninePatchComponent.ninePatch.getTotalHeight();

        switch (anchor) {
            case NormalSelectionFollower.L:
                newWidth = dimensionsComponent.width + (transformComponent.x - mousePointStage.x);
                if (newWidth < minWidth) {
                    newX = mousePointStage.x - (minWidth - newWidth);
                    newWidth = minWidth;
                } else {
                    newX = mousePointStage.x;
                }
                break;
            case NormalSelectionFollower.R:
                newWidth = dimensionsComponent.width + (mousePointStage.x - (transformComponent.x + dimensionsComponent.width));
                if (newWidth < minWidth) {
                    newWidth = minWidth;
                }
                break;
            case NormalSelectionFollower.B:
                newHeight = dimensionsComponent.height + (transformComponent.y - mousePointStage.y);
                if (newHeight < minHeight) {
                    newY = mousePointStage.y - (minHeight - newHeight);
                    newHeight = minHeight;
                } else {
                    newY = mousePointStage.y;
                }
                break;
            case NormalSelectionFollower.T:
                newHeight = dimensionsComponent.height + (mousePointStage.y - (transformComponent.y + dimensionsComponent.height));
                if (newHeight < minHeight) {
                    newHeight = minHeight;
                }
                break;
            case NormalSelectionFollower.LT:
                newWidth = dimensionsComponent.width + (transformComponent.x - mousePointStage.x);
                newHeight = dimensionsComponent.height + (mousePointStage.y - (transformComponent.y + dimensionsComponent.height));
                if (newWidth < minWidth) {
                    newX = mousePointStage.x - (minWidth - newWidth);
                    newWidth = minWidth;
                } else {
                    newX = mousePointStage.x;
                }
                if (newHeight < minHeight) {
                    newHeight = minHeight;
                }
                break;
            case NormalSelectionFollower.RT:
                newWidth = dimensionsComponent.width + (mousePointStage.x - (transformComponent.x + dimensionsComponent.width));
                newHeight = dimensionsComponent.height + (mousePointStage.y - (transformComponent.y + dimensionsComponent.height));
                if (newHeight < minHeight) {
                    newHeight = minHeight;
                }
                if (newWidth < minWidth) {
                    newWidth = minWidth;
                }
                break;
            case NormalSelectionFollower.RB:
                newWidth = dimensionsComponent.width + (mousePointStage.x - (transformComponent.x + dimensionsComponent.width));
                newHeight = dimensionsComponent.height + (transformComponent.y - mousePointStage.y);
                if (newWidth < minWidth) {
                    newWidth = minWidth;
                }
                if (newHeight < minHeight) {
                    newY = mousePointStage.y - (minHeight - newHeight);
                    newHeight = minHeight;
                } else {
                    newY = mousePointStage.y;
                }
                break;
            case NormalSelectionFollower.LB:
                newWidth = dimensionsComponent.width + (transformComponent.x - mousePointStage.x);
                newHeight = dimensionsComponent.height + (transformComponent.y - mousePointStage.y);
                if (newWidth < minWidth) {
                    newX = mousePointStage.x - (minWidth - newWidth);
                    newWidth = minWidth;
                } else {
                    newX = mousePointStage.x;
                }
                if (newHeight < minHeight) {
                    newY = mousePointStage.y - (minHeight - newHeight);
                    newHeight = minHeight;
                } else {
                    newY = mousePointStage.y;
                }
                break;
        }

        transformCommandBuilder.setPos(newX, newY);
        transformCommandBuilder.setSize(newWidth, newHeight);

        transformComponent.x = newX;
        transformComponent.y = newY;
        dimensionsComponent.width = newWidth;
        dimensionsComponent.height = newHeight;

        // Origin
        origin(mouseDx, mouseDy, anchor, transformComponent, transformCommandBuilder);

        // Rotating
        rotating(anchor, transformCommandBuilder, mousePointStage, lastTransformAngle, lastEntityAngle, transformComponent);
    }

}
