package com.tenlionsoft.baselib.core.widget.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.view.TimePickerView;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 基础的提示信息dialog
 */
public class BaseMsgDialog extends Dialog {

    private boolean iscancelable;
    private boolean isBackCancelable;
    private View view;
    private Context context;
    private int gravity;
    private OnChoseListener mListener;
    private boolean mIsShow;
    private boolean mIsShowOther;
    private TimePickerView mTimePicker;
    private EditText mEtContent;
    private String mNoticeWay = "NOTICE";
    private String mTitle = "提示";
    private String mMsg = "提示内容";

    private BaseMsgDialog(Context context, boolean isCancelable, boolean isBackCancelable, int gravity, String title, String msg, OnChoseListener mListener) {
        super(context, R.style.dialog_center_alpha);
        this.context = context;
        this.gravity = gravity;
        this.iscancelable = isCancelable;
        this.isBackCancelable = isBackCancelable;
        this.mTitle = title;
        this.mMsg = msg;
        this.mListener = mListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_msg_hint, null);
        setContentView(view);
        setCancelable(iscancelable);
        setCanceledOnTouchOutside(isBackCancelable);
        mEtContent = view.findViewById(R.id.et_content);
        view.findViewById(R.id.btn_cancel).setOnClickListener(v -> mListener.doCancel(this));
        view.findViewById(R.id.btn_confirm).setOnClickListener(v -> mListener.doConfirm(this));
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvMsg = view.findViewById(R.id.tv_msg);
        tvTitle.setText(mTitle);
        tvMsg.setText(mMsg);

        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.dialog_center_alpha);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (displayMetrics.widthPixels * 0.5);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    public void HideSoftKeyBoardDialog(Activity activity) {
        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        } catch (Exception ex) {


        }
    }


    /**
     * 校验参数
     */
    private boolean checkParams() {
        String content = mEtContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.show("请输入督办内容");
            return false;
        }
        return true;
    }


    public void addOnChoseListener(OnChoseListener listener) {
        this.mListener = listener;
    }


    public interface OnChoseListener {
        void doConfirm(BaseMsgDialog dialog);

        void doCancel(BaseMsgDialog dialog);
    }

    public static class DialogBuilder {
        private Context mContext;
        private boolean isCancelable;
        private boolean mIsBackable;
        private int mGravity = Gravity.BOTTOM;
        private OnChoseListener mListener;
        private boolean mIsShowOther;
        private String title;
        private String msg;

        public DialogBuilder(Context context) {
            this.mContext = context;
        }

        public DialogBuilder setIscancelable(boolean iscancelable) {
            this.isCancelable = iscancelable;
            return this;
        }

        public DialogBuilder setIsBackCancelable(boolean isBackCancelable) {
            this.mIsBackable = isBackCancelable;
            return this;
        }

        public DialogBuilder setShowLocation(int gravity) {
            this.mGravity = gravity;
            return this;
        }

        private boolean mIsShow;

        public DialogBuilder setIsShowFile(boolean isShowFile) {
            mIsShow = isShowFile;
            return this;
        }

        public DialogBuilder setIsShowOther(boolean isShow) {
            mIsShowOther = isShow;
            return this;
        }

        public DialogBuilder setTitle(String title) {
            this.title = title;
            return this;
        }

        public DialogBuilder setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public DialogBuilder addListener(OnChoseListener listener) {
            this.mListener = listener;
            return this;
        }


        public BaseMsgDialog build() {
            return new BaseMsgDialog(this.mContext, this.isCancelable, this.mIsBackable, this.mGravity, this.title, this.msg, this.mListener);
        }
    }
}