/*
 * Copyright 2014 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.nui.layers.mainMenu;

import java.nio.ByteBuffer;

import org.terasology.asset.AssetType;
import org.terasology.asset.AssetUri;
import org.terasology.asset.Assets;
import org.terasology.config.Config;
import org.terasology.registry.In;
import org.terasology.rendering.assets.texture.Texture;
import org.terasology.rendering.assets.texture.TextureData;
import org.terasology.rendering.nui.Color;
import org.terasology.rendering.nui.CoreScreenLayer;
import org.terasology.rendering.nui.UIWidget;
import org.terasology.rendering.nui.WidgetUtil;
import org.terasology.rendering.nui.databinding.BindHelper;
import org.terasology.rendering.nui.databinding.DefaultBinding;
import org.terasology.rendering.nui.widgets.ActivateEventListener;
import org.terasology.rendering.nui.widgets.UIButton;
import org.terasology.rendering.nui.widgets.UIImage;
import org.terasology.rendering.nui.widgets.UISlider;
import org.terasology.rendering.nui.widgets.UIText;

/**
 * @author Martin Steiger
 */
public class PlayerSettingsScreen extends CoreScreenLayer {

    @In
    private Config config;

    private UISlider sliderBlue;
    private UISlider sliderGreen;
    private UISlider sliderRed;
    private UIImage img;
    
    @Override
    public void initialise() {
        UIText nametext = find("playername", UIText.class);
        if (nametext != null) {
            nametext.bindText(BindHelper.bindBeanProperty("name", config.getPlayer(), String.class));
        }

        Color color = config.getPlayer().getColor();
        
        img = find("image", UIImage.class);
        img.setImage(createTexture(10, 10, color));

        sliderRed = find("red", UISlider.class);
        sliderGreen = find("green", UISlider.class);
        sliderBlue = find("blue", UISlider.class);

        sliderRed.bindValue(new NotifyingBinding(color.rf()));
        sliderGreen.bindValue(new NotifyingBinding(color.gf()));
        sliderBlue.bindValue(new NotifyingBinding(color.bf()));
        
        WidgetUtil.trySubscribe(this, "close", new ActivateEventListener() {
            @Override
            public void onActivated(UIWidget button) {
                getManager().popScreen();
            }
        });
    }
    
    /**
     * 
     */
    public void update() {
        float red = sliderRed.getValue();
        float green = sliderGreen.getValue();
        float blue = sliderBlue.getValue();

        Color c = new Color(red, green, blue);

        config.getPlayer().setColor(c);
        
        img.setImage(createTexture(10, 10, c));
    }

    private Texture createTexture(int width, int height, Color color) {
        int size = 4 * width * height;

        ByteBuffer buf = ByteBuffer.allocateDirect(size);
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                color.addToBuffer(buf);
            }
        }
        buf.flip();

        ByteBuffer[] data = new ByteBuffer[]{buf};
        AssetUri uri = new AssetUri(AssetType.TEXTURE, "engine:colorPreview");
        TextureData texData = new TextureData(width, height, data, Texture.WrapMode.CLAMP, Texture.FilterMode.LINEAR);

        return Assets.generateAsset(uri, texData, Texture.class);
    }
    
    /**
     * Calls update() in parent class when the slider value changes
     */
    private final class NotifyingBinding extends DefaultBinding<Float> {

        private NotifyingBinding(Float value) {
            super(value);
        }

        @Override
        public void set(Float v) {
            super.set(v);
            
            update();
        }
    }
}

@@ -971,16 +971,76 @@
 reData;%0A
+import org.terasology.rendering.assets.texture.TextureUtil;%0A
 import o
@@ -2245,60 +2245,8 @@
 ss);
-%0A        img.setImage(createTexture(10, 10, color));
 %0A%0A  
@@ -2820,26 +2820,24 @@
     %7D);%0A    
-%7D%0A
     %0A    /**
@@ -2837,38 +2837,49 @@
     
-/**%0A     * 
+    updateImage();%0A    %7D
 %0A    
- */
 %0A    p
-ublic
+rivate
  voi
@@ -2886,16 +2886,21 @@
 d update
+Image
 () %7B%0A   
@@ -3044,16 +3044,20 @@
  Color c
+olor
  = new C
@@ -3118,16 +3118,20 @@
 tColor(c
+olor
 );%0A     
@@ -3146,712 +3146,140 @@
     
-img.setImage(createTexture(10, 10, c));%0A    %7D%0A%0A    private Texture createTexture(int width, int height, Color color) %7B%0A        int size = 4 * width * height;%0A%0A        ByteBuffer buf = ByteBuffer.allocateDirect(size);%0A        for (int y = 0; y %3C height; ++y) %7B%0A            for (int x = 0; x %3C width; ++x) %7B%0A                color.addToBuffer(buf);%0A            %7D%0A        %7D%0A        buf.flip();%0A%0A        ByteBuffer%5B%5D data = new ByteBuffer%5B%5D%7Bbuf%7D;%0A        AssetUri uri = new AssetUri(AssetType.TEXTURE, %22engine:colorPreview%22);%0A        TextureData texData = new TextureData(width, height, data, Texture.WrapMode.CLAMP, Texture.FilterMode.LINEAR);%0A%0A        return Assets.generateAsset(uri, texData, Texture.class
+AssetUri uri = TextureUtil.getTextureUriForColor(color);%0A        Texture tex =  (Texture) Assets.get(uri);%0A%0A        img.setImage(tex
 );%0A 
@@ -3279,28 +3279,24 @@
 tex);%0A    %7D%0A
-    
 %0A    /**%0A   
@@ -3637,16 +3637,21 @@
   update
+Image
 ();%0A    

Use TextureUtil to create color texture