package de.sebse.fuplanner.fragments.moddetails;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

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 ModDetailAnnounceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ModDetailAnnounceFragment extends Fragment {
    private static final String ARG_POSITION = "itemPosition";

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


    public ModDetailAnnounceFragment() {
        // 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 ModDetailAnnounceFragment newInstance(String itemPosition) {
        ModDetailAnnounceFragment fragment = new ModDetailAnnounceFragment();
        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_expandable_list_view, container, false);
        // Set the adapter
        ExpandableListView expandableListView = view.findViewById(R.id.list);
        adapter = new ModDetailAnnounceAdapter();
        expandableListView.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.getModuleAnnouncements(module, success1 -> {
                    adapter.setModule();
                    swipeLayout.setRefreshing(false);
                }, error -> {
                    swipeLayout.setRefreshing(false);
                    log.e(error);
                }, forceRefresh);
            }, error -> {
                swipeLayout.setRefreshing(false);
                log.e(error);
            }, forceRefresh);
        }
    }

}
