package com.jigdraw.draw.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.jigdraw.draw.model.ImageEntity;
import com.jigdraw.draw.service.ImageService;
import com.jigdraw.draw.service.impl.ImageServiceImpl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class ImageAdapter extends BaseAdapter {
    private ImageService imgserv;
    private Context mContext;
    private Bitmap[] mThumbIds;
    private int numColumns;

    public ImageAdapter(Context c, long id) {
        mContext = c;
        imgserv = new ImageServiceImpl(c);
        LinkedList<ImageEntity> entities = new LinkedList<>(imgserv.findTiles(id));
        List<Bitmap> ramdomArrangement = new ArrayList<>();

        numColumns = (int) Math.sqrt(entities.size());

        mThumbIds = new Bitmap[entities.size()];

        Random r = new Random();

        //build a list with random arrangements
        for (ImageEntity e : entities) {
            int index = r.nextInt(entities.size());

            Bitmap d = entities.get(index).getImage();
            ramdomArrangement.add(d);
            entities.remove(d);
        }

        //add the images to the thumbs list
        int n = 0;
        for (Bitmap b : ramdomArrangement) {
            mThumbIds[n] = b;
            n++;
        }
    }

    public int getNumColumns() {
        return numColumns;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return mThumbIds[position];
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        Bitmap d = mThumbIds[position];
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(d
                    .getWidth(),
                    d.getHeight()));
            imageView.setPadding(1, 1, 1, 1);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageDrawable(new BitmapDrawable(d));
        return imageView;
    }
}

@@ -842,22 +842,86 @@
         
-L
 in
-ked
+itJigsaw(id);%0A    %7D%0A%0A    private void initJigsaw(long id) %7B%0A        
 List%3CIma
@@ -1027,133 +1027,553 @@
 t = 
-new ArrayList%3C%3E();%0A%0A        numColumns = (int) Math.sqrt(entities.size());%0A%0A        mThumbIds = new Bitmap%5Bentities.size()%5D;%0A
+getRandomArrangement(entities);%0A        initThumbnails(ramdomArrangement, entities.size());%0A    %7D%0A%0A    private void initThumbnails(List%3CBitmap%3E bitmaps, int size) %7B%0A        numColumns = (int) Math.sqrt(size);%0A        mThumbIds = new Bitmap%5Bsize%5D;%0A%0A        //add the images to the thumbs list%0A        int n = 0;%0A        for (Bitmap b : bitmaps) %7B%0A            mThumbIds%5Bn%5D = b;%0A            n++;%0A        %7D%0A    %7D%0A%0A    private List%3CBitmap%3E getRandomArrangement(List%3CImageEntity%3E entities) %7B%0A        List%3CBitmap%3E ramdomArrangement = new ArrayList%3C%3E();
 %0A   
@@ -1880,25 +1880,24 @@
       %7D%0A
-%0A
         
 //add th
@@ -1892,164 +1892,33 @@
     
-//add the images to the thumbs list%0A        int n = 0;%0A        for (Bitmap b : ramdomArrangement) %7B%0A            mThumbIds%5Bn%5D = b;%0A            n++;%0A        %7D
+return ramdomArrangement;
 %0A   

clean up and refactor.