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.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.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.beans.AddPhotoBean;
import com.tenlionsoft.baselib.core.retrofit_net.BaseUrlApi;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 评论dialog
 */
public class ButtomDialogCommentView 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;
    public EditText mEtComment;
    public ImageView mIvImg;
    public RelativeLayout mRlPhoto;
    private AddPhotoBean photoBean;
    private ImageView mMIvClose;
    private boolean mIsCommentImg;

    private ButtomDialogCommentView(Context context, boolean isCancelable, boolean isBackCancelable, int gravity, boolean isShow, boolean isShowOther, boolean isCommentImg) {
        super(context, R.style.Dialog_ChoseFile);
        this.context = context;
        this.gravity = gravity;
        this.iscancelable = isCancelable;
        this.isBackCancelable = isBackCancelable;
        this.mIsShow = isShow;
        this.mIsShowOther = isShowOther;
        this.mIsCommentImg = isCommentImg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_bottom_input, null);
        setContentView(view);
        mEtComment = view.findViewById(R.id.et_comment);
        TextView btnSend = view.findViewById(R.id.btn_send);
        ImageView mIvPic = view.findViewById(R.id.iv_pic);
        ImageView mIvGif = view.findViewById(R.id.iv_gif);
        ImageView mIvEmoji = view.findViewById(R.id.iv_emoji);
        TextView mTvCancel = view.findViewById(R.id.tv_cancel);
        mTvCancel.setOnClickListener(v -> this.dismiss());
        mRlPhoto = view.findViewById(R.id.rl_photo);
        if (mIsCommentImg) {
            mRlPhoto.setVisibility(View.VISIBLE);
        } else {
            mRlPhoto.setVisibility(View.GONE);
        }
        mIvImg = view.findViewById(R.id.iv_photo);
        mMIvClose = view.findViewById(R.id.iv_close);
        mIvImg.setOnClickListener(v -> {
            if (photoBean == null) {
                //添加图片
                choseType(1);
            } else {
                //浏览图片
                choseType(8);
            }
        });
        mMIvClose.setOnClickListener(v -> choseType(7));
        if (photoBean == null) {
            mMIvClose.setVisibility(View.GONE);
        } else {
            mMIvClose.setVisibility(View.VISIBLE);
        }

        btnSend.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(mEtComment.getText().toString().trim())) {
                choseType(4);
            } else {
                ToastUtils.show("请输入评论内容");
            }
        });
        mIvPic.setOnClickListener(v -> choseType(1));
        mIvGif.setOnClickListener(v -> choseType(2));
        mIvEmoji.setOnClickListener(v -> choseType(3));
        setCancelable(iscancelable);
        setCanceledOnTouchOutside(isBackCancelable);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.Dialog_ChoseFile);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        this.setOnShowListener(dialogInterface -> showSoftKeyBoardDialog(mEtComment));
    }

    public void setPhotoBean(AddPhotoBean bean) {
        this.photoBean = bean;
        mMIvClose.setVisibility(View.VISIBLE);
        Glide.with(context)
                .load(BaseUrlApi.BASE_IMG_URL + bean.getId())
                .into(mIvImg);
    }

    public void removePhotoBean() {
        this.photoBean = null;
        mMIvClose.setVisibility(View.GONE);
        Glide.with(context)
                .load(R.drawable.ic_add_photo)
                .into(mIvImg);
    }

    private void showSoftKeyBoardDialog(EditText editText) {
        if (editText != null) {
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            //请求获得焦点
            editText.requestFocus();
            //调用系统输入法
            InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(editText, 0);
        }
    }

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

    /**
     * 选择
     *
     * @param i
     */
    private void choseType(int i) {
        if (mListener != null) {
            switch (i) {
                case 1:
                    mListener.chosePhoto();
                    break;
                case 2:
                    mListener.choseGif();
                    break;
                case 3:
                    mListener.choseEmoji();
                    break;
                case 4:
                    mListener.changePwd(mEtComment.getText().toString().trim());
                    mEtComment.setText("");
                    this.dismiss();
                    break;
                case 5:
                    mListener.loginOut();
                    break;
                case 7:
                    mListener.removeImg();
                    break;
                case 8:
                    mListener.showImg(photoBean);
                    break;
            }
        }
//        this.dismiss();
    }

    public interface OnChoseListener {
        void chosePhoto();

        void choseGif();

        void choseEmoji();

        void loginOut();

        void changePwd(String content);

        void removeImg();

        void showImg(AddPhotoBean bean);

    }

    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 boolean isCommentImg;

        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 ButtomDialogCommentView build() {
            return new ButtomDialogCommentView(this.mContext, this.isCancelable, this.mIsBackable, this.mGravity, mIsShow, mIsShowOther, isCommentImg);
        }

        public DialogBuilder setIsShowPhoto(String isCommentImg) {
            if ("0".equals(isCommentImg)) {
                this.isCommentImg = false;
            } else {
                this.isCommentImg = true;
            }
            return this;
        }
    }
}