package com.rc.core.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

public abstract class BaseFragment extends Fragment {

    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    protected BaseFragmentHelp baseFragmentHelp;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return setViewBinding(inflater,container).getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        baseFragmentHelp = new BaseFragmentHelp(this);
        onInit();
    }

    protected abstract ViewBinding setViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container);

    protected abstract void onInit();

    protected void showLoading(String msg) {
        if (null != baseFragmentHelp) {
            baseFragmentHelp.showLoading(msg);
        }
    }

    protected void dismissLoading() {
        if (null != baseFragmentHelp) {
            baseFragmentHelp.dismissLoading();
        }

    }

    protected void showToast(String msg) {
        if (null != baseFragmentHelp) {
            baseFragmentHelp.showToast(msg);
        }

    }

    protected void showNetError(Throwable throwable) {
        if (null != baseFragmentHelp) {
            baseFragmentHelp.showNetError(throwable);
        }

    }

    protected void addDisposable(Disposable disposable) {
        if (null != baseFragmentHelp) {
            baseFragmentHelp.addDisposable(disposable);
        }

    }

    protected void delDisposable(Disposable disposable) {
        if (null != baseFragmentHelp) {
            baseFragmentHelp.delDisposable(disposable);
        }

    }

    @Override
    public void onDestroyView() {
        if (null != baseFragmentHelp) {
            baseFragmentHelp.onDestroy();
            baseFragmentHelp = null;
        }
        super.onDestroyView();
    }
}
