package com.webclip.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.webclip.base.databinding.DialogActionConfirmBinding;


/**
 * 通用弹窗
 */
public class ActionConfirmDialog extends Dialog {
    DialogActionConfirmBinding dialogActionConfirmBinding;

    String content;
    String cancel = null;
    String sure = null;
    boolean showCancel = true;
    OnToActionListener onToActionListener;

    public interface OnToActionListener {
        void toSumbit();
        void toCancel();

    }

    public void setOnToActionListener(OnToActionListener onNextCallListener) {
        this.onToActionListener = onNextCallListener;
    }


    public ActionConfirmDialog(Context context, String content,boolean showCancel) {
        super(context, R.style.MaterialDesignDialog);
        this.content = content;
        this.showCancel = showCancel;
    }
    public ActionConfirmDialog(Context context, String content, String cancel, String sure) {
        super(context, R.style.MaterialDesignDialog);
        this.content = content;
        this.cancel = cancel;
        this.sure = sure;
    }

    public ActionConfirmDialog(Context context, String content, String cancel, String sure,boolean showCancel) {
        super(context, R.style.MaterialDesignDialog);
        this.content = content;
        this.cancel = cancel;
        this.sure = sure;
        this.showCancel = showCancel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialogActionConfirmBinding = DialogActionConfirmBinding.inflate(getLayoutInflater());
        setContentView(dialogActionConfirmBinding.getRoot());
        dialogActionConfirmBinding.contentTv.setText(content);
        dialogActionConfirmBinding.contentTv.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                //这个监听的回调是异步的，在监听完以后一定要把绘制监听移除，不然这个会一直回调，导致界面错乱
                dialogActionConfirmBinding.contentTv.getViewTreeObserver().removeOnPreDrawListener(this);
                int line = dialogActionConfirmBinding.contentTv.getLineCount();
                if(line>1){
                    dialogActionConfirmBinding.contentTv.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
                }

                return true;
            }
        });
        if(!TextUtils.isEmpty(cancel)){
            dialogActionConfirmBinding.cancelTv.setText(cancel);
        }
        if(!TextUtils.isEmpty(sure)){
            dialogActionConfirmBinding.sumbitTv.setText(sure);
        }
        if(!showCancel){
            dialogActionConfirmBinding.cancelTv.setVisibility(View.GONE);
            dialogActionConfirmBinding.lineV.setVisibility(View.GONE);
        }

        dialogActionConfirmBinding.sumbitTv.setOnClickListener(v -> {
            dismiss();
            if(onToActionListener!=null){
                onToActionListener.toSumbit();
            }
        });
        dialogActionConfirmBinding.cancelTv.setOnClickListener(v -> {
            dismiss();
            if(onToActionListener!=null){
                onToActionListener.toCancel();
            }
        });

        Window window = getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.CENTER;
        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setAttributes(wlp);
    }

}
