package com.mayi.fastdevelop.base;

import android.app.Dialog;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.mayi.fastdevelop.R;
import com.mayi.fastdevelop.util.LogUtils;
import com.mayi.fastdevelop.util.ToastUtil;
import com.mayi.fastdevelop.view.LoadingDialog;

public class BaseDialogFragment extends DialogFragment {

    private String TAG = getClass().getSimpleName();

    public void logI(String msg) {
        LogUtils.i(TAG + "  " + msg);
    }

    public void showToast(String msg) {
        ToastUtil.show(getContext(), msg);
    }

    private Dialog dialog;

    public void showLoading() {
        if (dialog == null) {
            dialog = LoadingDialog.init(getContext()).setCanTouchOutsideCancel(true).show();
        } else {
            dialog.setCanceledOnTouchOutside(true);
            dialog.setCancelable(true);
            dialog.show();
        }
    }

    public void showLoading(boolean canTouchOutsideCancel) {
        if (dialog == null) {
            dialog = LoadingDialog.init(getContext()).setCanTouchOutsideCancel(canTouchOutsideCancel).show();
        } else {
            dialog.setCanceledOnTouchOutside(canTouchOutsideCancel);
            dialog.setCancelable(canTouchOutsideCancel);
            dialog.show();
        }
    }

    public void dismssLoading() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        dismssLoading();
        super.onDestroyView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.Dialog_FullScreen);
    }

}
