package com.epam.sample.adapters;

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.TextView;

import com.epam.sample.R;
import com.epam.sample.models.DayImages;
import com.epam.sample.widget.MySquareImagesView;
import com.epam.widget.SquareImagesLayout;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.Optional;

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

    private int mScreenWidth;
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<DayImages> mDaysImages;

    private static final boolean USE_LAYOUT = false;

    public GalleryListAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;

        // Picasso.with(mContext).setLoggingEnabled(true);
    }

    public void setList(ArrayList<DayImages> mergeCursor) {
        if (mergeCursor == mDaysImages) {
            return;
        }

        mDaysImages = mergeCursor;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (null == mDaysImages) {
            return 0;
        }

        return mDaysImages.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v;
        if (USE_LAYOUT) {
            v = mInflater.inflate(R.layout.list_item_gallery_list_layout, viewGroup, false);
        } else {
            v = mInflater.inflate(R.layout.list_item_gallery_list_view, viewGroup, false);
        }

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        final DayImages dayImages = mDaysImages.get(i);
        int imagesNum = dayImages.getUriCount();

        int colNum = (int) Math.ceil(Math.sqrt(imagesNum));
        int rowNum = (int) Math.floor(Math.sqrt(imagesNum));
        while (colNum * rowNum < imagesNum) {
            rowNum++;
        }

        viewHolder.txvTitle.setText(dayImages.getDayDate() + " - (" + imagesNum + ")");

        if (null != viewHolder.silImages) {
            viewHolder.silImages.setNumberOfRows(rowNum);
            viewHolder.silImages.setNumberOfColumns(colNum);

            int contentPadding = viewHolder.silImages.getContentPadding() * (colNum - 1);
            int viewPadding = viewHolder.silImages.getPaddingLeft() + viewHolder.silImages.getPaddingRight();
            int availableWidth = mScreenWidth - (contentPadding + viewPadding);
            int imageSize = availableWidth / colNum;

            for (int k = 0; k < imagesNum && k < viewHolder.silImages.getChildCount(); k++) {
                Picasso.with(mContext)
                        .load("file://" + dayImages.getUriAt(k).toString())
                        .resize(imageSize, imageSize)
                        .centerCrop()
                        .into(viewHolder.silImages.getChildAt(k));
            }
        } else {
            viewHolder.sivImages.setTag(i);
            viewHolder.sivImages.clearImages(false);

            viewHolder.sivImages.setNumberOfRows(rowNum);
            viewHolder.sivImages.setNumberOfColumns(colNum);

            int contentPadding = viewHolder.sivImages.getContentPadding() * (colNum - 1);
            int viewPadding = viewHolder.sivImages.getPaddingLeft() + viewHolder.sivImages.getPaddingRight();
            int availableWidth = mScreenWidth - (contentPadding + viewPadding);
            int imageSize = availableWidth / colNum;

            for (int k = 0; k < dayImages.getUriCount(); k++) {
                Picasso.with(mContext)
                        .load("file://" + dayImages.getUriAt(k).toString())
                        .resize(imageSize, imageSize)
                        .centerCrop()
                        .into(viewHolder.sivImages);
            }
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.gallery_list_txv_title)
        TextView txvTitle;
        @Optional
        @InjectView(R.id.gallery_list_sil_images)
        SquareImagesLayout silImages;
        @Optional
        @InjectView(R.id.gallery_list_siv_images)
        MySquareImagesView sivImages;

        public ViewHolder(View itemView) {
            super(itemView);

            ButterKnife.inject(this, itemView);
        }
    }
}

@@ -822,18 +822,69 @@
 nal 
-boolean US
+int VIEW_TYPE_VIEW = 0;%0A    private static final int VIEW_TYP
 E_LA
@@ -890,21 +890,17 @@
 AYOUT = 
-false
+1
 ;%0A%0A    p
@@ -1212,27 +1212,25 @@
 Images%3E 
-mergeCursor
+dayImages
 ) %7B%0A    
@@ -1237,27 +1237,25 @@
     if (
-mergeCursor
+dayImages
  == mDay
@@ -1322,19 +1322,17 @@
 s = 
-mergeCursor
+dayImages
 ;%0A  
@@ -1516,32 +1516,131 @@
 .size();%0A    %7D%0A%0A
+    @Override%0A    public int getItemViewType(int position) %7B%0A        return VIEW_TYPE_VIEW;%0A    %7D%0A%0A
     @Override%0A  
@@ -1699,25 +1699,32 @@
 wGroup, int 
-i
+position
 ) %7B%0A        
@@ -1743,26 +1743,82 @@
     
-if (USE_LAYOUT) %7B%0A
+switch (getItemViewType(position)) %7B%0A            case VIEW_TYPE_VIEW:%0A    
     
@@ -1879,22 +1879,20 @@
 ry_list_
-layout
+view
 , viewGr
@@ -1908,33 +1908,78 @@
 e);%0A        
-%7D else %7B%0A
+        break;%0A%0A            case VIEW_TYPE_LAYOUT:%0A   
             
@@ -1970,32 +1970,33 @@
 %0A               
+ 
 v = mInflater.in
@@ -2025,36 +2025,38 @@
 em_gallery_list_
-view
+layout
 , viewGroup, fal
@@ -2052,32 +2052,249 @@
 wGroup, false);%0A
+                break;%0A%0A            default:%0A                throw new IllegalArgumentException(%22Invalid View Type (%22%0A                        + getItemViewType(position)%0A                        + %22) at %22 + position);%0A
         %7D%0A%0A     
@@ -2403,17 +2403,24 @@
 er, int 
-i
+position
 ) %7B%0A    
@@ -2467,17 +2467,24 @@
 ges.get(
-i
+position
 );%0A     
@@ -3754,17 +3754,24 @@
 .setTag(
-i
+position
 );%0A     

use getItemViewType