package com.rdiot.yx485.widget;

import static android.util.TypedValue.COMPLEX_UNIT_SP;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.rdiot.yx485.databinding.DialogCommonNewBinding;
import com.rdiot.yx485.util.StringUtils;


/**
 * @Description
 * @ClassName CustomDialog
 * @Author DOAING
 * @date 2020.11.13 17:25
 */
public class CustomDialog extends Dialog {
    private DialogCommonNewBinding mBinding;

    public static CustomDialog newInstance(Context context) {
        CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle("温馨提示");
        dialog.setConfirmText("确定", null);
        dialog.setCancelTxt("", null);
        return dialog;
    }

    private CustomDialog(Context context) {
        super(context);
        Window window = getWindow();
        if (window == null) {
            return;
        }

        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mBinding = DialogCommonNewBinding.inflate(LayoutInflater.from(context));
        setContentView(mBinding.getRoot());
        setCancelable(false);
    }


    public void setTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            return;
        }

        mBinding.textDialogTitle.setText(title);
    }

    public void setConfirmText(String confirmText, OnConfirmClickListener onConfirmClickListener) {
        if (TextUtils.isEmpty(confirmText)) {
            return;
        }

        mBinding.textDialogConfirm.setText(confirmText);
        mBinding.textDialogConfirm.setOnClickListener(v -> {
            dismiss();
            if (onConfirmClickListener != null) {
                onConfirmClickListener.onConfirm();
            }
        });
    }

    public void setCancelTxt(String cancelTxt, OnCancelClickListener onCancelClickListener) {
        if (TextUtils.isEmpty(cancelTxt)) {
            mBinding.textDialogCancel.setVisibility(View.GONE);
            return;
        }

        mBinding.textDialogCancel.setVisibility(View.VISIBLE);
        mBinding.textDialogCancel.setText(cancelTxt);
        mBinding.textDialogCancel.setOnClickListener(v -> {
            if (onCancelClickListener != null) {
                onCancelClickListener.onCancel();
            } else {
                dismiss();
            }
        });
    }

    public void setContent(String content) {
        mBinding.textDialogSubtitle.setText(StringUtils.checkEmpty(content));
    }

    public void setContentText(int size) {
        mBinding.textDialogSubtitle.setTextSize(COMPLEX_UNIT_SP, size);
        mBinding.textDialogSubtitle.setGravity(Gravity.START);
    }

    public void setContent(SpannableStringBuilder content) {
        if (content != null) {
            mBinding.textDialogSubtitle.setMovementMethod(LinkMovementMethod.getInstance());
            mBinding.textDialogSubtitle.setText(content);
        }
    }

    public interface OnConfirmClickListener {
        /**
         * 点击确定按钮
         */
        void onConfirm();
    }

    public interface OnCancelClickListener {
        /**
         * 点击取消按钮
         */
        void onCancel();
    }
}
