package com.datarecovery.master.utils;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class SpannableUtil {

    public static SpannableStringBuilder getSpannableStringBuilder(SpannableStringBuilder spannableStringBuilder, String targetTxt, int color, boolean isLine) {
        if (spannableStringBuilder == null) {
            return null;
        }
        int targetTextStart = spannableStringBuilder.toString().indexOf(targetTxt);
        ClickableSpan targetSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {

            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
                ds.setUnderlineText(isLine);
            }
        };
        spannableStringBuilder.setSpan(targetSpan, targetTextStart,
                targetTextStart + targetTxt.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    }

    public static SpannableStringBuilder getSpannableStringBuilder(String allTxt, String targetTxt, int color, boolean isLine) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(allTxt);
        int targetTextStart = allTxt.indexOf(targetTxt);
        ClickableSpan targetSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {

            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
                ds.setUnderlineText(isLine);
            }
        };
        spannableStringBuilder.setSpan(targetSpan, targetTextStart,
                targetTextStart + targetTxt.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    }


    public static SpannableStringBuilder getSpannableStringBuilder(String allTxt, String[] targetTxt, int color, boolean isLine) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(allTxt);
        int[] targetTextStart = new int[targetTxt.length];
        for (int i = 0; i < targetTxt.length; i++) {
            targetTextStart[i] = allTxt.indexOf(targetTxt[i]);
        }
        for (int i = 0; i < targetTxt.length; i++) {
            ClickableSpan targetSpan = new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {

                }

                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    ds.setColor(color);
                    ds.setUnderlineText(isLine);
                }
            };
            if (targetTextStart[i] < 0) {
                continue;
            }
            spannableStringBuilder.setSpan(targetSpan, targetTextStart[i],
                    targetTextStart[i] + targetTxt[i].length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    }


    public static void getAgreementSpannableStringBuilder(TextView targetView, String allTxt, String[] targetTxt, int color, boolean isLine, View.OnClickListener... clickListener) {
        if (clickListener.length > 0)
            targetView.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(allTxt);
        int[] targetTextStart = new int[targetTxt.length];
        for (int i = 0; i < targetTxt.length; i++) {
            targetTextStart[i] = allTxt.indexOf(targetTxt[i]);
        }

        for (int i = 0; i < targetTxt.length; i++) {
            int finalI = i;
            ClickableSpan targetSpan = new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {
                    if (finalI < clickListener.length && clickListener[finalI] != null) {
                        clickListener[finalI].onClick(widget);
                    }
                }

                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    ds.setColor(color);
                    ds.setUnderlineText(isLine);
                }
            };
            if (targetTextStart[i] < 0) {
                continue;
            }
            spannableStringBuilder.setSpan(targetSpan, targetTextStart[i],
                    targetTextStart[i] + targetTxt[i].length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        targetView.setText(spannableStringBuilder);
    }
}
