package com.gh.gamecenter.common.view;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;

import com.gh.gamecenter.common.R;

import java.util.ArrayList;
import java.util.List;

public class MessageSpannableTextView extends AppCompatTextView {

    private OnSpannableClickListener mSpannableClickListener;

    private final String mStartMarkKey = "{{";
    private final String mEndMarkKey = "}}";

    private List<MarkData> mMarkList;

    public MessageSpannableTextView(Context context) {
        super(context);
    }

    public MessageSpannableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMarkList = new ArrayList<>();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (TextUtils.isEmpty(text) ||
                !text.toString().contains(mStartMarkKey) && !text.toString().contains(mEndMarkKey)) {
            super.setText(text, type);
            return;
        }

        mMarkList.clear();
        String sText = text.toString();
        String[] split = sText.split("\\}\\}");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            int p = s.lastIndexOf(mStartMarkKey);
            if (p != -1) {
                MarkData markData = new MarkData();
                String substring = s.substring(p);
                String content = s.replace(substring, "");
                builder.append(content);
                markData.setStart(builder.length() + 1);

                String key = substring.replace(mStartMarkKey, " ") + " ";
                builder.append(key);
                markData.setEnd(builder.length() - 1);
                markData.setKey(key);

                mMarkList.add(markData);
            } else {
                builder.append(s);
                if (i != split.length - 1 || sText.substring(sText.length() - 2, sText.length()).equals("}}")) {
                    builder.append("}}");
                }
            }
        }

        SpannableStringBuilder msp = new SpannableStringBuilder(builder);
        for (MarkData markData : mMarkList) {
            msp.setSpan(new ClickableSpan() {
                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(getContext().getResources().getColor(R.color.tag_orange));
                    ds.setUnderlineText(true);
                }

                @Override
                public void onClick(View widget) {
                    if (mSpannableClickListener != null) {
                        mSpannableClickListener.onClick(markData.getKey().trim());
                    }
                }
            }, markData.getStart(), markData.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        }

        setMovementMethod(CustomLinkMovementMethod.getInstance());
        super.setText(msp, type);
    }

    public void setOnSpannableClickListener(OnSpannableClickListener spannableClickListener) {
        this.mSpannableClickListener = spannableClickListener;
    }


    public interface OnSpannableClickListener {
        void onClick(String spannableText);
    }

    class MarkData {
        private int start;
        private int end;
        private String key;

        public int getStart() {
            return start;
        }

        public void setStart(int start) {
            this.start = start;
        }

        public int getEnd() {
            return end;
        }

        public void setEnd(int end) {
            this.end = end;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }
    }
}
