/*
 * The MIT License (MIT)
 * Copyright (c) 2016. Sergio Leonardo Isasmendi
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package leoisasmendi.android.com.suricatepodcast.ui;

import android.app.Fragment;
import android.app.LoaderManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import leoisasmendi.android.com.suricatepodcast.R;
import leoisasmendi.android.com.suricatepodcast.data.PlaylistAdapter;
import leoisasmendi.android.com.suricatepodcast.provider.DataProvider;
import leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService;


public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    /*local*/

    OnFragmentInteractionListener mListener;
    final String TAG = getClass().getSimpleName();
    RecyclerView.LayoutManager mLayoutManager;
    RecyclerView mRecyclerView;
    PlaylistAdapter mAdapter;



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

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Start loader
        getLoaderManager().restartLoader(1, null, this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment, container, false);
        checkListenerImplementation(view.getContext());

        mRecyclerView = (RecyclerView) view.findViewById(R.id.master_fragment);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new PlaylistAdapter(mListener);
        mRecyclerView.setAdapter(mAdapter);
        setupFAB();
        return view;
    }

    private void setupFAB() {
        FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.contextual_fab);
        fab.setImageResource(R.drawable.magnifier);
        fab.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View view) {
                                       mListener.searchPodcast();
                                   }
                               }
        );
        fab.setContentDescription(getString(R.string.cd_search_fab));
    }

    private void checkListenerImplementation(Context context) {
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnMainListInteractionListener");
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        checkListenerImplementation(context);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        Log.d(TAG, "onCreateLoader: ");
        return new CursorLoader(getActivity().getBaseContext(), DataProvider.CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        Log.d(TAG, "onLoadFinished: ");
        if (mAdapter != null) {
            mAdapter.swapCursor(cursor);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }

    public interface OnFragmentInteractionListener {
        void searchPodcast();

        void onClick(int position, Cursor item);

        void onDeleteItem(int itemId);

        void onShowDetail(Cursor item);
    }
}

@@ -1271,50 +1271,8 @@
 er;%0A
-import android.content.BroadcastReceiver;%0A
 impo
@@ -1299,16 +1299,16 @@
 ontext;%0A
+
 import a
@@ -1340,39 +1340,8 @@
 er;%0A
-import android.content.Intent;%0A
 impo
@@ -1750,37 +1750,8 @@
 oup;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -1865,24 +1865,24 @@
 istAdapter;%0A
+
 import leois
@@ -1944,85 +1944,8 @@
 er;%0A
-import leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService;%0A
 %0A%0Apu

Remove unused imports