package com.developerxy.myimdb.ui.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
import com.developerxy.myimdb.R;
import com.developerxy.myimdb.adapters.GenreAdapter;
import com.developerxy.myimdb.models.Genre;
import com.developerxy.myimdb.models.Show;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SecondFragment extends NavigableFragment {

    private SecondFragmentListener mListener;
    private GenreAdapter genreAdapter;

    @Bind(R.id.genreListview)
    ListView genreListview;

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

    public static SecondFragment newInstance(Show show) {
        SecondFragment fragment = new SecondFragment();

        if (show != null) {
            Bundle args = new Bundle();

            List<String> genres = show.getGenres();
            args.putStringArrayList("genres", new ArrayList<>(genres));

            fragment.setArguments(args);
        }

        return fragment;
    }

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

        List<String> genres = new ArrayList<>();
        Bundle args = getArguments();
        if (args != null) {
            genres = args.getStringArrayList("genres");
        }

        List<String> gList = Arrays.asList(
                getActivity().getResources().getStringArray(R.array.genreArray));
        List<Genre> genresList = Stream.of(gList).map(g -> new Genre(g, false)).collect(Collectors.toList());
        Stream.of(genres)
                .forEach(g -> {
                    int index = gList.indexOf(g);
                    genresList.get(index).setSelected(true);
                });

        System.out.println(genresList);

        genreAdapter = new GenreAdapter(getActivity(), R.layout.genre_list_item, genresList);
        genreListview.setAdapter(genreAdapter);

        return view;
    }

    /* If the onAttach(Context context) version is used instead, the method does not get called on API 19 devices. */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (SecondFragmentListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement SecondFragmentListener");
        }
    }

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

    @Override
    public void onNext() {
        List<String> genres = genreAdapter.getSelectedGenres();
        if (genres.size() == 0) {
            Toast.makeText(getActivity(), "Please select at least 1 genre.", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mListener != null)
            mListener.navigateToThirdFragment(genres);
        else
            Toast.makeText(getActivity(), "Listener is null.", Toast.LENGTH_SHORT).show();
    }

    public interface SecondFragmentListener {
        void navigateToThirdFragment(List<String> genres);
    }
}
