package sauer.thatgameimade;

import android.content.Context;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class LevelEditorView extends View {

    @SuppressWarnings("unused")
    private static final String TAG = LevelEditorView.class.getSimpleName();
    private BitmapShader backgroundShader;

    private Paint backgroundPaint;
    private Paint bitmapPaint;
    private Paint touchPaint;

    private float touchX;
    private float touchY;

    private Matrix backgroundMatrix = new Matrix();
    private Matrix drawMatrix = new Matrix();

    private LevelHolder levelHolder;
    private float scale = 1.4f;
    private int blockIndex;

    public LevelEditorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void init() {
        backgroundShader = new BitmapShader(levelHolder.getBackgroundBitmap(), Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);

        backgroundPaint = new Paint();
        backgroundPaint.setShader(backgroundShader);

        bitmapPaint = new Paint();

        touchPaint = new Paint();
        touchPaint.setColor(Color.argb(75, 255, 255, 255));
    }

    @Override
    protected void onSizeChanged(int weight, int height, int oldWidth, int oldHeight) {
        if (isInEditMode()) {
            return;
        }
        backgroundMatrix.setScale(scale, scale);
        backgroundShader.setLocalMatrix(backgroundMatrix);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isInEditMode()) {
            return;
        }

        canvas.drawRect(0, 0, levelHolder.getBackgroundBitmap().getWidth() * scale, levelHolder.getBackgroundBitmap().getHeight() * scale, backgroundPaint);

        // TODO avoid conditional
        if (levelHolder != null) {
            paintBitmaps(canvas);
        }
    }

    private void paintBitmaps(Canvas canvas) {
        for (int i = 0; i < levelHolder.getLevelBlocksX(); i++) {
            for (int j = 0; j < levelHolder.getLevelBlocksY(); j++) {
                int blockIndex = levelHolder.getLevelBlocks()[i][j];
                drawMatrix.reset();
                drawMatrix.postTranslate(i * levelHolder.getBlockSize(), j * levelHolder.getBlockSize());
                drawMatrix.postScale(scale, scale);
                canvas.drawBitmap(levelHolder.getBlockList().get(blockIndex).getBitmap(), drawMatrix, bitmapPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (event.getPointerCount() == 1) {
            this.touchX = event.getX();
            this.touchY = event.getY();
            int x = (int) (touchX / scale / levelHolder.getBlockSize());
            int y = (int) (touchY / scale / levelHolder.getBlockSize());
            try {
                levelHolder.getLevelBlocks()[x][y] = blockIndex;
            } catch (IndexOutOfBoundsException ignore) {
            }
            invalidate();
            return true;
        }
        return false;
    }

    public void setLevelHolder(LevelHolder levelHolder) {
        this.levelHolder = levelHolder;
        init();
    }

    public void setBlockIndex(int blockIndex) {
        this.blockIndex = blockIndex;
    }
}

@@ -129,39 +129,8 @@
 as;%0A
-import android.graphics.Color;%0A
 impo
@@ -624,91 +624,8 @@
 int;
-%0A    private Paint touchPaint;%0A%0A    private float touchX;%0A    private float touchY;
 %0A%0A  
@@ -785,15 +785,8 @@
 cale
- = 1.4f
 ;%0A  
@@ -1202,103 +1202,8 @@
 t();
-%0A%0A        touchPaint = new Paint();%0A        touchPaint.setColor(Color.argb(75, 255, 255, 255));
 %0A   
@@ -1258,21 +1258,20 @@
 ed(int w
-eight
+idth
 , int he
@@ -1359,32 +1359,184 @@
 turn;%0A        %7D%0A
+        scale = Math.min((float) width / levelHolder.getBackgroundBitmap().getWidth(), (float) height / levelHolder.getBackgroundBitmap().getHeight());%0A
         backgrou
@@ -2789,62 +2789,23 @@
     
-this.touchX = event.getX();%0A            this.touchY = 
+int x = (int) (
 even
@@ -2813,46 +2813,11 @@
 .get
-Y
+X
 ()
-;%0A            int x = (int) (touchX
  / s
@@ -2883,14 +2883,20 @@
 t) (
-touchY
+event.getY()
  / s

scale level to available space