package com.csed17.library.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.csed17.library.R;
import com.csed17.library.activities.ResultActivity;
import com.csed17.library.adapters.LibraryAdapter;
import com.csed17.library.models.api.LibrariesResponse;
import com.csed17.library.models.core.Library;
import com.csed17.library.services.api.retrofit.ApiBuilder;
import com.csed17.library.util.ApplicationData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by islamsalah on 12/6/16.
 */
public class HomeFragment extends Fragment {

    @BindView(R.id.library_recycler_view)
    RecyclerView mLibraryRV;

    private LibraryAdapter mLibraryAdapter;
    private Callback<LibrariesResponse> mCallback = new Callback<LibrariesResponse>() {
        @Override
        public void onResponse(Call<LibrariesResponse> call, Response<LibrariesResponse> response) {
            if (response != null && response.isSuccessful()) {
                Log.i("TEST", "Successful connection!");
                ApplicationData.setLibraries(response.body().getLibraries());
                setupAdapter(ApplicationData.getLibraries());
            }
        }

        @Override
        public void onFailure(Call<LibrariesResponse> call, Throwable t) {
            Log.i("TEST", "Failure in connection!");

        }
    };

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);

        mLibraryRV.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        ApiBuilder.syncLibraries(mCallback);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_home, menu);

        MenuItem searchItem = menu.findItem(R.id.menu_item_search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setQueryHint(getResources().getString(R.string.search));

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent = ResultActivity.newIntent(getActivity(), query);
                startActivity(intent);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void setupAdapter(List<Library> libraries) {
        mLibraryAdapter = new LibraryAdapter(getActivity(), libraries);
        mLibraryRV.setAdapter(mLibraryAdapter);
    }
}
