package com.levibostian.pantrypirate.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.levibostian.pantrypirate.R;
import com.levibostian.pantrypirate.model.RecipesModel;

public class HomeScreenRecipesAdapter extends ArrayAdapter {
    private Activity mContext;
    private RecipesModel mRecipesModel;

    public HomeScreenRecipesAdapter(Activity context, RecipesModel model) {
        super(context, R.layout.home_screen_recipes_list_item, model.getRecipes());

        mContext = context;
        mRecipesModel = model;
    }

    static class ViewHolder {
        public TextView recipeTitle;
        public ImageView recipeImage;
        public RatingBar recipeRating;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = mContext.getLayoutInflater();
            rowView = inflater.inflate(R.layout.home_screen_recipes_list_item, null);

            ViewHolder holder = new ViewHolder();
            holder.recipeTitle = (TextView) rowView.findViewById(R.id.recipe_title);
            holder.recipeImage = (ImageView) rowView.findViewById(R.id.recipe_image);
            holder.recipeRating = (RatingBar) rowView.findViewById(R.id.recipe_rating);
            rowView.setTag(holder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();
        holder.recipeTitle.setText(mRecipesModel.getRecipeTitle(position));
        holder.recipeImage.setImageBitmap(mRecipesModel.getRecipeBitmap(position));
        holder.recipeRating.setRating((float) 3.0);

        return rowView;
    }
}

@@ -392,16 +392,42 @@
 Model;%0A%0A
+import java.util.Random;%0A%0A
 public c
@@ -1817,32 +1817,48 @@
 tion));%0A        
+setRandomRating(
 holder.recipeRat
@@ -1864,30 +1864,8 @@
 ting
-.setRating((float) 3.0
 );%0A%0A
@@ -1887,16 +1887,296 @@
 rowView;
+%0A    %7D%0A%0A    private void setRandomRating(RatingBar ratingBar) %7B%0A        Random random = new Random();%0A%0A        int randInt = random.nextInt(6);%0A        if (randInt %3E= 3) %7B%0A            ratingBar.setRating(randInt);%0A        %7D else %7B%0A            setRandomRating(ratingBar);%0A        %7D
 %0A    %7D%0A%7D

Set custom rating to each star randomly.