package com.github.anastr.speedviewlib;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.github.anastr.speedviewlib.base.LinearGauge;

/**
 * this Library build By Anas Altair
 * see it on <a href="https://github.com/anastr/SpeedView">GitHub</a>
 */
public class ImageLinearGauge extends LinearGauge {

    private Drawable image;

    private int backColor = Color.parseColor("#d6d7d7");

    public ImageLinearGauge(Context context) {
        this(context, null);
    }

    public ImageLinearGauge(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageLinearGauge(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttributeSet(context, attrs);
    }

    @Override
    protected void defaultValues() {
        super.setSpeedTextPosition(Position.CENTER);
        super.setUnitUnderSpeedText(true);
    }

    private void initAttributeSet(Context context, AttributeSet attrs) {
        if (attrs == null)
            return;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ImageLinearGauge, 0, 0);

        backColor = a.getColor(R.styleable.ImageLinearGauge_sv_speedometerBackColor, backColor);
        image = a.getDrawable(R.styleable.ImageLinearGauge_sv_image);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (image == null
                || image.getIntrinsicWidth() == -1 || image.getIntrinsicHeight() == -1)
            return;
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();
        if (getOrientation() == Orientation.HORIZONTAL) {
            float image_w_to_h = (float) image.getIntrinsicWidth() / (float) image.getIntrinsicHeight();
            if (image_w_to_h > 1f)
                setMeasuredDimension(w, (int) (w / image_w_to_h));
            else
                setMeasuredDimension(w, (int) (w * image_w_to_h));
        }
        else {
            float image_h_to_w = (float) image.getIntrinsicHeight() / (float) image.getIntrinsicWidth();
            if (image_h_to_w > 1f)
                setMeasuredDimension((int) (h / image_h_to_w), h);
            else
                setMeasuredDimension((int) (h * image_h_to_w), h);
        }
    }

    @Override
    protected void updateFrontAndBackBitmaps() {
        Canvas canvasBack = createBackgroundBitmapCanvas();
        Canvas canvasFront = createForegroundBitmapCanvas();

        if (image != null) {
            image.setBounds(getPadding(), getPadding(), getWidth() - getPadding(), getHeight() - getPadding());
            image.setColorFilter(backColor, PorterDuff.Mode.SRC_IN);
            image.draw(canvasBack);

            image.setColorFilter(null);
            image.draw(canvasFront);
        }
    }
}

@@ -1905,70 +1905,8 @@
 ();%0A
-        if (getOrientation() == Orientation.HORIZONTAL) %7B%0A    
     
@@ -1920,23 +1920,17 @@
 at image
-_w_to_h
+W
  = (floa
@@ -1957,18 +1957,40 @@
 cWidth()
- /
+;%0A        float imageH =
  (float)
@@ -2030,215 +2030,35 @@
     
-    if (image_w_to_h %3E 1f)%0A                setMeasuredDimension(w, (int) (w / image_w_to_h));%0A            else%0A                setMeasuredDimension(w, (int) (w * image_w_to_h));%0A        %7D%0A        else %7B%0A    
+float view_w_to_h = w / h;%0A
     
@@ -2077,93 +2077,35 @@
 age_
-h
+w
 _to_
-w = (float) image.getIntrinsicHeight() / (float) image.getIntrinsicWidth();%0A    
+h = imageW / imageH;%0A%0A
     
@@ -2122,25 +2122,30 @@
 age_
-h
+w
 _to_
-w %3E 1f)%0A    
+h %3E view_w_to_h)%0A
     
@@ -2177,35 +2177,38 @@
 ion(
+w, 
 (int) (
-h / image_h_to_w), h
+w * imageH / imageW)
 );%0A 
@@ -2218,21 +2218,13 @@
     
-    
 else%0A
-    
     
@@ -2257,17 +2257,16 @@
 on((int)
- 
 (h * ima
@@ -2271,31 +2271,24 @@
 mage
-_h_to_w), h);%0A        %7D
+W / imageH), h);
 %0A   

fix image scale.