package com.martin.feeder;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Path;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("WeakerAccess")
public class NewsFragment extends Fragment {

    public RecyclerView mList;
    private OnProgressChangeListener mCallback;
    private NewsSources nSources;
    private NewsCollection nColl;
    private RelativeLayout mSnackbar;
    private TextView mSnackButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_news, container, false);
        mList = (RecyclerView) v.findViewById(R.id.rvList);
        mSnackButton = (TextView) v.findViewById(R.id.snackbar_button);
        mSnackbar = (RelativeLayout) v.findViewById(R.id.snackbar);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mList.setLayoutManager(layoutManager);
        nSources = new NewsSources(getActivity());
        loadUnread();

        mSnackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSnackbar();
            }
        });

        // Initialize Snackbar
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
        mSnackbar.setY(mSnackbar.getY() + px);
        mSnackbar.setVisibility(View.VISIBLE);
    }

    void showSnackbar() {
        float px = -TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
        moveSnackbar(px);
    }

    private void hideSnackbar() {
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
        moveSnackbar(px);
    }

    private void moveSnackbar(float px) {
        Path p = new Path();
        ObjectAnimator sbAnimator;
        p.moveTo(mSnackbar.getX(), mSnackbar.getY());
        p.rLineTo(0, px);
        sbAnimator = ObjectAnimator.ofFloat(mSnackbar, View.X, View.Y, p);
        sbAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        sbAnimator.start();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnProgressChangeListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnProgressChangeListener");
        }
    }

    public void loadUnread() {
        if (mCallback.changeVisibility()) {
            getActivity().setProgressBarIndeterminateVisibility(true);
        }
        mCallback.actionStarted();
        new Thread(new Runnable() {

            @Override
            public void run() {
                nColl = nSources.getAllUnread();
                mList.post(new Runnable() {

                    @Override
                    public void run() {
                        if (nColl.getTitles().length == 0) {
                            showSnackbar();
                        } else {
                            // Only show entries if they're not full of nulls
                            if (!nColl.getTitles()[0].contains("null")) {
                                mList.setAdapter(new SiteAdapter(nColl, ((Main) getActivity()).findFragmentByPosition(0), "NewsFragment"));
                                nSources.setAllNotified(nColl);
                            }
                        }
                        mCallback.actionFinished();
                        if (mCallback.changeVisibility()) {
                            getActivity().setProgressBarIndeterminateVisibility(false);
                        }
                    }

                });
            }

        }).start();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mList.getAdapter() != null) {
            NewsCollection current = ((SiteAdapter) mList.getAdapter()).getColl();
            SharedPreferences prefs = getActivity().getSharedPreferences("open_collection", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putStringSet("titles", new HashSet<String>(Arrays.asList(current.getTitles())));
            editor.putStringSet("contents", new HashSet<String>(Arrays.asList(current.getContents())));
            editor.putStringSet("urls", new HashSet<String>(Arrays.asList(current.getContents())));
            editor.apply();
        }
    }
}

@@ -3630,24 +3630,701 @@
 onStarted();
+%0A%0A        // Load last entries%0A        SharedPreferences prefs = getActivity().getSharedPreferences(%22open_collection%22, Context.MODE_PRIVATE);%0A        Set%3CString%3E titles = prefs.getStringSet(%22titles%22, null);%0A        Set%3CString%3E contents = prefs.getStringSet(%22contents%22, null);%0A        Set%3CString%3E urls = prefs.getStringSet(%22urls%22, null);%0A        if (titles != null) %7B%0A            NewsCollection oldEntries = new NewsCollection((String%5B%5D) titles.toArray(), (String%5B%5D) contents.toArray(), (String%5B%5D) urls.toArray());%0A            mList.setAdapter(new SiteAdapter(oldEntries, ((Main) getActivity()).findFragmentByPosition(0), %22NewsFragment%22));%0A        %7D%0A%0A        // Load new entries
 %0A        new
@@ -5499,137 +5499,8 @@
 ();%0A
-        if (mList.getAdapter() != null) %7B%0A            NewsCollection current = ((SiteAdapter) mList.getAdapter()).getColl();%0A    
     
@@ -5610,28 +5610,24 @@
 E);%0A        
-    
 SharedPrefer
@@ -5658,24 +5658,243 @@
 efs.edit();%0A
+        // Delete old entries just in case we don't have any new ones%0A        editor.clear();%0A        if (mList.getAdapter() != null) %7B%0A            NewsCollection current = ((SiteAdapter) mList.getAdapter()).getColl();%0A
             
@@ -6181,32 +6181,38 @@
 tents())));%0A    
+    %7D%0A
         editor.a
@@ -6219,26 +6219,16 @@
 pply();%0A
-        %7D%0A
     %7D%0A%7D%0A

Load unread on startup