package com.cooleshow.base.widgets;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

import com.cooleshow.base.R;
import com.cooleshow.base.databinding.CommonDialogTipLayoutBinding;
import com.cooleshow.base.utils.KeyboardUtils;
import com.cooleshow.base.utils.ToastUtils;

/**
 * @author dengqw
 * @time 2022/6/28 21:11
 * @describe：
 **/
public class CommonTipDialog extends Dialog {
    public static final int TYPE_SET_TEACHER_COMMENT = 0;//老师评价
    public static final int TYPE_SET_HOMEWORK = 1;//布置作业
    public static final int TYPE_SET_HOMEWORK_COMMENT = 2;//作业点评

    private int showType = TYPE_SET_TEACHER_COMMENT;

    private CommonDialogTipLayoutBinding mViewBinding;
    private static CommonTipDialog dialog;

    public CommonTipDialog(@NonNull Context context) {
        super(context, R.style.DialogStyle);
        mViewBinding = CommonDialogTipLayoutBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mViewBinding.ivClose.setOnClickListener(v -> {
            dismiss();
        });
        setOnDismissListener(dialog -> unbind());

    }

    public void unbind() {
        if (mViewBinding != null) {
            mViewBinding = null;
        }
    }

    public static CommonTipDialog getInstance(Context context) {
        dialog = new CommonTipDialog(context);
        return dialog;
    }

    public CommonTipDialog setTitle(String titleText) {
        mViewBinding.tvTitle.setText(titleText);
        return this;
    }

    public CommonTipDialog setContent(String content) {
        mViewBinding.contentText.setText(content);
        return this;
    }

    public CommonTipDialog setCancel(String cancel, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(cancel)) {
            mViewBinding.tvCancel.setVisibility(View.VISIBLE);
        }
        mViewBinding.tvCancel.setText(cancel);
        mViewBinding.tvCancel.setOnClickListener(listener);
        return this;
    }

    public CommonTipDialog setConfirm(String confirm, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(confirm)) {
            mViewBinding.tvSure.setVisibility(View.VISIBLE);
        }
        mViewBinding.tvSure.setText(confirm);
        mViewBinding.tvSure.setOnClickListener(listener);
        return this;
    }

    public static void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
