package com.unis.sxzcl.widget.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import androidx.appcompat.app.AlertDialog;

import com.unis.sxzcl.R;
import com.unis.sxzcl.databinding.DialogSystemBinding;


/**
 * 创建人 mQ
 * 创建时间 2019/7/23 0023 20:22
 * 文件名称 SystemDialog
 * 说明:  系统提示对话框
 **/
public class SystemDialog {

    AlertDialog mDialog;
    private DialogSystemBinding mBinding;

    private LeftCallBack mLeftCallBack;
    private CenterCallBack mCenterCallBack;
    private RightCallBack mRightCallBack;


    public SystemDialog(Context context) {
        initDialog(context);
    }


    private void initDialog(Context context) {
        mDialog = new AlertDialog.Builder(context, R.style.MyDialog).create();
        mBinding = DialogSystemBinding.inflate(LayoutInflater.from(context));
        mDialog.setView(mBinding.getRoot());
        mDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        initView();
    }

    private void initView() {
        mBinding.tvTitle.setText("");

        mBinding.btnClose.setOnClickListener(mClickListener);

        mBinding.tvMsg.setVisibility(View.GONE);

        mBinding.contentLayout.setVisibility(View.GONE);

        mBinding.viewBottomLine.setVisibility(View.GONE);

        mBinding.btnLeft.setOnClickListener(mClickListener);
        mBinding.btnLeft.setVisibility(View.GONE);

        mBinding.btnCenter.setOnClickListener(mClickListener);
        mBinding.btnCenter.setVisibility(View.GONE);

        mBinding.btnRight.setOnClickListener(mClickListener);
        mBinding.btnRight.setVisibility(View.GONE);

        mBinding.rootLayout.setOnClickListener(mClickListener);
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_close:
                    //关闭
                    dismiss();
                    break;
                case R.id.btn_left:
                    //左侧按钮点击
                    if (mLeftCallBack == null)
                        dismiss();
                    else
                        mLeftCallBack.onLeftListener(SystemDialog.this);
                    break;
                case R.id.btn_center:
                    //中间按钮点击
                    if (mCenterCallBack == null)
                        dismiss();
                    else
                        mCenterCallBack.onCenterListener(SystemDialog.this);
                    break;
                case R.id.btn_right:
                    //右侧按钮点击
                    if (mRightCallBack == null)
                        dismiss();
                    else
                        mRightCallBack.onRightListener(SystemDialog.this);
                    break;
                case R.id.root_layout:
                    //空白处点击
//                    if (mBtnClose.getVisibility() == View.VISIBLE) {
//                        dismiss();
//                    }
                    break;
            }
        }
    };

    /**
     * 设置标题
     *
     * @param title
     * @return
     */
    public SystemDialog setTitle(String title) {
        mBinding.tvTitle.setText(title);
        return this;
    }

    /**
     * 设置内容
     *
     * @param msg
     */
    public SystemDialog setMsg(String msg) {
        mBinding.tvMsg.setVisibility(View.VISIBLE);
        mBinding.tvMsg.setText(msg);
        return this;
    }

    /**
     * 设置左侧按钮
     *
     * @param left
     * @param leftCallBack
     * @return
     */
    public SystemDialog setLeftButton(String left, LeftCallBack leftCallBack) {
        mBinding.viewBottomLine.setVisibility(View.VISIBLE);
        mBinding.btnLeft.setVisibility(View.VISIBLE);
        mBinding.btnLeft.setText(left);
        mLeftCallBack = leftCallBack;
        return this;
    }

    /**
     * 设置中间按钮
     *
     * @param center
     * @param centerCallBack
     * @return
     */
    public SystemDialog setCenterButton(String center, CenterCallBack centerCallBack) {
        mBinding.btnCenter.setVisibility(View.VISIBLE);
        mBinding.btnCenter.setText(center);
        mCenterCallBack = centerCallBack;
        return this;
    }

    /**
     * 设置右侧按钮
     *
     * @param right
     * @param rightCallBack
     * @return
     */
    public SystemDialog setRightButton(String right, RightCallBack rightCallBack) {
        mBinding.btnRight.setVisibility(View.VISIBLE);
        mBinding.btnRight.setText(right);
        mRightCallBack = rightCallBack;
        return this;
    }

    /**
     * 设置内容布局
     *
     * @param contentView
     * @return
     */
    public SystemDialog setContentView(View contentView) {
        mBinding.contentLayout.setVisibility(View.VISIBLE);
        mBinding.contentLayout.addView(contentView);
        return this;
    }

    public SystemDialog setCancelable(boolean cancelable) {
        mDialog.setCancelable(cancelable);
        return this;
    }

    public SystemDialog setHideClose() {
        mBinding.btnClose.setVisibility(View.GONE);
        return this;
    }

    public SystemDialog setHideTitle() {
        mBinding.tvTitle.setVisibility(View.GONE);
        return this;
    }

    public SystemDialog setDismissListener(DialogInterface.OnDismissListener dismissListener) {
        mDialog.setOnDismissListener(dismissListener);
        return this;
    }

    public SystemDialog setOnShowListener(DialogInterface.OnShowListener listener) {
        mDialog.setOnShowListener(listener);
        return this;
    }

    public void show() {
        mDialog.show();
    }


    public void dismiss() {
        mDialog.dismiss();
    }

    public AlertDialog getDialog() {
        return mDialog;
    }

    public interface LeftCallBack {
        public void onLeftListener(SystemDialog dialog);
    }

    public interface CenterCallBack {
        public void onCenterListener(SystemDialog dialog);
    }

    public interface RightCallBack {
        public void onRightListener(SystemDialog dialog);
    }
}
