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.RatingBar;

import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.utils.RegexUtils;

import androidx.appcompat.widget.AppCompatRatingBar;
import androidx.appcompat.widget.SwitchCompat;


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

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

    private AppraiseDialog(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_appraise, null);
        setContentView(view);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        mEtContent = view.findViewById(R.id.et_content);
        AppCompatRatingBar arbStar = view.findViewById(R.id.arb_star);
        arbStar.setOnRatingBarChangeListener((ratingBar, rating, fromUser) -> level = rating);
        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;
        }
        if (level == -1) {
            ToastUtils.show("请选择评价分数");
            return false;
        }
        return true;
    }


    public static class DialogBuilder {
        private Context mContext;

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


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