package com.datarecovery.master.dialog;

import android.content.Context;
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 androidx.annotation.NonNull;

import com.datarecovery.master.dialog.BaseDialog;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.Constants;
import com.datarecovery.master.databinding.DialogAgreementBinding;
import com.datarecovery.master.module.browser.BrowserActivity;


@BaseDialog.FullScreen(height = false)
public class AgreementDialog extends BaseDialog<DialogAgreementBinding> {


    private AgreementAction agreementAction;

    public AgreementDialog(@NonNull Context context) {
        super(context, R.style.Theme_Common_Dialog);
        setCancelable(false);
        binding.setIsTwoStep(false);
        binding.setOnAgreeClickListener(v -> {
            dismiss();
            if (agreementAction != null) {
                agreementAction.onAgree();
            }
        });
        binding.setOnStepAgreeClickListener(v -> {
            dismiss();
            if (agreementAction != null) {
                agreementAction.onAgree();
            }
        });
        binding.setOnStepOneDisagreeClickListener(v -> {
            binding.setIsTwoStep(true);

        });
        binding.setOnStepTwoDisagreeClickListener(v -> {
            dismiss();
            if (agreementAction != null) {
                agreementAction.onDisagree();
            }
        });
        binding.tvOneContent.setMovementMethod(LinkMovementMethod.getInstance());
        binding.tvOneContent.setText(getAgreementContent(getContext().getString(R.string.agreement_one_step_content)));
    }

    public SpannableStringBuilder getAgreementContent(String agreeText) {
        Context context = getContext();
        String userTermsText = context.getString(R.string.user_terms_text);
        String privacyPolicyText = context.getString(R.string.privacy_policy_text);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(agreeText);
        int userTermsTextStart = agreeText.indexOf(userTermsText);
        int privacyPolicyTextStart = agreeText.indexOf(privacyPolicyText);

        ClickableSpan userTermsClickableSpan = new ClickableSpan() {

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(context.getResources().getColor(R.color.colorPrimary));
            }

            @Override
            public void onClick(@NonNull View widget) {
                BrowserActivity.start(widget.getContext(), Constants.USER_AGREEMENT);
            }
        };
        ClickableSpan privacyPolicyClickableSpan = new ClickableSpan() {

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(context.getResources().getColor(R.color.colorPrimary));
            }


            @Override
            public void onClick(@NonNull View widget) {
                BrowserActivity.start(widget.getContext(), Constants.PRIVACY_POLICY);
            }
        };
        spannableStringBuilder.setSpan(userTermsClickableSpan, userTermsTextStart,
                userTermsTextStart + userTermsText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(privacyPolicyClickableSpan, privacyPolicyTextStart,
                privacyPolicyTextStart + privacyPolicyText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    }


    public AgreementDialog setAgreementAction(AgreementAction agreementAction) {
        this.agreementAction = agreementAction;
        return this;
    }


    public interface AgreementAction {
        void onAgree();

        void onDisagree();
    }
}
