package net.yaht.m.dynview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by lisp on 5/9/16.
 */
public class DrawingPlace extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder _surfaceHolder;
    // Paint.ANTI_ALIAS_FLAG
    private final Paint _paint = new Paint();

    public PaintThread _pthread = null;

    public DrawingPlace(Context context) {

        super(context);

        // step 1.v0) - getting the holder;
        _surfaceHolder = getHolder();
        _surfaceHolder.addCallback(this);

        Tools.log2me("DrawingPlace/Constructor.v0", "step 1 done;");

    }

    public DrawingPlace(Context context, AttributeSet attrs) {
        super(context, attrs);

        // step 1.v1) - getting the holder;
        _surfaceHolder = getHolder();
        _surfaceHolder.addCallback(this);
        Tools.log2me("DrawingPlace/Constructor.v1", "step 1 done;");

    }

    public DrawingPlace(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // step 1.v2) - getting the holder;
        _surfaceHolder = getHolder();
        _surfaceHolder.addCallback(this);
        Tools.log2me("DrawingPlace/Constructor.v2", "step 1 done;");

    }

    /*
    API lavel 21 this one is capable of something;
    public DrawingPlace(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    */

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        Rect r = holder.getSurfaceFrame();

        Tools.log2me("DrawingPlace/surfaceCreated", "left: " + r.left);
        Tools.log2me("DrawingPlace/surfaceCreated", "right: " + r.right);

        Tools.log2me("DrawingPlace/surfaceCreated", "top: " + r.top);
        Tools.log2me("DrawingPlace/surfaceCreated", "bottom: " + r.bottom);

        Tools.log2me("DrawingPlace/surfaceCreated", "width: " + r.width());
        Tools.log2me("DrawingPlace/surfaceCreated", "height: " + r.height());

        // step 2) initializing our canvas (with random color);
        // the same object from our constructor, i.e. we can make this changes there;
        Canvas c = holder.lockCanvas();
        _paint.setColor(Color.GREEN);
        c.drawRect(r.left, r.top, r.right, r.bottom, _paint);
        holder.unlockCanvasAndPost(c);

        // step 3) make a nice looking thread to control the drawings...
        // and control it via buttons for example ;)
        Tools.log2me("DrawingPlace/surfaceCreated", "done.");

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

         _pthread = new PaintThread(holder, holder.getSurfaceFrame(), _paint);
         _pthread.start();

        Tools.log2me("DrawingPlace/surfaceChanged", "width: " + width);
        Tools.log2me("DrawingPlace/surfaceChanged", "height: " + height);

        Tools.log2me("DrawingPlace/surfaceChanged", "done.");

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        Tools.log2me("DrawingPlace/surfaceChanged", "done.");

    }

}
