package com.atmob.map_amap_android.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class BatteryView extends View {

    private int level = 100;

    private Paint borderPaint;
    private Paint fillPaint;
    private Paint headPaint;

    private int borderColor = Color.parseColor("#4476FF");
    private int fillColor = Color.parseColor("#4476FF");
    private int lowColor = Color.parseColor("#4476FF");
    private int normalColor = Color.parseColor("#4476FF");

    private RectF bodyRect = new RectF();
    private RectF headRect = new RectF();
    private RectF fillRect = new RectF();

    private float borderStrokeWidth = 6f;
    private float headStrokeWidth = 6f;
    private float cornerRadius = 3f;
    private float headGap = 6f;

    public BatteryView(Context context) {
        super(context);
        init();
    }

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

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        float headWidth = headStrokeWidth;
        float headHeight = h / 2f;
        float gap = headGap;

        float bodyRight = w - headWidth - gap - borderStrokeWidth;
        bodyRect.set(
                borderStrokeWidth / 2f,
                borderStrokeWidth / 2f,
                bodyRight,
                h - borderStrokeWidth / 2f
        );

        float headLeft = bodyRight + gap;
        float headTop = (h - headHeight) / 2f;
        headRect.set(headLeft, headTop, headLeft + headWidth, headTop + headHeight);
    }

    private void init() {
        borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        borderPaint.setColor(borderColor);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(borderStrokeWidth);

        fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        fillPaint.setColor(fillColor);

        headPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        headPaint.setColor(borderColor);
        headPaint.setStyle(Paint.Style.FILL);
    }

    public void setBatteryLevel(int level) {
        this.level = Math.max(0, Math.min(100, level));
        invalidate();
    }

    public void setBorderStrokeWidth(float strokeWidth) {
        this.borderStrokeWidth = strokeWidth;
        borderPaint.setStrokeWidth(strokeWidth);
        invalidate();
    }

    public void setHeadStrokeWidth(float strokeWidth) {
        this.headStrokeWidth = strokeWidth;
        invalidate();
    }

    public void setCornerRadius(float radius) {
        this.cornerRadius = radius;
        invalidate();
    }

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

        canvas.drawRoundRect(bodyRect, cornerRadius, cornerRadius, borderPaint);
        canvas.drawRect(headRect, headPaint);

        fillPaint.setColor(level <= 20 ? lowColor : normalColor);

        float fillPadding = borderStrokeWidth;
        float fillWidth = (bodyRect.width() - fillPadding * 2) * level / 100f;
        fillRect.set(
                bodyRect.left + fillPadding,
                bodyRect.top + fillPadding,
                bodyRect.left + fillPadding + fillWidth,
                bodyRect.bottom - fillPadding
        );
        canvas.drawRoundRect(fillRect, cornerRadius / 1.5f, cornerRadius / 1.5f, fillPaint);
    }
}
