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);
        initJigsaw(id);
    }

    private void initJigsaw(long id) {
        List<ImageEntity> entities = new LinkedList<>(imgserv.findTiles(id));
        List<Bitmap> ramdomArrangement = getRandomArrangement(entities);
        initThumbnails(ramdomArrangement, entities.size());
    }

    private void initThumbnails(List<Bitmap> bitmaps, int size) {
        numColumns = (int) Math.sqrt(size);
        mThumbIds = new Bitmap[size];

        int n = 0;
        for (Bitmap b : bitmaps) {
            mThumbIds[n] = b;
            n++;
        }
    }

    private List<Bitmap> getRandomArrangement(List<ImageEntity> entities) {
        List<Bitmap> ramdomArrangement = new ArrayList<>();
        Random r = new Random();

        for (ImageEntity e : entities) {
            int index = r.nextInt(entities.size());

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

    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;
    }
}

@@ -465,94 +465,42 @@
 til.
-ArrayList;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Random
+Collections;%0Aimport java.util.List
 ;%0A%0Ap
@@ -893,25 +893,8 @@
 s = 
-new LinkedList%3C%3E(
 imgs
@@ -914,82 +914,8 @@
 (id)
-);%0A        List%3CBitmap%3E ramdomArrangement = getRandomArrangement(entities)
 ;%0A  
@@ -935,33 +935,24 @@
 mbnails(
-ramdomArrangement
+entities
 , entiti
@@ -1011,22 +1011,28 @@
 ist%3C
-Bitmap%3E bitmap
+ImageEntity%3E entitie
 s, i
@@ -1137,182 +1137,28 @@
     
-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 
+Collections.shuffle(
 enti
@@ -1154,34 +1154,34 @@
 huffle(entities)
- %7B
+;%0A
 %0A        List%3CBi
@@ -1177,356 +1177,137 @@
     
-List%3CBitmap%3E ramdomArrangement = new ArrayList%3C%3E();%0A        Random r = new Random();%0A%0A        for (ImageEntity e : entities) %7B%0A            int index = r.nextInt(entities.size());%0A%0A            Bitmap d = entities.get(index).getImage();%0A            ramdomArrangement.add(d);%0A            entities.remove(d);%0A        %7D%0A        return ramdomArrangement;
+int n = 0;%0A        for (ImageEntity entity : entities) %7B%0A            mThumbIds%5Bn%5D = entity.getImage();%0A            n++;%0A        %7D
 %0A   
@@ -2208,16 +2208,58 @@
 rawable(
+mContext.getResources()%0A                , 
 d));%0A   

clean up