/*
 * Copyright 2016 Mehmet Dilaveroğlu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package co.dilaver.quoter.fragments;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
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.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;

import co.dilaver.quoter.R;
import co.dilaver.quoter.activities.ShareActivity;
import co.dilaver.quoter.adapters.QuotesAdapter;
import co.dilaver.quoter.application.MyApplication;
import co.dilaver.quoter.models.Quote;
import co.dilaver.quoter.storage.SharedPrefStorage;


public class FavoriteQuotesFragment extends Fragment implements QuotesAdapter.LongClickListener {

    private static final String TAG = PopularFragment.class.getSimpleName();
    private QuotesAdapter quotesAdapter;
    private ArrayList<Quote> favoriteQuotesList;

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


        favoriteQuotesList = new ArrayList<>();
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rvFavoriteQuotes);
        TextView noFavoriteQuotes = (TextView) view.findViewById(R.id.tvNoFavoriteQuotes);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        quotesAdapter = new QuotesAdapter(getActivity());
        quotesAdapter.setLongClickListener(this);
        recyclerView.setAdapter(quotesAdapter);

        favoriteQuotesList = MyApplication.savedQuotesList;
        quotesAdapter.setList(favoriteQuotesList);

        if (favoriteQuotesList.isEmpty()) {
            noFavoriteQuotes.setVisibility(View.VISIBLE);
        } else {
            Toast.makeText(getActivity(), getString(R.string.str_longClickToDelete), Toast.LENGTH_SHORT).show();
        }

        return view;
    }

    @Override
    public void longClicked(View view, int position) {
        showAlertDialog(position);
    }

    private void showAlertDialog(final int pos) {
        CharSequence[] items = {getString(R.string.str_Delete), getString(R.string.str_Share)};

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 0) {
                    //Delete

                    SharedPrefStorage sharedPrefStorage = new SharedPrefStorage(getActivity());
                    Gson gson = new Gson();

                    for (int i = 0; i < MyApplication.savedQuotesList.size(); i++) {
                        if (MyApplication.savedQuotesList.get(i).getQuoteAuthor().equals(favoriteQuotesList.get(pos).getQuoteAuthor()) &&
                                MyApplication.savedQuotesList.get(i).getQuoteText().equals(favoriteQuotesList.get(pos).getQuoteText())) {

                            MyApplication.savedQuotesList.remove(i);
                            favoriteQuotesList = MyApplication.savedQuotesList;
                            sharedPrefStorage.setSavedQuotes(gson.toJson(MyApplication.savedQuotesList));
                        }
                    }

                    quotesAdapter.setList(favoriteQuotesList);
                    quotesAdapter.notifyDataSetChanged();

                } else if (item == 1)

                {
                    Intent shareIntent = new Intent(getActivity(), ShareActivity.class);
                    shareIntent.putExtra("quote", favoriteQuotesList.get(pos).getQuoteText());
                    shareIntent.putExtra("author", favoriteQuotesList.get(pos).getQuoteAuthor());
                    startActivity(shareIntent);
                }
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

@@ -654,88 +654,294 @@
 oid.
-content.DialogInterface;%0Aimport android.content.Intent;%0Aimport android.os.Bundle
+app.Activity;%0Aimport android.content.ClipData;%0Aimport android.content.ClipboardManager;%0Aimport android.content.DialogInterface;%0Aimport android.content.Intent;%0Aimport android.os.Bundle;%0Aimport android.support.design.widget.CoordinatorLayout;%0Aimport android.support.design.widget.Snackbar
 ;%0Aim
@@ -1875,16 +1875,59 @@
 esList;%0A
+    private CoordinatorLayout rootLayout;%0A%0A
 %0A    @Ov
@@ -2158,16 +2158,96 @@
 alse);%0A%0A
+        rootLayout = (CoordinatorLayout) view.findViewById(R.id.clFavoriteRoot);
 %0A       
@@ -3344,16 +3344,46 @@
 r_Share)
+, getString(R.string.str_Copy)
 %7D;%0A%0A    
@@ -4625,33 +4625,16 @@
 em == 1)
-%0A%0A               
  %7B%0A     
@@ -4954,24 +4954,533 @@
 areIntent);%0A
+                %7D else if (item == 2) %7B%0A                    ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Activity.CLIPBOARD_SERVICE);%0A                    ClipData clip = ClipData.newPlainText(%22Copied Text%22, favoriteQuotesList.get(pos).getQuoteText() + %22 - %22 +  favoriteQuotesList.get(pos).getQuoteAuthor());%0A                    clipboard.setPrimaryClip(clip);%0A%0A                    Snackbar.make(rootLayout, getString(R.string.str_QuoteCopied), Snackbar.LENGTH_SHORT).show();%0A
             

Implement copy action in Favorite quotes screen