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

import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.TouchDelegate;
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.TextView;

import com.tenlionsoft.baselib.R;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 评论dialog
 */
public class CenterDialogUpdateView extends Dialog {

    private View view;
    private Context context;
    private OnChoseListener mListener;
    public ImageView mIvImg;
    private String content;

    private CenterDialogUpdateView(Context context, String content) {
        super(context, R.style.dialog_center);
        this.context = context;
        this.content = content;
    }

    /**
     * 将view的点击区域放大
     *
     * @param view             需要放大的view
     * @param expandTouchWidth 放大的宽度
     */
    public void setTouchDelegate(final View view, final int expandTouchWidth) {
        final View parentView = (View) view.getParent();
        parentView.post(() -> {
            final Rect rect = new Rect();
            view.getHitRect(rect);
            rect.top -= expandTouchWidth;
            rect.bottom += expandTouchWidth;
            rect.left -= expandTouchWidth;
            rect.right += expandTouchWidth;
            TouchDelegate touchDelegate = new TouchDelegate(rect, view);
            parentView.setTouchDelegate(touchDelegate);
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_center_update, null);
        setContentView(view);
        TextView mTvContent = view.findViewById(R.id.tv_content);
        mTvContent.setText(content);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        ImageView mIvCancel = view.findViewById(R.id.iv_cancel);
        mIvCancel.setOnClickListener(v -> this.dismiss());
        setTouchDelegate(mIvCancel, 15);
        TypeFaceTextView mTvUpdate = view.findViewById(R.id.tv_update);
        mTvUpdate.setOnClickListener(v -> choseType(1));
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Dialog_ChoseFile);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (dm.widthPixels * 0.7);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }


    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.doUpdate();
                    break;
            }
        }
        this.dismiss();
    }

    public interface OnChoseListener {
        void doUpdate();
    }

    public static class DialogBuilder {
        private Context mContext;
        private String content;

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

        public CenterDialogUpdateView build() {
            return new CenterDialogUpdateView(this.mContext, this.content);
        }

        public DialogBuilder setContent(String content) {
            this.content = content;
            return this;
        }
    }
}