package uk.co.nyakeh.papertrail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class ArchiveFragment extends Fragment {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private RecyclerView mBookRecyclerView;
    private ArchivedBookAdapter mArchivedBookAdapter;
    private TextView mBookListEmptyMessageView;
    private List<Integer> headerPositionList = new ArrayList<>();

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

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

        mBookListEmptyMessageView = (TextView) view.findViewById(R.id.book_list_empty_message);
        mBookRecyclerView = (RecyclerView) view.findViewById(R.id.book_recycler_view);
        mBookRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST);
        mBookRecyclerView.addItemDecoration(itemDecoration);

        updateUI();
        return view;
    }

    private void updateUI() {
        BookLab bookLab = BookLab.get(getActivity());
        List<Book> archivedBooks = bookLab.getArchivedBooks();

        List<Book> bookList = new ArrayList<>();
        headerPositionList = new ArrayList<>();
        if (!archivedBooks.isEmpty()) {
            int position = 1;
            String currentMonth = DateFormat.format(Constants.MONTH_DATE_FORMAT, archivedBooks.get(0).getDateFinished()).toString();
            Book book1 = new Book("");
            book1.setTitle(currentMonth);
            bookList.add(book1);
            headerPositionList.add(0);
            for (Iterator<Book> i = archivedBooks.iterator(); i.hasNext(); ) {
                Book book = i.next();
                String bookMonth = DateFormat.format(Constants.MONTH_DATE_FORMAT, book.getDateFinished()).toString();
                if (!bookMonth.equals(currentMonth)){
                    currentMonth = bookMonth;
                    Book bookHeader = new Book(UUID.randomUUID());
                    bookHeader.setTitle(bookMonth);
                    bookList.add(bookHeader);
                    headerPositionList.add(position);
                    position++;
                }
                bookList.add(book);
                position++;
            }
        }

        if (mArchivedBookAdapter == null) {
            mArchivedBookAdapter = new ArchivedBookAdapter(bookList);
            mBookRecyclerView.setAdapter(mArchivedBookAdapter);
        } else {
            mArchivedBookAdapter.setBooks(bookList);
            mArchivedBookAdapter.notifyDataSetChanged();
        }

        if (archivedBooks.isEmpty()) {
            mBookRecyclerView.setVisibility(View.GONE);
            mBookListEmptyMessageView.setVisibility(View.VISIBLE);
        } else {
            mBookRecyclerView.setVisibility(View.VISIBLE);
            mBookListEmptyMessageView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    private class ArchivedBookHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private Book mBook;
        private TextView mLetterTextView;
        private TextView mTitleTextView;
        private TextView mAuthorTextView;
        private TextView mRatingTextView;

        public ArchivedBookHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            mLetterTextView = (TextView) itemView.findViewById(R.id.list_item_book_letter);
            mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_book_title);
            mAuthorTextView = (TextView) itemView.findViewById(R.id.list_item_book_author);
            mRatingTextView = (TextView) itemView.findViewById(R.id.list_item_book_rating);
        }

        private void bindBook(Book book) {
            mBook = book;
            String letter = (book.getCategory().isEmpty()) ? "" : book.getCategory().substring(0, 1);
            mLetterTextView.setText(letter);
            mTitleTextView.setText(book.getTitle());
            mAuthorTextView.setText(book.getAuthor());
            if (book.getRating() > 0){
                mRatingTextView.setText(String.valueOf(book.getRating()));
            }
        }

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), BookActivity.class);
            intent.putExtra(Constants.ARG_BOOK_ID, mBook.getId());
            startActivity(intent);
        }
    }

    private class ArchivedBookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<Book> mBooks;

        public ArchivedBookAdapter(List<Book> books) {
            mBooks = books;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            if (viewType == TYPE_HEADER) {
                View view = layoutInflater.from(parent.getContext()).inflate(R.layout.list_item_archive_heading, parent, false);
                return new VHHeader(view);
            } else {
                View view = layoutInflater.inflate(R.layout.list_item_book_archived, parent, false);
                return new ArchivedBookHolder(view);
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            Book book = mBooks.get(position);
            if (holder instanceof VHHeader) {
                VHHeader VHheader = (VHHeader) holder;
                VHheader.mHeading.setText(book.getTitle());
            } else if (holder instanceof ArchivedBookHolder) {
                ArchivedBookHolder archivedBookHolder = (ArchivedBookHolder) holder;
                archivedBookHolder.bindBook(book);
            }
        }

        @Override
        public int getItemCount() {
            return mBooks.size();
        }

        public void setBooks(List<Book> books) {
            mBooks = books;
        }

        @Override
        public int getItemViewType(int position) {
            if (isPositionHeader(position))
                return TYPE_HEADER;
            return TYPE_ITEM;
        }

        private boolean isPositionHeader(int position) {
            return headerPositionList.contains(position);
        }

        class VHHeader extends RecyclerView.ViewHolder {
            TextView mHeading;

            public VHHeader(View itemView) {
                super(itemView);
                this.mHeading = (TextView) itemView.findViewById(R.id.list_item_archive_heading);
            }
        }
    }
}

@@ -385,24 +385,25 @@
 t.TextView;%0A
+%0A
 import java.
@@ -2594,16 +2594,17 @@
 tMonth))
+ 
 %7B%0A      
@@ -3889,22 +3889,24 @@
 xtView m
-Letter
+Category
 TextView
@@ -4166,38 +4166,40 @@
 ;%0A%0A            m
-Letter
+Category
 TextView = (Text
@@ -4628,22 +4628,24 @@
  String 
-letter
+category
  = (book
@@ -4730,14 +4730,16 @@
    m
-Letter
+Category
 Text
@@ -4751,22 +4751,24 @@
 setText(
-letter
+category
 );%0A     
@@ -4882,18 +4882,31 @@
         
-if
+String rating =
  (book.g
@@ -4920,54 +4920,21 @@
 g() 
-%3E 0)%7B%0A                mRatingTextView.setText(
+%3C= 0) ? %22%22 : 
 Stri
@@ -4961,17 +4961,16 @@
 ating())
-)
 ;%0A      
@@ -4967,33 +4967,64 @@
 ));%0A            
-%7D
+mRatingTextView.setText(rating);
 %0A        %7D%0A%0A    

Revert "Revert "+ Bug fix: when a book didn't have a rating set higher than zero, it was using the previous books value in the RecyclerView.""