/*
 * Framework code written for the Multimedia course taught in the first year
 * of the UvA Informatica bachelor.
 *
 * Nardi Lam, 2015 (based on code by I.M.J. Kamps, S.J.R. van Schaik, R. de Vries, 2013)
 */

package nl.uva.multimedia.image;

import android.content.Context;
import android.graphics.Canvas;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;

import nl.uva.multimedia.ImageActivity;
import nl.uva.multimedia.R;


/*
 * This is a View that displays incoming images.
 */
public class ImageDisplayView extends View implements ImageListener {

    /*** Constructors ***/

    public ImageDisplayView(Context context) {
        super(context);
    }

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

    public ImageDisplayView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /*** Image drawing ***/

    private int[] currentImage = null;
    private int imageWidth, imageHeight;

    // Rotation dependency
    private ImageRotation imgRot = null;
    private int[] rotatedImage = null;
    private static float rotation_amount = 0;
    public static float scale = 1;

    public static boolean dieKnop = false;

    public void setScale(float scaleAmount) {
        this.scale += scaleAmount;
    }

    public float setMyRotation(float degrees) {


        rotation_amount = (degrees);
//        if (rotation_amount < 0){
//
//            rotation_amount = 0;
//
//
//        }
//
//        if (rotation_amount > 360){
//            rotation_amount = 360;
//        }

        return rotation_amount;


    }


    public float addMyRotation(float degrees) {


        if (rotation_amount - degrees / 10 < 0) {

            rotation_amount = 0;


        } else if (rotation_amount - degrees / 10 > 360) {
            rotation_amount = 360;
        } else {
            rotation_amount -= (degrees / 10);
        }

        return rotation_amount;


    }


    @Override
    public void onImage(int[] argb, int width, int height) {
        /* When we recieve an image, simply store it and invalidate the View so it will be
         * redrawn. */
        this.currentImage = argb;
        this.imageWidth = width;
        this.imageHeight = height;
        this.invalidate();

        // Initialize rotation object
        this.imgRot = new ImageRotation(this.currentImage, this.imageWidth, this.imageHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // TODO: Hier wordt een afbeelding op het scherm laten zien!
        // Je zou hier dus code kunnen plaatsen om iets anders weer te geven.

        /* If there is an image to be drawn: */

        if (this.imgRot != null) {

            // Determine which algorithm to use
            if (dieKnop) {

                this.rotatedImage = imgRot.rotateBI(rotation_amount);
            } else {

                this.rotatedImage = imgRot.rotateNN(rotation_amount);
            }


            /* Center the image... */
            int left = (this.getWidth() - this.imageWidth) / 2;
            int top = (this.getHeight() - this.imageHeight) / 2;
            canvas.save();
            canvas.scale(scale, scale, getWidth() / 2, getHeight() / 2);

            /* ...and draw it. */
            canvas.drawBitmap(this.rotatedImage, 0, this.imageWidth, left, top, this.imageWidth,
                    this.imageHeight, true, null);


            canvas.restore();
        }
    }

    /*** Source selection ***/

    private ImageSource source = null;

    public void setImageSource(ImageSource source) {
        if (this.source != null) {
            this.source.setOnImageListener(null);
        }
        source.setOnImageListener(this);
        this.source = source;
    }

    public ImageSource getImageSource() {
        return this.source;
    }

}
