package com.cooleshow.base.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cooleshow.base.BuildConfig;
import com.cooleshow.base.R;
import com.cooleshow.base.common.BaseConstant;
import com.cooleshow.base.utils.LogUtils;

import androidx.annotation.NonNull;

/**
 * Author by pq, Date on 2022/7/5.
 */
public class ImgVerifyCodeDialog extends Dialog {

    private ImageView mIvClose;
    private ImageView mIvIcon;
    private TextView mTvRefresh;
    private EditText etCode;
    private String currentPhone;
    private TextWatcher mTextWatcher;
    private OnEventListener mListener;

    public ImgVerifyCodeDialog(@NonNull Context context) {
        super(context, R.style.DialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_img_verify_code_layout);
        mIvClose = findViewById(R.id.iv_close);
        mIvIcon = findViewById(R.id.iv_code);
        mTvRefresh = findViewById(R.id.tv_refresh);
        etCode = findViewById(R.id.et_code);
        mIvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadImg();
            }
        });
        mTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (mListener != null) {
                    if (editable.length() == 4) {
                        mListener.onVerifyImgCode(currentPhone, etCode.getText().toString().trim());
                    }
                }

            }
        };
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                release();
            }
        });
    }

    public void setPhone(String phone) {
        if (mIvIcon != null) {
            this.currentPhone = phone;
            loadImg();
        }
    }


    private void loadImg() {
        RequestOptions options = new RequestOptions()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        String imgUrl = BaseConstant.getImgCodeApiUrl() + currentPhone;
        LogUtils.i("pq", "imgUrl:" + imgUrl);
        Glide.with(getContext()).load(imgUrl).apply(options).into(mIvIcon);
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.mListener = onEventListener;
    }

    public interface OnEventListener {
        void onVerifyImgCode(String phone, String imgCode);
    }

    public void release() {
        if (mTextWatcher != null && etCode != null) {
            etCode.removeTextChangedListener(mTextWatcher);
        }
    }

    @Override
    public void show() {
        super.show();
        if (etCode != null && mTextWatcher != null) {
            etCode.addTextChangedListener(mTextWatcher);
        }
    }
}
