package com.xjd.wolfpuzzle;


import org.json.JSONObject;

import com.baidu.mobads.AdSettings;
import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SelectActivity extends Activity{
	public static int levelNum = 3;
	public static int imageIndex;
	AdView adView;
	private MyAdapter adapter0;
	private MyAdapter adapter1;
	TextView teView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select);
		RelativeLayout your_original_layout = (RelativeLayout)findViewById(R.id.activity_select);

		RecyclerView gallery = (RecyclerView) findViewById(R.id.gallery);
		RecyclerView gallery2 = (RecyclerView) findViewById(R.id.gallery2);
		teView = (TextView)findViewById(R.id.adtext_info);
		Button back = (Button)findViewById(R.id.back);
		adapter0 = new MyAdapter(SelectActivity.this, 0);
		
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);  
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);  
        gallery.setLayoutManager(linearLayoutManager);  
		gallery.setAdapter(adapter0);
		
		adapter1 = new MyAdapter(SelectActivity.this, 1);

        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this);  
        linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);  
        gallery2.setLayoutManager(linearLayoutManager2);  
		gallery2.setAdapter(adapter1);
//		gallery.setSelection(3);
		
		//人群属性
		AdSettings.setKey(new String[]{"baidu","中国"});
		//创建广告view
		String adPlaceID = "2347784";//重要：请填上你的广告位ID
		adView = new AdView(this,adPlaceID);
		//设置监听器
		adView.setListener(new AdViewListener() {
			public void onAdSwitch() {
				Log.w("", "onAdSwitch");
			}
			public void onAdShow(JSONObject info) {
				// 广告已经渲染出来
				Log.w("xjd", "onAdShow " + info.toString());
			}
			public void onAdReady(AdView adView) {
				// 资源已经缓存完毕，还没有渲染出来
				Log.w("", "onAdReady " + adView);
			}
			public void onAdFailed(String reason) {
				Log.w("", "onAdFailed " + reason);
			}
			public void onAdClick(JSONObject info) {
				Log.w("xjd", "onAdClick " + info.toString());
				int value = getMissionVer(SelectActivity.this, "mission");
				setMissionVer(SelectActivity.this, "mission", value + 1);
				teView.setText(getString(R.string.adtext_info)+"LV"+value);
				teView.setVisibility(View.VISIBLE);
			}
			public void onVideoStart() {
				Log.w("", "onVideoStart");
			}
			public void onVideoFinish() {
				Log.w("", "onVideoFinish");
			}
		});	
		RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		your_original_layout.addView(adView,rllp);
		back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				finish();
			}
		});
//		adView.performClick()
		
	}
	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();
		adapter0.notifyDataSetChanged();
		adapter1.notifyDataSetChanged();

	}

	
	/*/
	private OnItemClickListener itemClickListtener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			imageIndex = position;
			// TODO 自动生成的方法存根
			new AlertDialog.Builder(SelectActivity.this)
			.setTitle("请选择难度")
			.setPositiveButton("简单", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO 自动生成的方法存根
					levelNum =3;
					selecGame(3);
				}

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

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					levelNum =4;

					selecGame(4);
				}
			}).setNegativeButton("困难", new OnClickListener() {

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

					selecGame(5);
				}
			}).show();
		}
		
	};
	

	private void selecGame(int num) {
		// TODO 自动生成的方法存根
	}
	//*/
	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();
	}
}
