package ru.m.puzzlez.render;

import flash.geom.Rectangle;
import haxework.view.SpriteView;
import haxework.view.utils.DrawUtil;
import ru.m.puzzlez.core.GameState;

class Render extends SpriteView implements IRender {

    private var state:GameState;

    public function new() {
        super();
    }

    public function draw(state:GameState):Void {
        this.state = state;
        toRedraw();
    }

    override public function redraw():Void {
        super.redraw();
        if (state != null) {
            var rect = RenderUtil.containRectangle(new Rectangle(0, 0, state.image.width, state.image.height), rect);
            DrawUtil.draw(content.graphics, state.image, rect, FillType.CONTAIN);
            content.graphics.lineStyle(1, 0x00ff00);
            for (i in 0...state.preset.grid.width + 1) {
                var x = rect.x + i * rect.width / state.preset.grid.width;
                content.graphics.moveTo(x, rect.top);
                content.graphics.lineTo(x, rect.bottom);
            }
            for (i in 0...state.preset.grid.height + 1) {
                var y = rect.y + i * rect.height / state.preset.grid.height;
                content.graphics.moveTo(rect.left, y);
                content.graphics.lineTo(rect.right, y);
            }
            content.graphics.lineStyle();
        }
    }
}
