package cn.yyxx.commsdk.core.ui.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import cn.yyxx.commsdk.core.utils.ResourceUtil;

public class LoadingDialogUtils {

    public static LoadingDialogUtils mInstance;
    private AlertDialog dialog;


    private static boolean isLoading;

    private LoadingDialogUtils(){}

    public void show(Context context,String msg){
        if (null == context || ((Activity)context).isFinishing()) return;
        if (isLoading)return;
        dialog = new AlertDialog.Builder(context, ResourceUtil.getStyleId(context, "yyxx_comm_loading_dialog")).create();
        View view = LayoutInflater.from(context).inflate(ResourceUtil.getLayoutId(context, "yyxx_comm_loading_dialog"),null);
        TextView text = view.findViewById(ResourceUtil.getId(context, "id_tv_loadingmsg"));
        text.setText(msg);
        Window window = dialog.getWindow();
        if (window != null){
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        dialog.show();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);


    }

    public void show(Context context){
        if (null == context || ((Activity)context).isFinishing()) return;
        if (isLoading)return;
        dialog = new AlertDialog.Builder(context, ResourceUtil.getStyleId(context, "yyxx_comm_loading_dialog")).create();
        View view = LayoutInflater.from(context).inflate(ResourceUtil.getLayoutId(context, "yyxx_comm_loading_dialog"),null);
        TextView text = view.findViewById(ResourceUtil.getId(context, "id_tv_loadingmsg"));
        text.setVisibility(View.GONE);
        Window window = dialog.getWindow();
        if (window != null){
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setWindowAnimations(ResourceUtil.getAnimId(context,"hnyy_rotate_anim"));

        }
        dialog.show();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);


    }


    public void hide(){
        if (dialog != null){
            dialog.dismiss();
        }
    }
    public static boolean isLoading() {
        return isLoading;
    }






    /**
     * 单一实例
     *
     */
    public static LoadingDialogUtils getInstance() {
        if (mInstance == null) {
            synchronized (LoadingDialogUtils.class) {
                if (mInstance == null) {
                    mInstance = new LoadingDialogUtils();
                    return mInstance;
                }
            }
        }
        return mInstance;
    }
}
