package com.xjd.wolfpuzzle;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

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

	public static class ViewHolder extends RecyclerView.ViewHolder {
		public ViewHolder(View arg0) {
			super(arg0);
		}

		ImageView mImg;
	}

	public static final int[] gameImage = {
			R.drawable.b0, R.drawable.b1,R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5,R.drawable.b6, R.drawable.b7, R.drawable.b8, R.drawable.b9, 
			R.drawable.b10, R.drawable.b11, R.drawable.b12, R.drawable.b13, R.drawable.b14, R.drawable.b15, R.drawable.b16, R.drawable.b17, R.drawable.b18, R.drawable.b19, 
			R.drawable.b20, R.drawable.b21, R.drawable.b22, R.drawable.b23, R.drawable.b24, R.drawable.b25, R.drawable.b26, R.drawable.b27, R.drawable.b28, R.drawable.b29, 
			R.drawable.b30, R.drawable.b31, R.drawable.b32, R.drawable.b33, R.drawable.b34, R.drawable.b35, R.drawable.b36, R.drawable.b37, R.drawable.b38, R.drawable.b39,
			R.drawable.b40, R.drawable.b41, R.drawable.b42, R.drawable.b43, R.drawable.b44, R.drawable.b45, R.drawable.b46, R.drawable.b47, R.drawable.b48, R.drawable.b49, 
			R.drawable.b50, R.drawable.b51,
	};

	private static int length = gameImage.length/2;
	private Context mcontext;
	private LayoutInflater mInflater;
	private int count;
	public static int levelNum;

	public static int imageIndex;

	MyAdapter(Context context, int num) {
		mcontext = context;
		mInflater = LayoutInflater.from(context);
		count = num;
	}

	@Override
	public int getItemCount() {
		// TODO 自动生成的方法存根
		return length;
	}

	@Override
	public void onBindViewHolder(ViewHolder viewHolder, final int arg1) {
		// TODO 自动生成的方法存根
		if (getMissionVer(mcontext, "mission") < arg1 + count*length) {
			viewHolder.mImg.setImageResource(R.drawable.lock);
		} else {
			viewHolder.mImg.setImageResource(gameImage[arg1 + count*length]);		
		}
		viewHolder.itemView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(getMissionVer(mcontext, "mission") < arg1){
					return;
				}
				// TODO 自动生成的方法存根
				new AlertDialog.Builder(mcontext)
						.setTitle("请选择难度")
						.setPositiveButton("简单",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(
											DialogInterface dialog,
											int which) {
										// TODO 自动生成的方法存根
										selecGame(3, arg1);
									}

								})
						.setNeutralButton("普通",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(
											DialogInterface arg0, int which) {
										// TODO Auto-generated method stub
										selecGame(4, arg1);
									}
								})
						.setNegativeButton("困难",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(
											DialogInterface arg0, int which) {
										// TODO Auto-generated method stub
										selecGame(5, arg1);
									}

								}).show();
			}
		});

	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		// TODO 自动生成的方法存根
		View view = mInflater.inflate(R.layout.select_item, arg0, false);
		ViewHolder viewHolder = new ViewHolder(view);

		viewHolder.mImg = (ImageView) view
				.findViewById(R.id.id_index_gallery_item_image);
		return viewHolder;
	}

	private void selecGame(int i, int position) {
		// TODO 自动生成的方法存根
		levelNum = i;
		imageIndex = position + count*length;
		Intent intent = new Intent(mcontext, GameActivity.class);
		intent.putExtra("image", gameImage[position]);
		mcontext.startActivity(intent);
	}

	private int getMissionVer(Context context, String key) {
		int value = 0;
		SharedPreferences perf = context.getSharedPreferences("mission", 1);
		value = perf.getInt(key, 0);
		return value;
	}

	private void setMissionVer(Context context, String key, int value) {
		SharedPreferences perf = context.getSharedPreferences("mission", 1);
		Editor editor = perf.edit();
		editor.putInt(key, value);
		editor.commit();
	}
}
