package com.hainaos.vc.dialog;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.hainaos.vc.R;
import com.hainaos.vc.activity.privacy.PrivacyActivity;

public class PrivacyPolicyDialog extends AlertDialog {
    private Context mContext;
    private TextView tv_content;
    private TextView tv_cancel;
    private TextView tv_confirm;

    public PrivacyPolicyDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
    }

    public PrivacyPolicyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    public PrivacyPolicyDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public void setPrivacyPolicyCallback(PrivacyPolicyCallback callback) {
        this.privacyPolicyCallback = callback;
    }

    private PrivacyPolicyCallback privacyPolicyCallback;

    public interface PrivacyPolicyCallback {
        void onCancel();

        void onConfirm();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_privacy_policy);
        tv_content = findViewById(R.id.tv_content);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_confirm = findViewById(R.id.tv_confirm);
        SpannableString spannableString = new SpannableString(mContext.getResources().getString(R.string.privacy_agreement_content));
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(mContext, PrivacyActivity.class);
                mContext.startActivity(intent);
            }
        }, 84, 90, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 84, 90, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv_content.setText(spannableString);
        tv_content.setMovementMethod(LinkMovementMethod.getInstance());

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (privacyPolicyCallback != null) {
                    privacyPolicyCallback.onCancel();
                }
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (privacyPolicyCallback != null) {
                    privacyPolicyCallback.onConfirm();
                }
            }
        });
    }
}
