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

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.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 选择与输入
 */
public class CenterTextAndInputView extends Dialog {

    private boolean iscancelable;
    private boolean isBackCancelable;
    private View view;
    private Context context;
    private int gravity;
    private OnChoseListener mListener;
    private OnTvChooseListener mTvChooseListener;
    private boolean mIsShow;
    private boolean mIsShowOther;
    private EditText mEtContent;
    private TextView mTvTitle;
    private TextView mTvNameTitle;
    private TextView mTvNameDesc;
    private TextView mTvContentTitle;

    private String mNameDescHint = "请选择";
    private String mNameTitle = "名称";
    private String mContentDescHint = "标题";
    private String mTitle = "标题";
    private String mSelId;

    private CenterTextAndInputView(Context context, String title, String nameTitle, String nameDescHint, String contentTitle) {
        super(context, R.style.dialog_center_alpha);
        this.context = context;
        this.mNameTitle = nameTitle;
        this.mNameDescHint = nameDescHint;
        this.mContentDescHint = contentTitle;
        this.mTitle = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_input_text, null);
        setContentView(view);
        setCancelable(iscancelable);
        setCanceledOnTouchOutside(isBackCancelable);

        mTvTitle = view.findViewById(R.id.tv_title);
        mTvNameTitle = view.findViewById(R.id.tv_name_title);
        mTvNameDesc = view.findViewById(R.id.tv_name_desc);
        mTvContentTitle = view.findViewById(R.id.tv_content_title);
        mEtContent = view.findViewById(R.id.et_content);
        mTvTitle.setText(mTitle);
        mTvNameTitle.setText(mNameTitle);
        mTvNameDesc.setHint("请选择" + mNameTitle);
        mTvNameDesc.setOnClickListener(v -> mTvChooseListener.onChoose());
        mTvContentTitle.setText(mContentDescHint);
        mEtContent.setHint("请输入" + mContentDescHint);
        view.findViewById(R.id.btn_cancel).setOnClickListener(v -> this.dismiss());
        view.findViewById(R.id.btn_confirm).setOnClickListener(v -> doConfirm());
        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.9);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    private void doConfirm() {
        hideSoftKeyBoardDialog(context);
        if (checkParams()) {
            String content = mEtContent.getText().toString().trim();
            String name = mTvNameDesc.getText().toString().trim();
            mListener.doConfirm(mSelId, name, content);
        }
    }

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


        }
    }

    /**
     * 设置选择的id
     */
    public void setSelId(String selId) {
        this.mSelId = selId;
    }

    public void setSelText(String text) {
        mTvNameDesc.setText(text);
    }

    /**
     * 校验参数
     */
    private boolean checkParams() {
        if (TextUtils.isEmpty(mSelId)) {
            ToastUtils.show("请选择" + mNameTitle);
            return false;
        }
        String content = mEtContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.show("请输入" + mContentDescHint);
            return false;
        }
        return true;
    }

    public void addOnChooseListener(OnTvChooseListener listener) {
        this.mTvChooseListener = listener;
    }

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

    /**
     * 选择
     *
     * @param i
     */
    private void choseType(int i) {
        this.dismiss();
    }

    public interface OnChoseListener {
        void doConfirm(String id, String text, String content);
    }


    public interface OnTvChooseListener {
        void onChoose();
    }

    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 mNameDescHint = "请选择";
        private String mNameTitle = "名称";
        private String mContentDescHint = "请输入";
        private String mTitle = "标题";


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

        public DialogBuilder setNameDescHint(String desc) {
            this.mNameDescHint = desc;
            return this;
        }

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

        public DialogBuilder setContentHint(String hint) {
            this.mContentDescHint = hint;
            return this;
        }

        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.mTitle = title;
            return this;
        }

        public CenterTextAndInputView build() {
            return new CenterTextAndInputView(this.mContext, this.mTitle, this.mNameTitle, this.mNameDescHint, this.mContentDescHint);
        }
    }
}