package org.stepic.droid.ui.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import org.stepic.droid.R;
import org.stepic.droid.base.App;
import org.stepic.droid.util.resolvers.text.TextResolver;

import javax.inject.Inject;

public class ExpandableTextView extends TextView {
    private static final int DEFAULT_TRIM_LENGTH = 200;
    private final String ELLIPSIS;
    private CharSequence originalText;
    private CharSequence trimmedText;
    private BufferType bufferType;
    private boolean trim = true;
    private int trimLength;

    @Inject
    TextResolver textResolver;

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

    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        App.component().inject(this);

        if (isInEditMode()) {
            ELLIPSIS = "<font color=" + "#CCCCCC" + ">"
                    + "more..." + "</font>";
        } else {
            ELLIPSIS = "<font color=" + App.getAppContext().getResources().getColor(R.color.default_color_of_link) + ">"
                    + App.getAppContext().getString(R.string.tap_to_see_more) + "</font>";
        }

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
        this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
        typedArray.recycle();

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                trim = !trim;
                setText();
//                requestFocusFromTouch();
            }
        });
    }

    private void setText() {
        super.setText(getDisplayableText(), bufferType);
    }

    private CharSequence getDisplayableText() {
        return trim ? trimmedText : originalText;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        trimmedText = getTrimmedText(text);
        bufferType = type;
        setText();
    }

    private CharSequence getTrimmedText(CharSequence text) {
        if (originalText != null && originalText.length() > trimLength) {
            return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(" ").append(textResolver.fromHtml(ELLIPSIS));
        } else {
            return originalText;
        }
    }

    public CharSequence getOriginalText() {
        return originalText;
    }

    public void setTrimLength(int trimLength) {
        this.trimLength = trimLength;
        trimmedText = getTrimmedText(originalText);
        setText();
    }

    public int getTrimLength() {
        return trimLength;
    }
}
@@ -101,16 +101,68 @@
 dArray;%0A
+import android.support.v7.widget.AppCompatTextView;%0A
 import a
@@ -260,40 +260,8 @@
 iew;
-%0Aimport android.widget.TextView;
 %0A%0Aim
@@ -447,16 +447,25 @@
 extends 
+AppCompat
 TextView

use appcompat textview for custom textview instead of textView