package org.keyscience.fragments;

import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import org.keyscience.BackgroundTask.CspLearnAsync;
import org.keyscience.R;
import org.keyscience.SingletonPattern.LabsSingletonList;
import org.keyscience.adapters.CspLearnAdapter;
import org.keyscience.models.CspLearnModel;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

public class CspLearnFragment extends Fragment {

    private static final String jsonUrl = "http://www.keyscience.org/wp-content/app/labs.json";
    private static final String phpUrl = "http://www.keyscience.org/wp-content/app/getLabsLearnData.php";

    private RecyclerView recyclerView;
    private CspLearnAsync task;
    private LinearLayout linearLayout;
    private LabsSingletonList<CspLearnModel> data;

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

        linearLayout = view.findViewById(R.id.load_animation_csplab_linearLayout);

        Display display = (getActivity().getWindowManager().getDefaultDisplay());
        Point size = new Point();
        display.getSize(size);
        int height = size.y;
        linearLayout.setMinimumHeight(height - 180);

        setUpRecyclerView(view);
        return view;
    }

    private void setUpRecyclerView(View view) {
        recyclerView = view.findViewById(R.id.learn_recyclerView);
        recyclerView.setFocusable(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // recyclerview content is fetched from the URL by an Async task.
        // When done, async task will set the recycler adapter
        data = LabsSingletonList.getListInstance();

        // if data has been already fetched, we are not going to download again. the list already has
        // data.
        if (data.size() == 0) {
            task = new CspLearnAsync(recyclerView, getContext(), data, linearLayout);
            task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, phpUrl, jsonUrl);
        } else {
            linearLayout.setVisibility(View.GONE);
            CspLearnAdapter adapter = new CspLearnAdapter(data.getList(), getContext());
            recyclerView.setAdapter(adapter);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (task != null) {
            task.cancel(true);
            try {
                //Waits if necessary to complete
                task.get();
            } catch (ExecutionException e) {
                //  ignore
            } catch (InterruptedException e) {
                //  ignore
            } catch (CancellationException e) {
                //  ignore
            }
            task = null;
        }
    }
}
