/*
 * Copyright (C) 2013 Marten Gajda <marten@dmfs.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

package org.dmfs.android.colorpicker;

import org.dmfs.android.colorpicker.palettes.AbstractPalette;
import org.dmfs.android.view.IDrawableTitlePagerAdapter;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.util.LruCache;


/**
 * Pager adapter for the {@link PaletteFragment}s.
 * 
 * @author Marten Gajda <marten@dmfs.org>
 */
public class PalettesPagerAdapter extends FragmentStatePagerAdapter implements IDrawableTitlePagerAdapter
{
	private final static int PREVIEW_SIZE = 32; // dp

	private final static int FACTOR = 100;

	private AbstractPalette[] mPalettes;
	private final Resources mResources;
	private final float mDensity;
	private final PreviewCache mCache = new PreviewCache(15);


	public PalettesPagerAdapter(Resources res, FragmentManager fm, AbstractPalette... palettes)
	{
		super(fm);
		mPalettes = palettes;
		mResources = res;
		mDensity = res.getDisplayMetrics().density;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.FragmentStatePagerAdapter#getItem(int)
	 */
	@Override
	public Fragment getItem(int position)
	{
		PaletteFragment fragment = new PaletteFragment();
		fragment.setPalette(mPalettes[mapPosition(position)]);
		return fragment;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
	@Override
	public int getCount()
	{
		return mPalettes.length > 1 ? mPalettes.length * FACTOR : 1;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.PagerAdapter#getPageTitle(int)
	 */
	@Override
	public CharSequence getPageTitle(int position)
	{
		return mPalettes[mapPosition(position)].getName();
	}


	/**
	 * Get the actual position from the "infinite" position.
	 * 
	 * @param position
	 *            The position in the "infinite" list of palettes.
	 * @return The actual position.
	 */
	private int mapPosition(int position)
	{
		return position % mPalettes.length;
	}


	@Override
	public Drawable getDrawableTitle(int position)
	{
		return mCache.get(mapPosition(position));
	}

	/**
	 * A cache for palette preview images.
	 */
	class PreviewCache extends LruCache<Integer, Drawable>
	{

		public PreviewCache(int maxSize)
		{
			super(maxSize);
		}


		@Override
		protected Drawable create(Integer key)
		{
			AbstractPalette palette = mPalettes[key];
			final int size = (int) (PREVIEW_SIZE * mDensity);
			final int cols = palette.getNumberOfColumns();

			Bitmap preview = Bitmap.createBitmap(size, size, Config.ARGB_8888);
			Canvas canvas = new Canvas(preview);

			final float spacing = 1.2f * mDensity;
			final float halfSpacing = spacing / 2;
			final float grid = (size + spacing) / cols;
			final float radius = (grid - spacing) / 2;
			Paint paint = new Paint();
			paint.setFlags(Paint.ANTI_ALIAS_FLAG);

			for (int j = 0, k = palette.getNumberOfColors() / cols; j < k; ++j)
			{
				for (int i = 0, l = cols; i < l; ++i)
				{
					paint.setColor(palette.getColor(j * cols + i) + 0xff000000);
					// canvas.drawRect(i * grid, j * grid, (i + 1) * grid - spacing, (j + 1) * grid - spacing, paint);
					canvas.drawCircle((i + 0.5f) * grid - halfSpacing, (j + 0.5f) * grid - halfSpacing, radius, paint);
				}
			}

			return new BitmapDrawable(mResources, preview);
		}
	}
}

@@ -4058,17 +4058,17 @@
 ls + i) 
-+
+%7C
  0xff000

fix the way the alpha channel is set