package de.sebse.fuplanner.fragments.moddetails;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.KVV.types.Modules;
import de.sebse.fuplanner.tools.MainAcitivityListener;
import de.sebse.fuplanner.tools.logging.Logger;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MainAcitivityListener} interface
 * to handle interaction events.
 * Use the {@link ModDetailFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ModDetailFragment extends Fragment implements ModDetailListener {
    private static final String ARG_POSITION = "itemPosition";

    // Parameters
    private String mItemPos;

    private MainAcitivityListener mListener;
    private final Logger log = new Logger(this);
    private ViewPager mViewPager;

    public ModDetailFragment() {
        // 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 ModDetailFragment.
     */
    public static Fragment newInstance(String itemPosition) {
        ModDetailFragment fragment = new ModDetailFragment();
        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);
        }
        if (mListener != null) {
            mListener.onTitleTextChange(R.string.courses);
            mListener.getKVV().getModuleList(success -> {
                Modules.Module module = success.get(mItemPos);
                if (mListener != null && module != null)
                    mListener.onTitleTextChange(module.title);
            }, log::e);
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_mod_detail, container, false);

        mViewPager = v.findViewById(R.id.vpPager);
        ModDetailAdapter adapterViewPager = new ModDetailAdapter(getChildFragmentManager(), mItemPos, getContext());
        mViewPager.setAdapter(adapterViewPager);
        return v;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainAcitivityListener) {
            mListener = (MainAcitivityListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement MainActivityListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void gotoFragmentPart(int part, int index) {
        mViewPager.setCurrentItem(ModulePart.getPageByPart(part), true);
    }
}
