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

import com.bumptech.glide.Glide;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.retrofit_net.BaseUrlApi;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 显示二维码的弹窗
 */
public class QrDialog extends Dialog {

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

    private QrDialog(Context context, String qr) {
        super(context, R.style.dialog_center_alpha);
        this.context = context;
        this.mQr = qr;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_meeting_qr, null);
        setContentView(view);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        ImageView mIvQr = view.findViewById(R.id.iv_qr);
        Glide.with(context)
            .load(BaseUrlApi.BASE_IMG_URL + mQr)
            .into(mIvQr);
        view.findViewById(R.id.tv_confirm).setOnClickListener(v -> this.dismiss());
        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() {
        return true;
    }


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

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

        public DialogBuilder setQr(String qr) {
            this.mQr = qr;
            return this;
        }

        public QrDialog build() {
            return new QrDialog(mContext, mQr);
        }
    }
}