package com.mq.mylibrary.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mq.mylibrary.R;


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

    AlertDialog mDialog;

    private TextView mTvTitle;
    private ImageView mBtnClose;
    private TextView mTvMsg;
    private FrameLayout mContentLayout;
    private View mViewBottomLine;
    private TextView mBtnLeft;
    private TextView mBtnCenter;
    private TextView mBtnRight;
    private LinearLayout mRootLayout;

    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();
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_system, null, false);
        mDialog.setView(view);
        initView(view);
    }

    private void initView(View view) {
        mTvTitle = view.findViewById(R.id.tv_title);
        mTvTitle.setText("");

        mBtnClose = view.findViewById(R.id.btn_close);
        mBtnClose.setOnClickListener(mClickListener);

        mTvMsg = view.findViewById(R.id.tv_msg);
        mTvMsg.setVisibility(View.GONE);

        mContentLayout = view.findViewById(R.id.content_layout);
        mContentLayout.setVisibility(View.GONE);

        mViewBottomLine = view.findViewById(R.id.view_bottom_line);
        mViewBottomLine.setVisibility(View.GONE);


        mBtnLeft = view.findViewById(R.id.btn_left);
        mBtnLeft.setOnClickListener(mClickListener);
        mBtnLeft.setVisibility(View.GONE);

        mBtnCenter = view.findViewById(R.id.btn_center);
        mBtnCenter.setOnClickListener(mClickListener);
        mBtnCenter.setVisibility(View.GONE);

        mBtnRight = view.findViewById(R.id.btn_right);
        mBtnRight.setOnClickListener(mClickListener);
        mBtnRight.setVisibility(View.GONE);

        mRootLayout = view.findViewById(R.id.root_layout);
        mRootLayout.setOnClickListener(mClickListener);
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();

            //关闭
            if (id == R.id.btn_close) {
                dismiss();
                return;
            }
            //左侧按钮点击
            if (id == R.id.btn_left) {
                if (mLeftCallBack == null)
                    dismiss();
                else
                    mLeftCallBack.onLeftListener(SystemDialog.this);
                return;
            }
            //中间按钮点击
            if (id == R.id.btn_center) {
                if (mCenterCallBack == null)
                    dismiss();
                else
                    mCenterCallBack.onCenterListener(SystemDialog.this);
                return;
            }
            //右侧按钮点击
            if (id == R.id.btn_right) {
                if (mRightCallBack == null)
                    dismiss();
                else
                    mRightCallBack.onRightListener(SystemDialog.this);
                return;
            }
            //空白处点击
            if (id == R.id.root_layout) {
//                    if (mBtnClose.getVisibility() == View.VISIBLE) {
//                        dismiss();
//                    }
                return;
            }
        }
    };

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

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

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

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

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

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

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

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

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

    public SystemDialog setDismissListener(DialogInterface.OnDismissListener dismissListener) {
        mDialog.setOnDismissListener(dismissListener);
        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);
    }
}
