package de.sebse.fuplanner.fragments.moddetails;


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.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import de.sebse.fuplanner.MainActivity;
import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.KVV.KVV;
import de.sebse.fuplanner.services.KVV.types.Modules;
import de.sebse.fuplanner.tools.logging.Logger;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ModDetailGradebookFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ModDetailGradebookFragment extends Fragment {
    private static final String ARG_POSITION = "itemPosition";

    private String mItemPos;
    private final Logger log = new Logger(this);
    private ModDetailGradebookAdapter adapter;
    private SwipeRefreshLayout swipeLayout;


    public ModDetailGradebookFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param itemPosition Item position in module list.
     * @return A new instance of fragment ModDetailAnnounceFragment.
     */
    public static ModDetailGradebookFragment newInstance(String itemPosition) {
        ModDetailGradebookFragment fragment = new ModDetailGradebookFragment();
        Bundle args = new Bundle();
        args.putString(ARG_POSITION, itemPosition);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mItemPos = getArguments().getString(ARG_POSITION);
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recycler_view, container, false);
        // Set the adapter
        Context context = view.getContext();
        RecyclerView recyclerView = view.findViewById(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ModDetailGradebookAdapter();
        recyclerView.setAdapter(adapter);

        // Getting SwipeContainerLayout
        swipeLayout = view.findViewById(R.id.swipe_container);
        // Adding Listener
        swipeLayout.setOnRefreshListener(() -> refresh(true));
        refresh(false);

        return view;
    }

    private void refresh(boolean forceRefresh) {
        if (getActivity() != null) {
            KVV kvv = ((MainActivity) getActivity()).getKVV();
            kvv.getModule(mItemPos, (Modules.Module module) -> {
                adapter.setModule(module);
                kvv.getModuleGradebook(module, success1 -> {
                    adapter.setModule();
                    swipeLayout.setRefreshing(false);
                }, error -> {
                    swipeLayout.setRefreshing(false);
                    log.e(error);
                }, forceRefresh);
            }, error -> {
                swipeLayout.setRefreshing(false);
                log.e(error);
            }, forceRefresh);
        }
    }

}
