package com.lucas.exercicio.tela.um;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.lucas.exercicio.R;
import com.lucas.exercicio.TargetPicasso;
import com.lucas.exercicio.tela.dois.DescricaoActivity;
import com.squareup.picasso.Picasso;

import java.io.File;

/**
 * @author Lucas Campos
 *         12/28/15
 */
public class LocalAdapter extends RecyclerView.Adapter<LocalAdapter.ViewHolder> {

    public static final String VENUE = "venue";
    public static final String URL_WALLPAPER = "http://aviewfrommyseat.com/wallpaper/";
    public static final int WIDTH_LAND = 650;
    public static final int HEIGHT_LAND = 200;
    public static final int WIDTH_PORTRAIT = 430;
    public static final int HEIGHT__PORTRAIT = 200;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista, parent, false);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecyclerView recycler = (RecyclerView) context.findViewById(R.id.lista);
                int itemPosition = recycler.getChildPosition(layout);

                Intent intent = new Intent(context, DescricaoActivity.class);
                intent.putExtra(VENUE, modelos.getAvfms().get(itemPosition).getVenue());
                context.startActivity(intent);
            }
        });
        ViewHolder holder = new ViewHolder(layout);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Local itemSelecionado = modelos.getAvfms().get(position);

        ImageView imageView = (ImageView) holder.layout.findViewById(R.id.imagem);

        File folder = context.getFilesDir();

        if (!folder.exists()) {
            folder.mkdirs();
        }

        File mediaFile = new File(folder.getPath() + File.pathSeparator + itemSelecionado.getImage());
        if (!mediaFile.exists()) {
            Picasso.with(context)
                    .load(URL_WALLPAPER + itemSelecionado.getImage())
                    .into(new TargetPicasso(itemSelecionado.getImage(), context));
        }

        Bitmap foto = BitmapFactory.decodeFile(mediaFile.getPath());
        if (foto != null) {
            Bitmap reduzida = null;
            if (context.getResources().getBoolean(R.bool.isLand)) {
                reduzida = Bitmap.createScaledBitmap(foto, WIDTH_LAND, HEIGHT_LAND, true);
            } else {
                reduzida = Bitmap.createScaledBitmap(foto, WIDTH_PORTRAIT, HEIGHT__PORTRAIT, true);
            }
            imageView.setImageBitmap(reduzida);
        } else {
            Picasso.with(context)
                    .load(URL_WALLPAPER + itemSelecionado.getImage())
                    .fit()
                    .into(imageView);
        }

        TextView venue = (TextView) holder.layout.findViewById(R.id.venue);
        venue.setText(itemSelecionado.getVenue());

        TextView note = (TextView) holder.layout.findViewById(R.id.note);
        note.setText(itemSelecionado.getNote());

        TextView view = (TextView) holder.layout.findViewById(R.id.view);
        view.setText(String.valueOf(itemSelecionado.getViews()));
    }

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

    private final ListaLocal modelos;
    private final Activity context;

    public LocalAdapter(ListaLocal modelos, Activity context) {
        this.modelos = modelos;
        this.context = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View layout;

        public ViewHolder(View tela) {
            super(tela);
            layout = tela;
        }
    }

}

@@ -95,79 +95,8 @@
 nt;%0A
-import android.graphics.Bitmap;%0Aimport android.graphics.BitmapFactory;%0A
 impo
@@ -759,203 +759,8 @@
 r/%22;
-%0A    public static final int WIDTH_LAND = 650;%0A    public static final int HEIGHT_LAND = 200;%0A    public static final int WIDTH_PORTRAIT = 430;%0A    public static final int HEIGHT__PORTRAIT = 200;
 %0A%0A  
@@ -951,24 +951,25 @@
 nt, false);%0A
+%0A
         layo
@@ -1483,24 +1483,25 @@
     %7D);%0A
+%0A
         
 ViewHold
@@ -1496,52 +1496,280 @@
     
-ViewHolder holder = new ViewHolder(layout
+TextView venue = (TextView) layout.findViewById(R.id.venue);%0A        TextView note = (TextView) layout.findViewById(R.id.note);%0A        TextView view = (TextView) layout.findViewById(R.id.view);%0A        ImageView imageView = (ImageView) layout.findViewById(R.id.imagem
 );%0A
+%0A
     
@@ -1779,22 +1779,68 @@
  return 
-holder
+new ViewHolder(layout, venue, note, view, imageView)
 ;%0A    %7D%0A
@@ -1993,92 +1993,8 @@
 );%0A%0A
-        ImageView imageView = (ImageView) holder.layout.findViewById(R.id.imagem);%0A%0A
     
@@ -2455,756 +2455,133 @@
     
-Bitmap foto = BitmapFactory.decodeFile(mediaFile.getPath());%0A        if (foto != null) %7B%0A            Bitmap reduzida = null;%0A            if (context.getResources().getBoolean(R.bool.isLand)) %7B%0A                reduzida = Bitmap.createScaledBitmap(foto, WIDTH_LAND, HEIGHT_LAND, true);%0A            %7D else %7B%0A                reduzida = Bitmap.createScaledBitmap(foto, WIDTH_PORTRAIT, HEIGHT__PORTRAIT, true);%0A            %7D%0A            imageView.setImageBitmap(reduzida);%0A        %7D else %7B%0A            Picasso.with(context)%0A                    .load(URL_WALLPAPER + itemSelecionado.getImage())%0A                    .fit()%0A                    .into(imageView);%0A        %7D%0A%0A        TextView venue = (TextView) holder.layout.findViewById(R.id.venue);%0A        
+//TODO ajustar tamanho da imagem%0A        Picasso.with(context).load(mediaFile).fit().into(holder.imageView);%0A%0A        holder.
 venu
@@ -2615,33 +2615,32 @@
 etVenue());%0A
-%0A
         
 TextView not
@@ -2631,82 +2631,15 @@
     
-TextView note = (TextView) holder.layout.findViewById(R.id.note);%0A        
+holder.
 note
@@ -2675,25 +2675,24 @@
 ote());%0A
-%0A
         
 TextView
@@ -2687,82 +2687,15 @@
     
-TextView view = (TextView) holder.layout.findViewById(R.id.view);%0A        
+holder.
 view
@@ -3142,63 +3142,235 @@
    p
-ublic View layout;%0A%0A        public ViewHolder(View tela
+rivate TextView venue;%0A        private TextView note;%0A        private TextView view;%0A        private ImageView imageView;%0A%0A        public ViewHolder(View layout, TextView venue, TextView note, TextView view, ImageView imageView
 ) %7B%0A
@@ -3387,20 +3387,22 @@
   super(
-te
 la
+yout
 );%0A     
@@ -3412,21 +3412,126 @@
     
-layout = tela
+this.view = view;%0A            this.note = note;%0A            this.venue = venue;%0A            this.imageView = imageView
 ;%0A  
@@ -3534,21 +3534,22 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%0A%7D%0A

Load local image with Picasso