package ru.m.puzzlez.core;

import flash.geom.Matrix;
import flash.display.BitmapData;
import flash.geom.Rectangle;

class GameUtil {
    public static function buildRectangle(source:Rectangle, grid:Grid):Rectangle {
        var r1 = grid.width / grid.height;
        var r2 = source.width / source.height;
        if (r1 > r2) {
            var height = source.width * r1;
            return new Rectangle(0, (source.height - height) / 2, source.width, height);
        } else {
            var width = source.height * r1;
            return new Rectangle((source.width - width) / 2, 0, width, source.height);
        }
    }

    public static function buildState(preset:GamePreset):GameState {
        var rect = buildRectangle(new Rectangle(0, 0, preset.image.width, preset.image.height), preset.grid);
        var image = new BitmapData(Std.int(rect.width), Std.int(rect.height));
        var matrix = new Matrix();
        matrix.translate(-rect.x, -rect.y);
        image.draw(preset.image, matrix);
        return {
            preset: preset,
            image: image,
        }
    }
}
