package com.fxc.translator.ui.activity;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.baidu.mobstat.StatService;
import com.fxc.translator.R;
import com.fxc.translator.ui.fragment.DailyFragment;
import com.fxc.translator.ui.fragment.TranslateFragment;
import com.fxc.translator.utils.PublicMethod;

/**
*
*/
public class MainActivity extends BaseActivity
		implements NavigationView.OnNavigationItemSelectedListener {

	private NavigationView mNavigationView;
	private Toolbar mToolbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		StatService.start(this);
		Log.d("fxc0719", "哈哈");

		initView();
		addFragment(new TranslateFragment());
	}

	private void addFragment(Fragment fragment) {
		getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).commit();
	}

	private void initView() {
		mToolbar = (Toolbar) findViewById(R.id.toolbar);
		setSupportActionBar(mToolbar);

		DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
		ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(MainActivity.this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
		drawer.setDrawerListener(toggle);
		toggle.syncState();

		mNavigationView = (NavigationView) findViewById(R.id.nav_view);
		mNavigationView.setNavigationItemSelectedListener(this);
	}

	@Override
	public void onBackPressed() {
		DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
		if (drawer.isDrawerOpen(GravityCompat.START)) {
			drawer.closeDrawer(GravityCompat.START);
		} else {
			super.onBackPressed();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		if (id == R.id.action_settings) {
			startActivity(MainActivity.this, AboutActivity.class);
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@SuppressWarnings("StatementWithEmptyBody")
	@Override
	public boolean onNavigationItemSelected(MenuItem item) {
		int id = item.getItemId();

		switch (id) {
			case R.id.nav_translate:
				mToolbar.setTitle("丑媳妇翻译");
				getSupportFragmentManager().beginTransaction().replace(R.id.content_main, new TranslateFragment()).commit();
				break;
			case R.id.nav_chinese:
				PublicMethod.showDialog(MainActivity.this, "", "开发中，请稍后");
				// getSupportFragmentManager().beginTransaction().replace(R.id.content_main, new XHFragment()).commit();
				break;
			case R.id.nav_daily:
				// showDialog();
				mToolbar.setTitle("每日一句");
				getSupportFragmentManager().beginTransaction().replace(R.id.content_main, new DailyFragment()).commit();
				break;
			case R.id.nav_option:
				// showDialog();
				startActivity(MainActivity.this, SettingActivity.class);
				break;
			case R.id.nav_about:
				// showDialog();
				startActivity(MainActivity.this, AboutActivity.class);
				break;
		}

		DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
		drawer.closeDrawer(GravityCompat.START);
		return true;
	}

}
