package com.andengine.blackdog.chapter4;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andengine.blackdog.R;
import com.andengine.blackdog.chapter4.helper.BaseHelper;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MainMenu4 extends BaseHelper {
	Intent intent = null;
	private EditText inputAddress = null;
	String text = null;
	String score;

	// Admob
	Handler handler = new Handler();

	private void loadBannerView() {
		new Thread(new Runnable() {

			public void run() {
				handler.post(new Runnable() {

					public void run() {
						LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
						AdView adView = new AdView(MainMenu4.this,
								AdSize.BANNER, "a151be1316d7644");
						layout.addView(adView);
						AdRequest request = new AdRequest();
						adView.loadAd(request);
					}
				});
			}
		}).start();
	}

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.gc();

		setContentView(R.layout.main_menu4);

		loadBannerView();

		// 이름 입력하기
		SharedPreferences pref = getSharedPreferences("pref",
				Activity.MODE_PRIVATE);
		inputAddress = (EditText) findViewById(R.id.inputAddr);

		// 저장된 값들을 불러옵니다.
		// 이름
		text = pref.getString("editText", "");
		inputAddress.setText(text);

		// 점수
		score = pref.getString("score", "");
		// 점수가 없을때는 표기 하지 않기 위해서
		if (score.trim().length() != 0) {
			score = score + " Meter";
		}
		TextView scoreTxt = (TextView) findViewById(R.id.score);
		scoreTxt.setText(score);

		// 게임시작 버튼
		findViewById(R.id.mark).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				intent = new Intent(getApplicationContext(),
						CharacterJump.class);
				startActivity(intent);
			}
		});

		// 기록 보기 버튼
		findViewById(R.id.book).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// todo:
				intent = new Intent(getApplicationContext(), WebForm.class);
				intent.putExtra("link", BaseHelper.URL + "rankView.php");
				startActivity(intent);
			}
		});

	}

	private void clearApplicationCache(java.io.File dir) {
		if (dir == null)
			dir = getCacheDir();
		else
			;
		if (dir == null)
			return;
		else
			;
		java.io.File[] children = dir.listFiles();
		try {
			for (int i = 0; i < children.length; i++)
				if (children[i].isDirectory())
					clearApplicationCache(children[i]);
				else
					children[i].delete();
		} catch (Exception e) {
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		clearApplicationCache(null);
	}

	// 어플리케이션이 화면에서 사라질때 여러 값을 저장 합니다.
	@Override
	public void onStop() {
		super.onStop();
		SharedPreferences pref = getSharedPreferences("pref",
				Activity.MODE_PRIVATE); // UI 상태를 저장합니다.
		SharedPreferences.Editor editor = pref.edit(); // Editor를 불러 옵니다.

		inputAddress = (EditText) findViewById(R.id.inputAddr);
		editor.putString("editText", inputAddress.getText().toString());
		editor.commit(); // 저장합니다.

	}

	/*
	 * finish를 통해 다시 진입할 때는 onResume() 함수에 들어오게 됩니다.
	 */
	@Override
	public void onResume() {
		super.onResume();
		SharedPreferences pref = getSharedPreferences("pref",
				Activity.MODE_PRIVATE);
		// 점수
		score = pref.getString("score", "");
		// 점수가 없을때는 표기 하지 않기 위해서
		if (score.trim().length() != 0) {
			score = score + " Meter";
		}
		TextView scoreTxt = (TextView) findViewById(R.id.score);
		scoreTxt.setText(score);

	}
}
