package it.integry.integrywmsnative.core.expansion;

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 java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.interfaces.IPoppableActivity;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.ui.ElevatedToolbar;
import it.integry.integrywmsnative.view.dialogs.DialogProgressView;

public abstract class BaseFragment extends Fragment {

    @Inject
    public DialogProgressView mCurrentProgress;

    protected ElevatedToolbar mToolbar;

    protected final List<Runnable> mOnPreDestroyList = new ArrayList<>();
    private boolean progressOpened;


    public void setScrollToolbar(ElevatedToolbar toolbar) {
        mToolbar = toolbar;
    }

    public void addOnPreDestroy(Runnable onPreDestroy) {
        this.mOnPreDestroyList.add(onPreDestroy);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        MainApplication.appComponent
                .baseFragmentComponent()
                .create()
                .inject(this);

        return view;
    }

    public void onLoadingStarted() {
        this.openProgress();
    }

    public void onLoadingEnded() {
        this.closeProgress();
    }

    private void openProgress() {
        BarcodeManager.disable();
        if (!progressOpened && !this.mCurrentProgress.isAdded()) {
            this.progressOpened = true;
            requireActivity().runOnUiThread(() -> {
                this.mCurrentProgress.show(requireActivity().getSupportFragmentManager(), "tag");
            });
        }
    }

    private void closeProgress() {
        BarcodeManager.enable();
        if (progressOpened) {
            this.progressOpened = false;
            requireActivity().runOnUiThread(() -> {
                mCurrentProgress.dismiss();
            });
        }
    }

    public void onError(Exception ex) {
        requireActivity().runOnUiThread(() -> {
            this.closeProgress();
            UtilityExceptions.defaultException(getActivity(), ex);
            BarcodeManager.enable();
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        for (Runnable onPreDestroy : mOnPreDestroyList) {
            onPreDestroy.run();
        }
    }

    public boolean onBackPressed() {
        return false;
    }

    protected void popMe() {
        if (requireActivity() instanceof IPoppableActivity) {
            requireActivity().runOnUiThread(() -> {
                ((IPoppableActivity) requireActivity()).pop();
            });
        } else {
            requireActivity().getSupportFragmentManager().popBackStack();
        }
    }
}
