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

import androidx.appcompat.widget.AppCompatRatingBar;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 评价弹窗
 */
public class CorrectionDialog extends Dialog {

    private View view;
    private Context context;
    private OnChangeListener mChangeListener;
    private EditText mEtContent;
    private float level = -1;

    private CorrectionDialog(Context context) {
        super(context, R.style.dialog_center_alpha);
        this.context = context;
    }

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

        mEtContent = view.findViewById(R.id.et_content);
        view.findViewById(R.id.tv_cancel).setOnClickListener(v -> this.dismiss());
        view.findViewById(R.id.tv_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() {
        if (checkParams()) {
            if (mChangeListener != null) {
                String content = mEtContent.getText().toString().trim();
                mChangeListener.doChange(content, level);
            }
        }
    }

    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) {


        }
    }

    public void addOnChangeListener(OnChangeListener listener) {
        this.mChangeListener = listener;
    }

    public interface OnChangeListener {
        void doChange(String org, float level);
    }

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


    public static class DialogBuilder {
        private Context mContext;

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


        public CorrectionDialog build() {
            return new CorrectionDialog(mContext);
        }
    }
}