package com.zxhl.cms.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.badoo.mobile.util.WeakHandler;
import com.zxhl.cms.R;

/**
 * Created by gaoleichao on 2018/5/5.
 */

public class LoadingDialog extends Dialog {
    private Context mContext;
    private LayoutInflater inflater;
    private WindowManager.LayoutParams lp;
    private TextView mTextTv;
    private ProgressBar mProgressBar;
    private ImageView mSucIv;
    private ImageView mFailIv;
    private WeakHandler mHandler = new WeakHandler();

    public static LoadingDialog getLoadingDialog(Context context, String loadingText) {
        return getLoadingDialog(context, loadingText, true, true);
    }

    public static LoadingDialog getLoadingDialog(Context context, String loadingText, boolean cancelable, boolean outeSiteCanceled) {
        LoadingDialog dialog = new LoadingDialog(context);
        if (!invalidString(loadingText)) {
            dialog.setLoadText(loadingText);
        }
        dialog.setCanceledOnTouchOutside(outeSiteCanceled);
        dialog.setCancelable(cancelable);
        return dialog;
    }

    public LoadingDialog(Context context) {
        super(context, R.style.confirm_dialog);
        this.mContext = context;
        View layout = LayoutInflater.from(context).inflate(R.layout.widget_loadingdialog, null);
        mTextTv = (TextView) layout.findViewById(R.id.loading_text);
        mProgressBar = (ProgressBar) layout.findViewById(R.id.loading_bar);
        mSucIv = (ImageView) layout.findViewById(R.id.img_success);
        mFailIv = (ImageView) layout.findViewById(R.id.img_fail);
        setContentView(layout);
    }

    public void setDimAmount(boolean b) {
        if (b) {
            getWindow().setAttributes(lp);
        }
    }

    public void setLoadText(final String content) {
        mTextTv.setVisibility(View.VISIBLE);

        if (!TextUtils.isEmpty(content)) {
            mTextTv.setText(content);
        }
    }

    public void setLoadText(int resId) {
        if (mTextTv.getVisibility() != View.VISIBLE) {
            mTextTv.setVisibility(View.VISIBLE);
        }
        mTextTv.setText(resId);
    }

    public void setLoading(String text) {
        setLoadText(text);
        mProgressBar.setVisibility(View.VISIBLE);
        mSucIv.setVisibility(View.GONE);
        mFailIv.setVisibility(View.GONE);
    }

    public void setResult(boolean isSucc, String text, int duration) {
        setResult(isSucc, text, duration, null);
    }

    public void setResult(boolean isSucc, String text, int duration, OnDissListener onDismissListener) {
        try {
            if (duration <= 0 && isShowing()) {
                dismiss();
            }
            mProgressBar.setVisibility(View.GONE);
            mSucIv.setVisibility(isSucc ? View.VISIBLE : View.GONE);
            mFailIv.setVisibility(isSucc ? View.GONE : View.VISIBLE);
            setLoadText(text);

            mHandler.postDelayed(() -> {
                try {
                    if (LoadingDialog.this != null) {
                        if (LoadingDialog.this.isShowing())
                            LoadingDialog.this.dismiss();
                        if (onDismissListener != null) {
                            onDismissListener.dissComplete();
                        }
                    }
                } catch (Exception e) {
                    if (onDismissListener != null) {
                        onDismissListener.dissComplete();
                    }
                }


            }, duration);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean invalidString(Object obj) {
        return obj == null || "".equals(obj.toString())
                || "null".equals(obj.toString());
    }

    public interface OnDissListener {
        void dissComplete();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeCallbacksAndMessages(null);
    }
}
