/**
 * Copyright 2012 The PlayN Authors
 *
 * 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 playn.tests.core;

import playn.core.Canvas;
import playn.core.CanvasImage;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Layer;
import playn.core.ResourceCallback;
import static playn.core.PlayN.*;

public class CanvasTest extends Test {

  private final static float GAP = 10;
  private float nextX, nextY, maxY;

  @Override
  public String getName() {
    return "CanvasTest";
  }

  @Override
  public String getDescription() {
    return "Tests various Canvas rendering features.";
  }

  @Override
  public void init() {
    nextX = nextY = GAP;

    addTestCanvas(100, 100, new Drawer() {
      public void draw(Canvas canvas) {
        canvas.setFillGradient(graphics().createRadialGradient(
                                 0, 0, 50, new int[] { 0xFFFF0000, 0xFF00FF00 },
                                 new float[] { 0, 1 }));
        canvas.fillRect(0, 0, 100, 100);
      }
    });

    addTestCanvas(100, 100, new Drawer() {
      public void draw(Canvas canvas) {
        canvas.setFillGradient(graphics().createLinearGradient(
                                 0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 },
                                 new float[] { 0, 1 }));
        canvas.fillRect(0, 0, 100, 100);
      }
    });

    addTestCanvas(100, 100, "images/tile.png", new ImageDrawer() {
      public void draw(Canvas canvas, Image tile) {
        canvas.setFillPattern(graphics().createPattern(tile));
        canvas.fillRect(0, 0, 100, 100);
      }
    });

    addTestCanvas(100, 100, new Drawer() {
      public void draw(Canvas canvas) {
        canvas.setFillColor(0xFF99CCFF);
        canvas.fillRect(0, 0, 100, 100);
        // draw a point and some lines
        canvas.setStrokeColor(0xFFFF0000);
        canvas.drawPoint(50, 50);
        canvas.drawLine(0, 25, 100, 25);
        canvas.drawLine(0, 75, 100, 75);
        canvas.drawLine(25, 0, 25, 100);
        canvas.drawLine(75, 0, 75, 100);
        // stroke and fill a circle
        canvas.strokeCircle(25, 75, 10);
        canvas.setFillColor(0xFF0000FF);
        canvas.fillCircle(75, 75, 10);
      }
    });

    addTestCanvas(100, 100, "images/pea.png", new ImageDrawer() {
      public void draw(Canvas canvas, Image pea) {
        canvas.setFillColor(0xFF99CCFF);
        canvas.fillRect(0, 0, 100, 100);

        // draw an image normally, scaled, cropped, cropped and scaled, etc.
        float half = 37/2f;
        canvas.drawImage(pea, 10, 10);
        canvas.drawImage(pea, 55, 10, 37, 37, half, half, half, half);
        canvas.drawImage(pea, 10, 55, 37, 37, half, 0, half, half);
        canvas.drawImage(pea, 55, 55, 37, 37, half, half/2, half, half);
      }
    });

    ImageLayer layer = graphics().createImageLayer(createCanvasImage(30, 30, new Drawer() {
      public void draw(Canvas canvas) {
        canvas.setFillColor(0xFF99CCFF);
        canvas.fillCircle(15, 15, 15);
        canvas.setStrokeColor(0xFF000000);
        canvas.strokeRect(0, 0, 29, 29);
      }
    }));
    layer.setRepeatX(true);
    layer.setRepeatY(true);
    layer.setSize(100, 100);
    addTestLayer(100, 100, layer);
  }

  private interface Drawer {
    void draw(Canvas canvas);
  }

  private void addTestCanvas(int width, int height, Drawer drawer) {
    CanvasImage image = createCanvasImage(width, height, drawer);
    addTestLayer(width, height, graphics().createImageLayer(image));
  }

  private CanvasImage createCanvasImage(int width, int height, final Drawer drawer) {
    final CanvasImage image = graphics().createImage(width, height);
    drawer.draw(image.canvas());
    return image;
  }

  private void addTestLayer(int width, int height, Layer layer) {
    // if this layer won't fit in this row, wrap down to the next
    if (nextX + width > graphics().width()) {
      nextY += (maxY + GAP);
      nextX = GAP;
      maxY = 0;
    }
    layer.setTranslation(nextX, nextY);
    graphics().rootLayer().add(layer);
    // update our positioning info
    nextX += (width + GAP);
    maxY = Math.max(maxY, height);
  }

  private interface ImageDrawer {
    void draw(Canvas canvas, Image image);
  }

  private void addTestCanvas(int width, int height, String imagePath, final ImageDrawer drawer) {
    final CanvasImage target = graphics().createImage(width, height);
    assets().getImage(imagePath).addCallback(new ResourceCallback<Image>() {
      public void done(Image image) {
        drawer.draw(target.canvas(), image);
      }
      public void error(Throwable err) {
        System.err.println("Oops! " + err);
      }
    });
    addTestLayer(width, height, graphics().createImageLayer(target));
  }
}

@@ -3595,14 +3595,14 @@
  0, 
-29, 29
+30, 30
 );%0A 

Use the fill size of the rect.