package com.github.anastr.speedviewlib.components.Indicators;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.github.anastr.speedviewlib.Speedometer;

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

    protected Paint indicatorPaint =  new Paint(Paint.ANTI_ALIAS_FLAG);
    private float density;
    private float indicatorWidth;
    private float viewWidth;
    private float viewHeight;
    private float speedometerWidth;
    private int indicatorColor;
    private int padding;
    private boolean inEditMode;

    protected Indicator (Speedometer speedometer) {
        updateData(speedometer);
        init();
    }

    private void init() {
        indicatorPaint.setColor(indicatorColor);
    }

    public abstract void draw(Canvas canvas, float degree);
    /** called when size change or color, width */
    protected abstract void updateIndicator();
    /** if indicator have effects like BlurMaskFilter */
    protected abstract void setWithEffects(boolean withEffects);

    /**
     * must call in {@code speedometer.onSizeChanged()}
     * @param speedometer target speedometer.
     */
    public void onSizeChange(Speedometer speedometer) {
        updateData(speedometer);
        updateIndicator();
    }

    private void updateData(Speedometer speedometer) {
        this.density = speedometer.getContext().getResources().getDisplayMetrics().density;
        this.indicatorWidth = speedometer.getIndicatorWidth();
        this.viewWidth = speedometer.getWidthPa();
        this.viewHeight = speedometer.getHeightPa();
        this.speedometerWidth = speedometer.getSpeedometerWidth();
        this.indicatorColor = speedometer.getIndicatorColor();
        this.padding = speedometer.getPadding();
        this.inEditMode = speedometer.isInEditMode();
    }

    public float dpTOpx(float dp) {
        return dp * density;
    }

    public float getIndicatorWidth() {
        return indicatorWidth;
    }

    public void setIndicatorWidth(float indicatorWidth) {
        this.indicatorWidth = indicatorWidth;
        updateIndicator();
    }

    public float getViewWidth() {
        return viewWidth;
    }

    public float getViewHeight() {
        return viewHeight;
    }

    public int getIndicatorColor() {
        return indicatorColor;
    }

    public void setIndicatorColor(int indicatorColor) {
        this.indicatorColor = indicatorColor;
        updateIndicator();
    }

    public float getCenterX() {
        return viewWidth /2f;
    }

    public float getCenterY() {
        return viewHeight /2f;
    }

    public int getPadding() {
        return padding;
    }

    public float getSpeedometerWidth() {
        return speedometerWidth;
    }

    public void setSpeedometerWidth(float speedometerWidth) {
        this.speedometerWidth = speedometerWidth;
        updateIndicator();
    }

    public void noticePaddingChange(int newPadding) {
        this.padding = newPadding;
        updateIndicator();
    }

    public void withEffects(boolean withEffects) {
        setWithEffects(withEffects);
        updateIndicator();
    }

    public boolean isInEditMode() {
        return inEditMode;
    }

    public enum Indicators {
        NoIndicator, NormalIndicator, NormalSmallIndicator, TriangleIndicator
        , SpindleIndicator, LineIndicator, HalfLineIndicator, QuarterLineIndicator
    }

}

@@ -287,16 +287,57 @@
 /a%3E%0A */%0A
+@SuppressWarnings(%22unused,WeakerAccess%22)%0A
 public a
@@ -2619,32 +2619,45 @@
 %7B%0A        return
+ (2*padding +
  viewWidth /2f;%0A
@@ -2650,16 +2650,17 @@
 iewWidth
+)
  /2f;%0A  
@@ -2702,32 +2702,45 @@
 %7B%0A        return
+ (2*padding +
  viewHeight /2f;
@@ -2734,16 +2734,17 @@
 ewHeight
+)
  /2f;%0A  

fix center indicator with padding