package com.jamesdube.laravelnewsapp.adapters;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jamesdube.laravelnewsapp.App;
import com.jamesdube.laravelnewsapp.R;
import com.jamesdube.laravelnewsapp.models.Post;
import com.jamesdube.laravelnewsapp.posts.PostActivity;

import java.util.List;
import java.util.Random;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
    List<Post> posts;
    private LayoutInflater inflator;
    public PostAdapter(List<Post> posts) {
        this.posts = posts;
        inflator = LayoutInflater.from(App.getAppContext());
    }

    @Override
    public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflator.inflate(R.layout.template_posts,parent,false);
        return new PostViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PostViewHolder holder, int position) {
        holder.title.setText(posts.get(position).getTitle());
        holder.subTitle.setText(posts.get(position).getSubTitle());
        //holder.coverImage.setImageUrl(posts.get(position).getCoverImage(),new ImageLoader());
        Glide.with(App.getAppContext()).load(getRandomImage())
                .into(holder.coverImage);
        holder.coverImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getAppContext()
                        .startActivity(new Intent(App.getAppContext(), PostActivity.class));
            }
        });

    }

    public static int getRandomImage(){
        Random rand = new Random();
        switch (rand.nextInt(8)){
            case 1 : {
                return R.drawable.article1;
            }
            case 2 : {
                return R.drawable.article2;
            }
            case 3 : {
                return R.drawable.article3;
            }
            case 4 : {
                return R.drawable.article4;
            }
            case 5 : {
                return R.drawable.article5;
            }
            case 6 : {
                return R.drawable.article6;
            }
            case 7 : {
                return R.drawable.article7;
            }
            case 8 : {
                return R.drawable.article8;
            }
            default:{
                return R.drawable.article1;
            }
        }
    }

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

     class PostViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        TextView subTitle;
        ImageView coverImage;

        PostViewHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.postTitle);
            subTitle = (TextView) itemView.findViewById(R.id.postSubtitle);
            coverImage = (ImageView) itemView.findViewById(R.id.postCoverImage);
        }
    }
}

@@ -1295,16 +1295,22 @@
  holder,
+ final
  int pos
@@ -1807,78 +1807,165 @@
     
-App.getAppContext()%0A                        .startActivity(new Intent(
+Intent postIntent = new Intent(App.getAppContext(), PostActivity.class);%0A                postIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);%0A                
 App.
@@ -1971,37 +1971,37 @@
 .getAppContext()
-, Pos
+.star
 tActivity.class)
@@ -1993,23 +1993,27 @@
 Activity
-.class)
+(postIntent
 );%0A     

add new Activity Flag on intent