package com.bookbase.app.library;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bookbase.app.R;
import com.bookbase.app.database.AppDatabase;
import com.bookbase.app.model.dao.AuthorDao;
import com.bookbase.app.model.entity.Author;
import com.bookbase.app.model.entity.Book;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BooksAdapter extends RecyclerView.Adapter<BooksAdapter.ViewHolder>{

    class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.book_list_image) ImageView coverImage;
        @BindView(R.id.book_list_title) TextView title;
        @BindView(R.id.book_list_author) TextView author;

        ViewHolder(View view){
            super(view);
            ButterKnife.bind(this, view);
        }

    }

    private List<Book> books;
    private Context context;

    BooksAdapter(Context context, List<Book> books){
        this.books = books;
        this.context = context;
    }

    private Context getContext(){
        return context;
    }

    @Override
    public BooksAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View bookView = inflater.inflate(R.layout.item_book, parent, false);
        return new ViewHolder(bookView);

    }

    public void onBindViewHolder(BooksAdapter.ViewHolder viewHolder, int position){

        // Get reference to item at current position.
        Book book = books.get(position);
        File file = null;

        final AppDatabase db = AppDatabase.getDatabase(context);
        final AuthorDao authorDao = db.authorDao();
        Author authorEntity = db.authorDao().getAuthorById(book.getAuthor());

        ImageView coverImage = viewHolder.coverImage;
        TextView title = viewHolder.title;
        TextView author = viewHolder.author;
        if(book.getCoverImage() != null){
            file = new File(book.getCoverImage());
        }

        // Set view content from model.
//        Picasso.with(this.getContext())
//                .load(book.getCoverImage())
//                .placeholder(R.drawable.book_default)
//                .error(R.drawable.book_default)
//                .into(coverImage);

        Picasso.with(this.getContext())
                .load(file)
                .placeholder(R.drawable.book_default)
                .error(R.drawable.book_default)
                .into(coverImage);
        title.setText(book.getTitle());
        //author.setText(book.getAuthor().toString());
        author.setText(authorEntity.getName());

    }

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



}

@@ -343,53 +343,8 @@
 se;%0A
-import com.bookbase.app.model.dao.AuthorDao;%0A
 impo
@@ -1676,62 +1676,8 @@
 )%7B%0A%0A
-        // Get reference to item at current position.%0A
     
@@ -1713,16 +1713,16 @@
 ition);%0A
+
         
@@ -1809,60 +1809,8 @@
 t);%0A
-        final AuthorDao authorDao = db.authorDao();%0A
     
@@ -2134,280 +2134,8 @@
  %7D%0A%0A
-        // Set view content from model.%0A//        Picasso.with(this.getContext())%0A//                .load(book.getCoverImage())%0A//                .placeholder(R.drawable.book_default)%0A//                .error(R.drawable.book_default)%0A//                .into(coverImage);%0A%0A
     
@@ -2379,63 +2379,8 @@
 ));%0A
-        //author.setText(book.getAuthor().toString());%0A
     
@@ -2509,15 +2509,13 @@
 ();%0A
+
     %7D%0A%0A
-%0A%0A
 %7D%0A

Clean up Books Adapter