package fishrungames.bashgid;


import java.util.Locale;

import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.HtmlDownloadManager.TextFileRecord;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.db.NewsDataSource;
import fishrungames.networkutils.ContextHolder;
import fishrungames.networkutils.ImageManager;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;

import android.content.res.Configuration;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import android.support.v4.widget.DrawerLayout;

public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks
{

	public static final String TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT = "TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT";
	public static final String TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT = "TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT";
	public static final String TAG_FROM_SEARCHFRAGMENT_TO_NEWSRECORDFRAGMENT = "TAG_FROM_SEARCHFRAGMENT_TO_NEWSRECORDFRAGMENT";
	public static final String TAG_TO_WELCOMEFRAGMENT = "WelcomeFragment";
	public static final String TAG_TO_SEARCHFRAGMENT = "SearchFragment";
	public static final String TAG_TO_MAINFRAGMENT = "MainFragment";
	public static final String TAG_TO_MAPFRAGMENT = "MapFragment";
	

	
	
	static MainActivity instance = null;

	private NavigationDrawerFragment mNavigationDrawerFragment = null;

	public NewsDataSource newsDataSource = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		SetupDrawer();
		
		ImageManager.getInstance().alwaysUpdateImagesWithoutHash = false;

		getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new WelcomeFragment(), TAG_TO_WELCOMEFRAGMENT).commit();

		instance = this;
		ContextHolder.setContext(instance);
	
		newsDataSource = new NewsDataSource(this);

		newsDataSource.open();
		
	}

	public static MainActivity getInstance()
	{
		return instance;

	}

	public void OnSelectEnglish(View view)
	{
		Locale.setDefault(Locale.US);
		Configuration config = new Configuration();
		config.locale = Locale.US;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

		getSupportActionBar().setTitle(R.string.app_name);

		SetupDrawer();

		OpenMainScreen();
	}

	public void OnSelectRussian(View view)
	{
		Locale locale = new Locale("ru");
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

		getSupportActionBar().setTitle(R.string.app_name);
		SetupDrawer();

		OpenMainScreen();
	}

	public void OnSelectChinese(View view)
	{
		Locale.setDefault(Locale.CHINA);
		Configuration config = new Configuration();
		config.locale = Locale.CHINA;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

		getSupportActionBar().setTitle(R.string.app_name);
		SetupDrawer();

		OpenMainScreen();
	}

	public void OnSelectSemiDetails(Integer pos)
	{
		// getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
		// new SemiDetailsFragment()).addToBackStack(null).commit();
	}

	public void OpenMainScreen()
	{

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MainPageFragment()).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenNewsScreen()
	{

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NewsListFragment()).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenSettingsScreen()
	{

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSearchScreen()
	{
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SearchFragment(), TAG_TO_SEARCHFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenSearchScreen(boolean restorePreviousSearch)
	{
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SearchFragment(restorePreviousSearch), TAG_TO_SEARCHFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();

	}
	
	public void OpenLinkContentScreen(TextFileRecord contentRecord)
	{
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new LinkContentFragment(contentRecord), TAG_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
		
	public void OpenMapScreen()
	{
		//getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapHolderFragment(), TAG_TO_MAINFRAGMENT).commit();
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new OsmMapHolderFragment(), TAG_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenStreetViewScreen(double lat, double lon)
	{
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new StreetViewHolderFragment(lat, lon), TAG_TO_MAPFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenPhotoAlbumListScreen()
	{
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new PhotoAlbumListFragment(), TAG_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenPhotoAlbumScreen(AlbumManager.AlbumStruct album)
	{
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new PhotoAlbumFragment(album), TAG_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	
	public void OpenNewsRecordScreen(NewsRecord newsRecord, String tag)
	{
		// Xperimental -- addToBackStack provoke error "Class not found". Need
		// to resolve somehow!
		// getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
		// new NewsRecordFragment(newsRecord)).addToBackStack(null).commit();

		// I added workaround:

		NewsRecordFragment newsRecordFragment = new NewsRecordFragment(newsRecord);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newsRecordFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenHtmlViewerScreen(String url)
	{
		HtmlViewerFragment htmlViewerFragment = new HtmlViewerFragment();
		htmlViewerFragment.url = url;
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, htmlViewerFragment, TAG_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	@Override
	public void onAttachFragment(Fragment fragment)
	{
		super.onAttachFragment(fragment);

	}

	public void SetupDrawer()
	{

		mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

		if (mNavigationDrawerFragment != null)
		{

			// Set up the drawer.
			mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.global, menu);		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		
		if (id == R.id.action_search)
		{
			if (getSupportFragmentManager().findFragmentByTag(TAG_TO_SEARCHFRAGMENT) == null
				&& getSupportFragmentManager().findFragmentByTag(TAG_TO_WELCOMEFRAGMENT) == null)
			{
				OpenSearchScreen();
			}
			return true;
		}
		
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onNavigationDrawerItemSelected(int position)
	{
		if (position == 1)
		{
			OpenMainScreen();
		}
		else if (position == 2)
		{
			OpenNewsScreen();
		}
		else if (position == 6)
		{
			OpenPhotoAlbumListScreen();
		}
		else if (position == 4)
		{
			OpenMapScreen();
			//OpenStreetViewScreen();
		}
		else if (position == 8)
		{
			OpenSettingsScreen();
		}
	}

	@Override
	public void onBackPressed()
	{

		if (getSupportFragmentManager().findFragmentByTag(TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT) != null)
		{
			OpenMainScreen();

			return;
		}

		if (getSupportFragmentManager().findFragmentByTag(TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT) != null)
		{
			OpenNewsScreen();

			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_FROM_SEARCHFRAGMENT_TO_NEWSRECORDFRAGMENT) != null)
		{
			OpenSearchScreen(true);

			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_TO_SEARCHFRAGMENT) != null)
		{
			OpenMainScreen();

			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_TO_MAINFRAGMENT) != null)
		{
			OpenMainScreen();

			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_TO_MAPFRAGMENT) != null)
		{
			OpenMapScreen();

			return;
		}
		

		
	
		super.onBackPressed();

		/*
		 * AddToBackStack is falling. Here is workaround
		 * if(getSupportFragmentManager().getBackStackEntryCount() != 0) {
		 * getSupportFragmentManager().popBackStack(); } else {
		 * super.onBackPressed(); }
		 */
	}

	@Override
	protected void onResume()
	{
		newsDataSource.open();		
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		newsDataSource.close();
		
		super.onPause();
	}
	
	


}
