package com.wisewoods.base.base;

import android.content.Context;
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 retrofit2.Call;

public abstract class BaseFragment<VB extends ViewBinding> extends Fragment {

    protected BaseFragment<VB> fragment;
    protected BaseActivity activity;
    protected VB binding;
    private BaseLogic<VB> logic;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (BaseActivity) context;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragment = this;
        logic = new BaseLogic<>(this);
        logic.init();
        View view = logic.initData(inflater, container, savedInstanceState);
        binding = logic.getBinding();
        if (view == null) {
            view = super.onCreateView(inflater, container, savedInstanceState);
        }
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData(savedInstanceState);
    }

    protected <T> Call<T> addCall(Call<T> call) {
        return logic.addCall(call);
    }

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

    protected boolean hasEvent() {
        return false;
    }


    protected abstract void initData(Bundle savedInstanceState);

    protected Object getLayout() {
        return logic.getLayout();
    }

    protected void showLoading() {
        if (logic != null) {
            logic.showLoading();
        }
    }

    protected void hideLoading() {
        if (logic != null) {
            logic.hideLoading();
        }
    }

    protected boolean isFastClick() {
        if (logic != null) {
            return logic.isFastClick();
        }
        return true;
    }
}
