package br.com.stackoverflowsearch;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import java.util.List;

import br.com.stackoverflowsearch.Apdaters.QuestionListAdapter;
import br.com.stackoverflowsearch.Models.Question;
import br.com.stackoverflowsearch.Services.RestQuestionService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class QuestionFragment extends Fragment implements QuestionListAdapter.OnClickQuestionListener {

    private static final String TAG_ARG = "tag";
    private String mTagArg;
    private RestQuestionService mQuestionService;
    private RecyclerView mRecyclerView;
    private ProgressBar mProgressBar;
    private SwipeRefreshLayout mSwipeRefreshLayout;


    public static QuestionFragment newInstance(String tag) {
        Bundle bundle = new Bundle(1);
        bundle.putString(TAG_ARG, tag);
        QuestionFragment fragment = new QuestionFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_question, container, false);
        mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar_question);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_question_list);
        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager lm = new LinearLayoutManager(getActivity());
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(lm);
        mQuestionService = new RestQuestionService();
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_list);
        mSwipeRefreshLayout.setOnRefreshListener(this::getQuestion);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        getQuestion();
    }

    private void getQuestion() {
        mQuestionService.getQuestion(mTagArg, new Callback<Question>() {
            @Override
            public void onResponse(Call<Question> call, Response<Question> response) {
                if (response.isSuccessful()) {
                    List<Question> list = response.body().questionList;
                    mRecyclerView.setVisibility(View.VISIBLE);
                    mRecyclerView.setAdapter(new QuestionListAdapter(list, QuestionFragment.this));
                }
                mProgressBar.setVisibility(View.GONE);
                mSwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(Call<Question> call, Throwable t) {

            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTagArg = getArguments().getString(TAG_ARG);
    }


    @Override
    public void onClickCallback(Question question) {
        startActivity(new Intent(getActivity(), AnswerActivity.class).putExtra("question", question));
    }
}