package com.smart.gtstrvl;

import com.example.mountaintravel.adapter.TeamListAdapter;
import com.example.mountaintravel.base.BaseActivity;
import com.example.mountaintravel.fragment.ApplyFrag;
import com.example.mountaintravel.fragment.CreateFrag;
import com.example.mountaintravel.fragment.JoinFragmentPage;
import com.example.mountaintravel.fragment.MyCreateFragment;
import com.example.mountaintravel.kind.TeamListInfo;
import com.example.mountaintravel.view.TitleBarView;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ApplyActivity extends BaseActivity implements OnClickListener {
	private TextView apply_creatteam;
	private TextView apply_team;
	private ListView apply_teamlist;
	private LinearLayout apply_layout;
	private FrameLayout frameLayout;
	private FragmentTransaction ft;
	private ApplyFrag applyFrag;
	private CreateFrag createFrag;
	@SuppressLint("NewApi")
	private FragmentManager fm = this.getFragmentManager();
	private SharedPreferences sp;
	public static String eventId="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apply);
		sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
		initTitleBar("申请团队");
		Intent intent = getIntent();
		eventId = intent.getStringExtra("eventId");
		findView();
	}

	@SuppressLint("NewApi")
	private void findView() {
		apply_layout = (LinearLayout) findViewById(R.id.apply_layout);
		apply_creatteam = (TextView) findViewById(R.id.apply_creatteam);
		apply_team = (TextView) findViewById(R.id.apply_team);
		apply_creatteam.setOnClickListener(this);
		apply_team.setOnClickListener(this);
		frameLayout = (FrameLayout) findViewById(R.id.fl_content);
		ft = fm.beginTransaction();
		apply_layout.setVisibility(View.VISIBLE);
		applyteam();
		ft.commit();
		// apply_creatteam = (TextView) findViewById(R.id.apply_creatteam);
		// apply_creatteam.setOnClickListener(this);
		// apply_teamlist = (ListView) findViewById(R.id.apply_teamlist);
		// apply_teamlist.setOnItemClickListener(listener);
		// adapter = new TeamListAdapter(this);
		// for (int i = 0; i < 20; i++) {
		// TeamListInfo info = new TeamListInfo(i + "A", "加入团队");
		// adapter.addData(info);
		// }
		// apply_teamlist.setAdapter(adapter);
		// adapter.notifyDataSetChanged();
	}

	@SuppressLint("NewApi")
	private void applyteam() {
		// TODO Auto-generated method stub

		if (applyFrag == null) {
			applyFrag = new ApplyFrag(this);
			ft.add(R.id.fl_content, applyFrag);
		} else {
			ft.show(applyFrag);

		}

	}

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

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

	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ft = fm.beginTransaction();
		setSelected();
		switch (v.getId()) {
		case R.id.apply_creatteam:
			apply_layout.setVisibility(View.INVISIBLE);
			apply_creatteam.setBackground(getResources().getDrawable(
					R.drawable.gamelist_pressed));
			apply_team.setBackground(getResources().getDrawable(
					R.drawable.gamelist_default));
			createteam();
			break;
		case R.id.apply_team:
			apply_layout.setVisibility(View.VISIBLE);
			apply_creatteam.setBackground(getResources().getDrawable(
					R.drawable.gamelist_default));
			apply_team.setBackground(getResources().getDrawable(
					R.drawable.gamelist_pressed));
			applyteam();
			break;
		}
		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.edit().putBoolean("apply", false)
											.commit();
									startActivity(EventInfoActivity.class);
									return;
								}
								finish();
							}
						}
					});
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

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

		return false;

	}

	@SuppressLint("NewApi")
	private void createteam() {
		// TODO Auto-generated method stub
		if (createFrag == null) {
			createFrag = new CreateFrag(this);

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

		} else {
			ft.show(createFrag);

		}
	}

}
