package com.smart.gtstrvl;

import com.example.mountaintravel.base.BaseActivity;
import com.example.mountaintravel.fragment.JoinFragmentPage;
import com.example.mountaintravel.fragment.MyCreateFragment;
import com.example.mountaintravel.view.TitleBarView;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;

import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;

public class GameListActivity extends BaseActivity implements OnClickListener {
	private TextView gamelist_tv1;
	private TextView gamelist_tv2;
	private FrameLayout frameLayout;
	@SuppressLint("NewApi")
	private FragmentManager fm = this.getFragmentManager();
	private FragmentTransaction ft;
	private JoinFragmentPage fragmentPage1;
	private MyCreateFragment fragmentPage2;
	private static final String TAG = "GameListActivity";
	private SharedPreferences sp;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game_list);
		initTitleBar("我的赛事安排");
		sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
		gamelist_tv1 = (TextView) findViewById(R.id.gamelist_tvadddown);
		gamelist_tv2 = (TextView) findViewById(R.id.gamelist_tvcreatup);
		gamelist_tv1.setOnClickListener(this);
		gamelist_tv2.setOnClickListener(this);
		frameLayout = (FrameLayout) findViewById(R.id.fl_content);
		ft = fm.beginTransaction();
		home();
		ft.commit();
	}

	public void initTitleBar(String text) {
		TitleBarView v = (TitleBarView) findViewById(R.id.titleBarView1);
		if (v != null) {
			v.initTitleBar(R.drawable.zuoback, -1, text,
					new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if (v.getId() == R.id.left_icon) {
								if (sp.getBoolean("apply", false)
										|| sp.getBoolean("virtualPinfo", false)) {
									sp.edit().putBoolean("apply", false)
											.commit();
									sp.edit().putBoolean("virtualPinfo", false)
											.commit();
									startActivity(MainActivity.class);
									return;
								}
								finish();
							}
						}
					});
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 创建退出对话框
			if (sp.getBoolean("apply", false)
					|| sp.getBoolean("virtualPinfo", false)) {
				sp.edit().putBoolean("apply", false).commit();
				sp.edit().putBoolean("virtualPinfo", false).commit();
				startActivity(MainActivity.class);
				return true;
			}
			finish();
		}

		return false;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game_list, menu);
		return true;
	}

	@SuppressLint("NewApi")
	private void home() {
		if (fragmentPage1 == null) {
			fragmentPage1 = new JoinFragmentPage(this);
			ft.add(R.id.fl_content, fragmentPage1);
		} else {
			ft.show(fragmentPage1);

		}
	}

	@SuppressLint("NewApi")
	private void friend() {
		if (fragmentPage2 == null) {
			fragmentPage2 = new MyCreateFragment(this);

			ft.add(R.id.fl_content, fragmentPage2);

		} else {
			ft.show(fragmentPage2);

		}
	}

	@SuppressLint("NewApi")
	private void setSelected() {
		// TODO Auto-generated method stub
		if (fragmentPage1 != null) {
			ft.remove(fragmentPage1);
			fragmentPage1 = null;
		}
		if (fragmentPage2 != null) {
			ft.remove(fragmentPage2);
			fragmentPage2 = null;
		}

	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ft = fm.beginTransaction();
		setSelected();
		switch (v.getId()) {

		case R.id.gamelist_tvadddown:

			gamelist_tv1.setBackground(getResources().getDrawable(
					R.drawable.gamelist_pressed));
			gamelist_tv2.setBackground(getResources().getDrawable(
					R.drawable.gamelist_default));

			home();
			break;
		case R.id.gamelist_tvcreatup:
			gamelist_tv1.setBackground(getResources().getDrawable(
					R.drawable.gamelist_default));
			gamelist_tv2.setBackground(getResources().getDrawable(
					R.drawable.gamelist_pressed));
			friend();
			break;

		}
		ft.commit();
	}

}
