/*
 * Copyright 2016 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.rendering.dag.stateChanges;

import org.terasology.assets.ResourceUrn;
import org.terasology.rendering.opengl.BaseFBOsManager;
import org.terasology.rendering.opengl.DefaultDynamicFBOs;
import org.terasology.rendering.opengl.FBOManagerSubscriber;
import java.util.Objects;
import org.terasology.rendering.dag.RenderPipelineTask;
import org.terasology.rendering.dag.StateChange;
import org.terasology.rendering.dag.tasks.SetViewportToSizeOfTask;
import org.terasology.rendering.opengl.FBO;
import static org.terasology.rendering.opengl.DefaultDynamicFBOs.READ_ONLY_GBUFFER;
/**
 * TODO: Add javadocs
 */
public final class SetViewportToSizeOf implements FBOManagerSubscriber, StateChange {
    private static SetViewportToSizeOf defaultInstance = new SetViewportToSizeOf(READ_ONLY_GBUFFER);

    private BaseFBOsManager frameBuffersManager;
    private SetViewportToSizeOfTask task;
    private ResourceUrn fboName;
    private DefaultDynamicFBOs defaultDynamicFBO;

    public SetViewportToSizeOf(ResourceUrn fboName, BaseFBOsManager frameBuffersManager) {
        this.frameBuffersManager = frameBuffersManager;
        this.fboName = fboName;
    }

    public SetViewportToSizeOf(DefaultDynamicFBOs defaultDynamicFBO) {
        this.defaultDynamicFBO = defaultDynamicFBO;
        this.frameBuffersManager = defaultDynamicFBO.getFrameBufferManager();
        this.fboName = defaultDynamicFBO.getName();
    }

    @Override
    public StateChange getDefaultInstance() {
        return defaultInstance;
    }

    @Override
    public RenderPipelineTask generateTask() {
        if (task == null) {
            task = new SetViewportToSizeOfTask(fboName);
            frameBuffersManager.subscribe(this);
            update();
        }

        return task;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(fboName);
    }

    @Override
    public boolean equals(Object obj) {
        return (obj instanceof SetViewportToSizeOf) && this.fboName.equals(((SetViewportToSizeOf) obj).getFboName());
    }

    @Override
    public boolean isTheDefaultInstance() {
        return this == defaultInstance;
    }

    @Override
    public void update() {
        if (defaultDynamicFBO == null) {
            FBO fbo = frameBuffersManager.get(fboName);
            task.setDimensions(fbo.width(), fbo.height());
        } else {
            task.setDimensions(defaultDynamicFBO.width(), defaultDynamicFBO.height());
        }
    }

    @Override
    public String toString() { // TODO: used for logging purposes at the moment, investigate different methods
        return String.format("%30s: %s", this.getClass().getSimpleName(), fboName);
    }

    public ResourceUrn getFboName() {
        return fboName;
    }
}

@@ -856,24 +856,25 @@
 Subscriber;%0A
+%0A
 import java.
@@ -887,16 +887,17 @@
 bjects;%0A
+%0A
 import o
@@ -1104,16 +1104,17 @@
 gl.FBO;%0A
+%0A
 import s
@@ -1189,16 +1189,17 @@
 BUFFER;%0A
+%0A
 /**%0A * T
@@ -2392,85 +2392,375 @@
     
-@Override%0A    public int hashCode() %7B%0A        return Objects.hashCode(fboName
+// TODO: change equals and hashCode to use dimensions instead.%0A    @Override%0A    public int hashCode() %7B%0A        // Generates a unique 32 bit signed integer from two integers.%0A        // This will return unique values for the following reasonable ranges:%0A        // width %3C (1 %3C%3C 16) (65536) and height %3C (1 %3C%3C 15) (32768)%0A        return getWidth() %3C%3C 15 %7C getHeight(
 );%0A 
@@ -2827,23 +2827,21 @@
         
-return 
+if (!
 (obj ins
@@ -2872,720 +2872,986 @@
 eOf)
- && this.fboName.equals(((SetViewportToSizeOf) obj).getFboName());%0A    %7D%0A%0A    @Override%0A    public boolean isTheDefaultInstance() %7B%0A        return this == defaultInstance;%0A    %7D%0A%0A    @Override%0A    public void update() %7B%0A        if (defaultDynamicFBO == null) %7B%0A            FBO fbo = frameBuffersManager.get(fboName);%0A            task.setDimensions(fbo.width(), fbo.height());%0A        %7D else %7B%0A            task.setDimensions(defaultDynamicFBO.width(), defaultDynamicFBO.height());%0A        %7D%0A    %7D%0A%0A    @Override%0A    public String toString() %7B // TODO: used for logging purposes at the moment, investigate different methods%0A        return String.format(%22%2530s: %25s%22, this.getClass().getSimpleName(), fboName);%0A    %7D%0A
+)%0A            return false;%0A%0A        SetViewportToSizeOf other = (SetViewportToSizeOf) obj;%0A%0A        return getWidth() == other.getWidth() && getHeight() == other.getHeight();%0A    %7D%0A%0A    @Override%0A    public boolean isTheDefaultInstance() %7B%0A        return this == defaultInstance;%0A    %7D%0A%0A    @Override%0A    public void update() %7B%0A        task.setDimensions(getWidth(), getHeight());%0A    %7D%0A%0A    @Override%0A    public String toString() %7B // TODO: used for logging purposes at the moment, investigate different methods%0A        return String.format(%22%2530s: %25s%22, this.getClass().getSimpleName(), fboName);%0A    %7D%0A%0A    public int getWidth() %7B%0A        FBO fbo = getFbo();%0A        return fbo.width();%0A    %7D%0A%0A    public int getHeight() %7B%0A        FBO fbo = getFbo();%0A        return fbo.height();%0A    %7D%0A%0A    private FBO getFbo() %7B%0A        if (defaultDynamicFBO == null)%0A            return frameBuffersManager.get(fboName);%0A%0A        return defaultDynamicFBO.getFbo();%0A    %7D%0A%0A    // TODO: Remove
 %0A   

Change equals and hashCode in SetViewportToSizeOf to use dimensions instead.