package client.renderer.chunk;

import java.util.List;
import client.Client;
import client.renderer.RegionRenderCacheBuilder;
import client.renderer.chunk.ChunkBuilder.ListenableFuture;
import common.collect.Lists;
import common.entity.Entity;
import common.log.Log;

public class ChunkRenderWorker implements Runnable
{
    private final ChunkRenderDispatcher chunkRenderDispatcher;
    private final RegionRenderCacheBuilder regionRenderCacheBuilder;

	private boolean running = true;

    public ChunkRenderWorker(ChunkRenderDispatcher p_i46201_1_)
    {
        this(p_i46201_1_, (RegionRenderCacheBuilder)null);
    }

    public ChunkRenderWorker(ChunkRenderDispatcher chunkRenderDispatcherIn, RegionRenderCacheBuilder regionRenderCacheBuilderIn)
    {
        this.chunkRenderDispatcher = chunkRenderDispatcherIn;
        this.regionRenderCacheBuilder = regionRenderCacheBuilderIn;
    }

    public void run()
    {
        while (this.running)
        {
            try
            {
                this.processTask(this.chunkRenderDispatcher.getNextChunkUpdate());
            }
            catch (InterruptedException var3)
            {
//            	Log.debug("Stoppe wegen Unterbrechung");
                return;
            }
        }
    }
    
    public void end() {
    	this.running = false;
    }

    protected void processTask(final ChunkCompileTaskGenerator generator) throws InterruptedException
    {
        generator.getLock().lock();

        try
        {
            if (generator.getStatus() != ChunkCompileTaskGenerator.Status.PENDING)
            {
                if (!generator.isFinished())
                {
                	Log.RENDER.warn("Chunk-Rendering-Aufgabe war " + generator.getStatus() + " wenn PENDING erwartet war; ignoriere Aufgabe");
                }

                return;
            }

            generator.setStatus(ChunkCompileTaskGenerator.Status.COMPILING);
        }
        finally
        {
            generator.getLock().unlock();
        }

        Entity lvt_2_1_ = Client.CLIENT.getRenderViewEntity();

        if (lvt_2_1_ == null)
        {
            generator.finish();
        }
        else
        {
            generator.setRegionRenderCacheBuilder(this.getRegionRenderCacheBuilder());
            float f = (float)lvt_2_1_.posX;
            float f1 = (float)lvt_2_1_.posY + lvt_2_1_.getEyeHeight();
            float f2 = (float)lvt_2_1_.posZ;

            generator.getRenderChunk().rebuildChunk(f, f1, f2, generator);

            generator.getLock().lock();

            try
            {
                if (generator.getStatus() != ChunkCompileTaskGenerator.Status.COMPILING)
                {
                    if (!generator.isFinished())
                    {
                    	Log.RENDER.warn("Chunk-Rendering-Aufgabe war " + generator.getStatus() + " wenn COMPILING erwartet war; breche Aufgabe ab");
                    }

                    this.freeRenderBuilder(generator);
                    return;
                }

                generator.setStatus(ChunkCompileTaskGenerator.Status.UPLOADING);
            }
            finally
            {
                generator.getLock().unlock();
            }

            final CompiledChunk compiled = generator.getCompiledChunk();
            List<ListenableFuture<Object>> list = Lists.newArrayList();

            for (BlockLayer enumworldblocklayer : BlockLayer.values())
            {
                if (compiled.isLayerStarted(enumworldblocklayer))
                {
                    list.add(this.chunkRenderDispatcher.uploadChunk(enumworldblocklayer, generator.getRegionRenderCacheBuilder().getWorldRendererByLayer(enumworldblocklayer), generator.getRenderChunk(), compiled));
                }
            }

            ChunkBuilder.addCallback(list, generator, compiled, this);
        }
    }

    private RegionRenderCacheBuilder getRegionRenderCacheBuilder() throws InterruptedException
    {
        return this.regionRenderCacheBuilder != null ? this.regionRenderCacheBuilder : this.chunkRenderDispatcher.allocateRenderBuilder();
    }

    public void freeRenderBuilder(ChunkCompileTaskGenerator taskGenerator)
    {
        if (this.regionRenderCacheBuilder == null)
        {
            this.chunkRenderDispatcher.freeRenderBuilder(taskGenerator.getRegionRenderCacheBuilder());
        }
    }

    public void setRenderBuilderChunk(ChunkCompileTaskGenerator generator, CompiledChunk compiled)
    {
        ChunkRenderWorker.this.freeRenderBuilder(generator);
        generator.getLock().lock();
        label21:
        {
            try
            {
                if (generator.getStatus() == ChunkCompileTaskGenerator.Status.UPLOADING)
                {
                    generator.setStatus(ChunkCompileTaskGenerator.Status.DONE);
                    break label21;
                }

                if (!generator.isFinished())
                {
                	Log.RENDER.warn("Chunk-Rendering-Aufgabe war " + generator.getStatus() + " wenn UPLOADING erwartet war; breche Aufgabe ab");
                }
            }
            finally
            {
                generator.getLock().unlock();
            }

            return;
        }
        generator.getRenderChunk().setCompiledChunk(compiled);
    }
}
