package com.bokecc.livemodule.view.subtitle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;

public class OutlineTextView extends AppCompatTextView {
    private static final String TAG = "OutlineTextView";

    private TextPaint textPaint;
    private TextPaint outlinePaint;
    private StaticLayout staticLayout;
    private float strokeWidth = 4.0f;
    private int outlineColor = Color.BLACK;
    private int maxHeight = Integer.MAX_VALUE;

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

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

    public OutlineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(dip2px(getContext(),getTextSize()));

        outlinePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        outlinePaint.setStyle(Paint.Style.STROKE);
        outlinePaint.setStrokeWidth(strokeWidth);
        outlinePaint.setColor(outlineColor);
        outlinePaint.setTextSize(dip2px(getContext(),getTextSize()));
    }



    @Override
    protected void onDraw(Canvas canvas) {

        String text = getText().toString();


        int width = getWidth() - getPaddingLeft() - getPaddingRight();
        int height = getHeight() - getPaddingTop() - getPaddingBottom();


        StaticLayout fullTextLayout = new StaticLayout(
                text,
                textPaint,
                width,
                Layout.Alignment.ALIGN_CENTER,
                1.0f,
                0.0f,
                false
        );

        if (fullTextLayout.getHeight() > height) {
            int lineHeight = getLineHeight();
            int lineCount = height/lineHeight;
            text = TextUtils.ellipsize(text, textPaint, fullTextLayout.getWidth()*lineCount-150, TextUtils.TruncateAt.END).toString();
        }


        staticLayout = new StaticLayout(
                text,
                textPaint,
                width,
                Layout.Alignment.ALIGN_CENTER,
                1.0f,
                0.0f,
                false
        );
        float textHeight = staticLayout.getHeight();
        float verticalOffset = (height - textHeight) / 2;

        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop() + verticalOffset);

        // Draw outline
        for (int i = 0; i < staticLayout.getLineCount(); i++) {
            float x = staticLayout.getLineLeft(i);
            float y = staticLayout.getLineBaseline(i);
            canvas.drawText(text, staticLayout.getLineStart(i), staticLayout.getLineEnd(i), x, y, outlinePaint);
        }

        // Draw text
        canvas.translate(0, 0);
        staticLayout.draw(canvas);
        canvas.restore();
    }

    @Override
    public void setTextColor(int color) {
        super.setTextColor(color);
        if (textPaint != null) {
            textPaint.setColor(color);
            invalidate();
        }
    }

    @Override
    public void setTextSize(float size) {
        super.setTextSize(size);
        if (textPaint != null) {
            textPaint.setTextSize(dip2px(getContext(),size));
        }
        if (outlinePaint != null) {
            outlinePaint.setTextSize(dip2px(getContext(),size));
        }
        invalidate();
    }

    private static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
        requestLayout();
        invalidate();
    }
    public void setStrokeWidth(float width) {
        strokeWidth = width;
        if (outlinePaint != null) {
            outlinePaint.setStrokeWidth(width);
            invalidate();
        }
    }

    public void setOutlineColor(int color) {
        outlineColor = color;
        if (outlinePaint != null) {
            outlinePaint.setColor(color);
            invalidate();
        }
    }
}