package studio.carbonylgroup.textfieldboxes;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;


/**
 * Text Field Boxes
 * Created by CarbonylGroup on 2017/09/01
 */
public class ExtendedEditText extends AppCompatEditText {

    protected String prefixText = "";
    protected String suffixText = "";
    protected ColorStateList prefixTextColor;
    protected ColorStateList suffixTextColor;

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

    public ExtendedEditText(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public ExtendedEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private class TextDrawable extends Drawable {

        private TextDrawable() {

            setBounds(0, 0, (int) getPaint().measureText(prefixText) + 2, (int) getTextSize());
            setPadding(0, 0, getPaddingRight() + (int) getPaint().measureText(suffixText) - 2, 0);
        }

        @Override
        public void draw(Canvas canvas) {

            int lineBaseline = getLineBounds(0, null);
            float endX = getWidth() - getPaddingRight() + (int) getPaint().measureText(suffixText) - 2 - getPaint().measureText(suffixText);

            Paint paint = getPaint();
            paint.setColor(prefixTextColor.getColorForState(getDrawableState(), 0));
            canvas.drawText(prefixText, 0, canvas.getClipBounds().top + lineBaseline, paint);
            paint.setColor(suffixTextColor.getColorForState(getDrawableState(), 0));
            canvas.drawText(suffixText, endX, canvas.getClipBounds().bottom - getLineHeight() + lineBaseline, paint);
        }

        @Override
        public void setAlpha(int alpha) {/* Not supported */}

        @Override
        public void setColorFilter(ColorFilter colorFilter) {/* Not supported */}

        @Override
        public int getOpacity() {
            return PixelFormat.OPAQUE;
        }
    }

    public void setPrefix(String _prefix) {

        prefixText = _prefix;
        setCompoundDrawables(new TextDrawable(), null, null, null);
    }

    public void setSuffix(String _suffix) {

        suffixText = _suffix;
        setCompoundDrawables(new TextDrawable(), null, null, null);
    }

    public void setPrefixTextColor(int color) {
        prefixTextColor = ColorStateList.valueOf(color);
    }

    public void setPrefixTextColor(ColorStateList color) {
        prefixTextColor = color;
    }

    public void setSuffixTextColor(int color) {
        suffixTextColor = ColorStateList.valueOf(color);
    }

    public void setSuffixTextColor(ColorStateList color) {
        suffixTextColor = color;
    }
}

@@ -1292,20 +1292,16 @@
 lineBase
-line
  = getLi
@@ -1310,16 +1310,86 @@
 Bounds(0
+, null);%0A            int lineBottom = getLineBounds(getLineCount() - 1
 , null);
@@ -1726,36 +1726,32 @@
 ).top + lineBase
-line
 , paint);%0A      
@@ -1902,40 +1902,19 @@
 s().
-bottom - getLineHeight()
+top
  + lineB
 asel
@@ -1913,15 +1913,13 @@
 ineB
-aseline
+ottom
 , pa

fix line bounds