package org.osmdroid.views.overlay;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;

/**
 * A ground overlay is an image that is fixed to 2 corners on a map.
 * @author pasniak inspired by zkhan's code in Avare
 *
 */
@SuppressWarnings("WeakerAccess")
public class GroundOverlay2 extends Overlay {

    private float   mLonL,
                    mLatU,
                    mLonR,
                    mLatD;
    protected Bitmap mImage;
	protected float mBearing;
	protected float mTransparency;
    private Matrix mStretchToFitTransformationMatrix;

	public GroundOverlay2() {
		super();
		mBearing = 0.0f;
		mTransparency = 0.0f;
        mStretchToFitTransformationMatrix = new Matrix();
	}

	public void setImage(Bitmap image){
		mImage = image;
	}

	public Bitmap getImage(){
		return mImage;
	}

	public void setPosition(GeoPoint UL, GeoPoint RD)
    {
        mLatU = (float)UL.getLatitude();
        mLonL = (float)UL.getLongitude();
        mLatD = (float)RD.getLatitude();
        mLonR = (float)RD.getLongitude();
	}

	public float getBearing(){
		return mBearing;
	}
	
	public void setBearing(float bearing){
		mBearing = bearing;
	}
	
	public void setTransparency(float transparency){
		mTransparency = transparency;
	}
	
	public float getTransparency(){
		return mTransparency;
	}
	

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {
		if(null == mImage) {
			return;
		}

        final Projection pj = mapView.getProjection();

        long x0 = pj.getLongPixelXFromLongitude(mLonL),
             y0 = pj.getLongPixelYFromLatitude(mLatU),
             x1 = pj.getLongPixelXFromLongitude(mLonR),
             y1 = pj.getLongPixelYFromLatitude(mLatD);

		float scaleX = (float)(x1 - x0) / mImage.getWidth(),
		      scaleY = (float)(y1 - y0) / mImage.getHeight();
        mStretchToFitTransformationMatrix.setScale(scaleX, scaleY);
        mStretchToFitTransformationMatrix.postTranslate(x0, y0);

        Paint paint = new Paint();
        paint.setAlpha(255-(int)(mTransparency * 255));
		canvas.drawBitmap(mImage, mStretchToFitTransformationMatrix, paint);
	}
}

@@ -1889,41 +1889,118 @@
 );%0A%0A
-%09%09float scaleX = (float)(x1 - x0)
+        float widthOnTheMap = x1 - x0,%0A              heightOnTheMap = y1 - y0;%0A%0A%09%09float scaleX = widthOnTheMap
  / m
@@ -2038,24 +2038,22 @@
 Y = 
-(float)(y1 - y0)
+heightOnTheMap
  / m
@@ -2071,24 +2071,25 @@
 ight();%0A
+%0A
         
 mStretch
@@ -2084,50 +2084,41 @@
     
-mStretchToFit
+setupScalingThen
 Trans
-form
+l
 ati
-o
 n
+g
 Matrix
-.setScale
 (sca
@@ -2132,67 +2132,10 @@
 aleY
-);%0A        mStretchToFitTransformationMatrix.postTranslate(
+, 
 x0, 
@@ -2304,11 +2304,250 @@
 int);%0A%09%7D
+%0A%0A    private void setupScalingThenTranslatingMatrix(float scaleX, float scaleY, long x0, long y0) %7B%0A        mStretchToFitTransformationMatrix.setScale(scaleX, scaleY);%0A        mStretchToFitTransformationMatrix.postTranslate(x0, y0);%0A    %7D
 %0A%7D%0A

add GroundOverlay2 - some explanations