package fishrungames.bashgid;

import java.util.ArrayList;

import java.util.Locale;

import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;
import fishrungames.bashgid.core.ArticleManager.ArticleFullData;
import fishrungames.bashgid.core.ArticleManager.ArticleShortData;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;

import fishrungames.bashgid.core.JournalManager.JournalRecordData;
import fishrungames.bashgid.core.JournalManager.JournalSemiFullData;

import fishrungames.bashgid.core.VideoManager.VideoRecordData;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;

import android.content.Context;
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.view.inputmethod.InputMethodManager;

import android.support.v4.widget.DrawerLayout;

public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks
{

	public static final String TAG_BACK_TO_MAINFRAGMENT = "TAG_BACK_TO_MAINFRAGMENT";
	public static final String TAG_BACK_TO_CATALOGFRAGMENT = "TAG_BACK_TO_CATALOGFRAGMENT";
	public static final String TAG_BACK_TO_ALBUMLISTFRAGMENT = "TAG_BACK_TO_ALBUMLISTFRAGMENT";
	
	public static final String TAG_BACK_TO_JOURNALFRAGMENT = "TAG_BACK_TO_JOURNALFRAGMENT";
	public static final String TAG_BACK_TO_ARTICLEFRAGMENT = "TAG_BACK_TO_ARTICLEFRAGMENT";
	public static final String TAG_BACK_TO_ALBUMFRAGMENT = "TAG_BACK_TO_ALBUMFRAGMENT";
	public static final String TAG_BACK_TO_CHANNELFRAGMENT = "TAG_BACK_TO_CHANNELFRAGMENT";
	
	public static final String TAG_BACK_TO_SEARCHFRAGMENT = "TAG_BACK_TO_SEARCHFRAGMENT";
	public static final String TAG_BACK_TO_MAPFRAGMENT = "TAG_BACK_TO_MAPFRAGMENT";
	
	public static final String TAG_BACK_TO_NEWSLISTFRAGMENT = "TAG_BACK_TO_NEWSLISTFRAGMENT";
	
	

	
	

	MainPageFragment mainPageFragment = null;
	NewsListFragment newsListFragment = null;

	AlbumListFragment albumListFragment = null;
	PhotoAlbumFragment photoAlbumFragment = null;
	PhotoFragment photoFragment = null;

	ArticleFragment articleFragment = null;
	JournalFragment journalFragment = null;

	VideoListFragment videoListFragment = null;
	VideoRecordFragment videoRecordFragment = null;
	OsmMapHolderFragment mapHolderFragment = null;

	SearchFragment searchFragment = null;

	CatalogFragment catalogFragment = null;

	static MainActivity instance = null;

	private NavigationDrawerFragment mNavigationDrawerFragment = null;
	
	private int postnotedDrawerSelection = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		SetLanguage(BashgidApplication.GetLastLanguage());

		setContentView(R.layout.activity_main);
		
		SetupDrawer();
		
		CreateSelectedFragment(postnotedDrawerSelection);

		instance = this;
		
	}
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mNavigationDrawerFragment.onConfigurationChanged(newConfig);
    }


	public static MainActivity getInstance()
	{
		return instance;

	}
	
	
	

	public void ClearAllFragmentLinks()
	{
		mainPageFragment = null;
		newsListFragment = null;

		albumListFragment = null;
		photoAlbumFragment = null;
		photoFragment = null;

		articleFragment = null;
		journalFragment = null;

		videoListFragment = null;
		videoRecordFragment = null;
		mapHolderFragment = null;

		searchFragment = null;
		catalogFragment = null;

		InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
	}
	
	public void UpdateOnImageDownloaded()
	{
		if (mainPageFragment != null)
		{
			mainPageFragment.RefreshAdapter();
		}
		
		if (albumListFragment != null)
		{
			albumListFragment.RefreshAdapter();
		}
		

		if (photoAlbumFragment != null)
		{
			photoAlbumFragment.RefreshAdapter();
		}
		
		if (photoFragment != null)
		{
			photoFragment.RefreshAdapter();
		}

		if (articleFragment != null)
		{
			articleFragment.RefreshAdapter();
		}
		
		if (journalFragment != null)
		{
			journalFragment.RefreshAdapter();
		}
		
		if (catalogFragment != null)
		{
			catalogFragment.RefreshAdapter();
		}
		
		if (searchFragment != null)
		{
			searchFragment.RefreshAdapter();
		}
		
		if (videoListFragment != null)
		{
			videoListFragment.RefreshAdapter();
		}
		
		if (videoRecordFragment != null)
		{
			videoRecordFragment.RefreshView();
		}
	}

	public void UpdateOnNewsDownloaded(Integer percent)
	{
		if (mainPageFragment != null)
		{
			mainPageFragment.OnNewsDownloadProgress(percent);
		}
		
		if (newsListFragment != null)
		{
			newsListFragment.OnNewsDownloadProgress(percent);
		}
	}

	public void UpdateOnDatabaseDownloaded(Integer percent)
	{
		BashgidApplication.SetDatabaseDownloadProgress(percent);
		
		if (mainPageFragment != null)
		{
			mainPageFragment.OnDatabaseDonwloadProgress(percent);
		}
		
		if (catalogFragment != null)
		{
			catalogFragment.OnDatabaseDonwloadProgress(percent);
		}
		
		if (albumListFragment != null)
		{
			albumListFragment.OnDatabaseDonwloadProgress(percent);
		}
		
		if (photoAlbumFragment != null)
		{
			photoAlbumFragment.OnDatabaseDonwloadProgress(percent);
		}
		
		if (videoListFragment != null)
		{
			videoListFragment.OnDatabaseDonwloadProgress(percent);
		}
		
	}

	public void UpdateOnAlbumLoaded(AlbumFullData album)
	{
		if (photoFragment != null)
		{
			photoFragment.SetAlbum(album);
		}

		if (photoAlbumFragment != null)
		{
			photoAlbumFragment.SetAlbum(album);
		}
	}

	public void UpdateOnArticleLoaded(ArticleFullData article)
	{
		if (articleFragment != null)
		{
			articleFragment.SetArticle(article);
		}

		if (mapHolderFragment != null)
		{
			mapHolderFragment.SetArticle(article);
		}
	}

	public void UpdateOnAlbumListLoaded(ArrayList<AlbumShortData> albumList)
	{
		if (albumListFragment != null)
		{
			albumListFragment.SetAlbumList(albumList);
		}
	}

	public void UpdateOnJournalLoaded(JournalSemiFullData journal)
	{
		if (journalFragment != null)
		{
			journalFragment.SetJournal(journal);
		}

		if (newsListFragment != null)
		{
			newsListFragment.SetJournal(journal);
		}

		if (mainPageFragment != null)
		{
			mainPageFragment.SetJournal(journal);
		}

		if (mapHolderFragment != null)
		{
			mapHolderFragment.SetJournal(journal);
		}

	}

	public void UpdateOnChannelLoaded(ChannelFullData channel)
	{

		if (videoListFragment != null)
		{
			videoListFragment.SetChannel(channel);
		}
	}

	public void UpdateOnVideoLoaded(VideoRecordData video)
	{

		if (videoRecordFragment != null)
		{
			videoRecordFragment.SetVideo(video);
		}
	}

	public void UpdateOnJournalListLoaded(ArrayList<JournalSemiFullData> journalList)
	{

		if (catalogFragment != null)
		{
			catalogFragment.SetJournalList(journalList);
		}

		if (mainPageFragment != null)
		{
			mainPageFragment.SetJournalList(journalList);
		}

	}

	public void UpdateOnGeoJournalListLoaded(ArrayList<JournalRecordData> journalList)
	{

		if (mapHolderFragment != null)
		{
			mapHolderFragment.SetGeoJournals(journalList);
		}

	}

	public void UpdateOnArticleSearchPerformed(ArrayList<ArticleShortData> articleArr)
	{

		if (searchFragment != null)
		{
			searchFragment.SetSearchResult(articleArr);
		}
	}

	public void OnSearchPressed()
	{
		if (searchFragment != null)
		{
			OpenSearchScreen(false);
			return;
		}

		if (journalFragment != null)
		{
			if (journalFragment.journal == null)
			{
				OpenSearchScreen(journalFragment.journalName, "");
			} else
			{
				OpenSearchScreen(journalFragment.journalName, journalFragment.journal.title);
			}
			return;
		}

		if (newsListFragment != null)
		{
			String newsTitle = getResources().getString(R.string.news_header);
			OpenSearchScreen("news", newsTitle);
			return;
		}

		OpenSearchScreen(false);
	}
	
	public void SetLanguage(int language)
	{
		Configuration config = new Configuration();
		
		if (language == 0)
		{
			Locale locale = new Locale("ru");
			Locale.setDefault(locale);
			config.locale = locale;
		}
		else if (language == 2)
		{
			Locale.setDefault(Locale.CHINA);
			config.locale = Locale.CHINA;
		}
		else
		{
			Locale.setDefault(Locale.US);
			config.locale = Locale.US;
		}
		
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

	}

	public void OnSelectEnglish(View view)
	{
		
		SetLanguage(1);
		BashgidApplication.SetLastLanguage(1);
		
		getSupportActionBar().setTitle(R.string.app_name);

		SetupDrawer();

		OpenMainScreen(false);
	}

	public void OnSelectRussian(View view)
	{
		SetLanguage(0);
		BashgidApplication.SetLastLanguage(0);
		getSupportActionBar().setTitle(R.string.app_name);
		SetupDrawer();

		OpenMainScreen(false);
	}

	public void OnSelectChinese(View view)
	{
		SetLanguage(2);
		BashgidApplication.SetLastLanguage(2);
		getSupportActionBar().setTitle(R.string.app_name);
		SetupDrawer();

		OpenMainScreen(false);
	}

	public void OpenMainScreen(boolean fromSavedInstanceState)
	{
		ClearAllFragmentLinks();

		mainPageFragment = new MainPageFragment();

		if (fromSavedInstanceState)
		{
			getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mainPageFragment).commit();
		}
		else
		{
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mainPageFragment).commit();
		}

		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenNewsScreen(boolean fromSavedInstanceState)
	{

		ClearAllFragmentLinks();

		newsListFragment = new NewsListFragment();
		
		if (fromSavedInstanceState)
		{
			getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, newsListFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		}
		else
		{
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newsListFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		}

		
		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenCatalogScreen(boolean fromSavedInstanceState)
	{

		ClearAllFragmentLinks();

		catalogFragment = new CatalogFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, catalogFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenJournalScreen(boolean fromSavedInstanceState, String journalName, String tag)
	{

		ClearAllFragmentLinks();

		journalFragment = new JournalFragment(journalName);

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

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSettingsScreen(boolean fromSavedInstanceState)
	{
		ClearAllFragmentLinks();
		
		
		if (fromSavedInstanceState)
		{
		getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new SettingsFragment()).commit();

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

		}
		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSearchScreen(boolean fromSavedInstanceState)
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSearchScreen(String query)
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment(query);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSearchScreen(String journalName, String journalTitle)
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment(journalName, journalTitle);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenSearchScreen(String query, String journalName, String journalTitle)
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment(query, journalName, journalTitle);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenMapScreen(boolean fromSavedInstanceState, String filter, String tag)
	{
		ClearAllFragmentLinks();

		mapHolderFragment = new OsmMapHolderFragment(0, filter);
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapHolderFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenMapScreen(ArticleFullData articleFullData, String tag)
	{
		ClearAllFragmentLinks();

		mapHolderFragment = new OsmMapHolderFragment(articleFullData);
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapHolderFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoAlbumListScreen(boolean fromSavedInstanceState)
	{
		ClearAllFragmentLinks();

		albumListFragment = new AlbumListFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, albumListFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoAlbumScreen(String albumName)
	{
		ClearAllFragmentLinks();

		photoAlbumFragment = new PhotoAlbumFragment(albumName);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, photoAlbumFragment, TAG_BACK_TO_ALBUMLISTFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoScreen(String albumName, int position, String tag)
	{
		ClearAllFragmentLinks();

		photoFragment = new PhotoFragment(albumName, position);

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

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoScreen(AlbumFullData album, int position, String tag)
	{
		ClearAllFragmentLinks();

		photoFragment = new PhotoFragment(album, position);

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

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenArticleScreen(String articleName, String tag)
	{

		ClearAllFragmentLinks();

		articleFragment = new ArticleFragment(articleName);

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

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenVideoListScreen(boolean fromSavedInstanceState, String channelName)
	{
		ClearAllFragmentLinks();

		videoListFragment = new VideoListFragment(channelName);
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, videoListFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenVideoRecordScreen(VideoRecordData video)
	{

		ClearAllFragmentLinks();

		videoRecordFragment = new VideoRecordFragment(video);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, videoRecordFragment, TAG_BACK_TO_CHANNELFRAGMENT).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)
		{
			OnSearchPressed();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onNavigationDrawerItemSelected(int position, boolean fromSavedInstanceState)
	{
		
		if (fromSavedInstanceState)
		{
			
			postnotedDrawerSelection = position;
			return;
		}
		
		if (position == 1)
		{
			OpenMainScreen(false);
		} else if (position == 2)
		{
			OpenNewsScreen(false);

		} else if (position == 3)
		{
			OpenCatalogScreen(false);
		} else if (position == 4)
		{
			OpenMapScreen(false, "map", MainActivity.TAG_BACK_TO_MAINFRAGMENT);
		} else if (position == 5)
		{
			OpenVideoListScreen(false, "main");
		} else if (position == 6)
		{
			OpenPhotoAlbumListScreen(false);
		} else if (position == 7)
		{
			OpenJournalScreen(false, "investor", MainActivity.TAG_BACK_TO_MAINFRAGMENT);
		} else if (position == 8)
		{
			OpenSettingsScreen(false);
		}
	}
	
	public void CreateSelectedFragment(int position)
	{
		if (position == 1)
		{
			OpenMainScreen(true);
		} else if (position == 2)
		{
			OpenNewsScreen(true);

		} else if (position == 3)
		{
			OpenCatalogScreen(true);
		} else if (position == 4)
		{
			OpenMapScreen(true, "map", MainActivity.TAG_BACK_TO_MAINFRAGMENT);
		} else if (position == 5)
		{
			OpenVideoListScreen(true, "main");
		} else if (position == 6)
		{
			OpenPhotoAlbumListScreen(true);
		} else if (position == 7)
		{
			OpenJournalScreen(true, "investor", MainActivity.TAG_BACK_TO_MAINFRAGMENT);
		} else if (position == 8)
		{
			OpenSettingsScreen(true);
		}
		else
		{
			if (true)
			{
				getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new WelcomeFragment(), null).commit();

			}
		}
	}

	@Override
	public void onBackPressed()
	{

	
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_MAPFRAGMENT) != null)
		{
			OpenMapScreen(false, BashgidApplication.GetLastMapFilterName(), TAG_BACK_TO_MAINFRAGMENT);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_NEWSLISTFRAGMENT) != null)
		{
			OpenNewsScreen(false);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_SEARCHFRAGMENT) != null)
		{
			OpenSearchScreen(BashgidApplication.GetLastSearchQuery(), BashgidApplication.GetLastJournalName(), BashgidApplication.GetLastJournalTitle());
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_JOURNALFRAGMENT) != null)
		{
			OpenJournalScreen(false, BashgidApplication.GetLastJournalName(), TAG_BACK_TO_MAINFRAGMENT);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_ARTICLEFRAGMENT) != null)
		{
			OpenArticleScreen(BashgidApplication.GetLastArticleName(), TAG_BACK_TO_MAINFRAGMENT);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_ALBUMFRAGMENT) != null)
		{
			OpenPhotoAlbumScreen(BashgidApplication.GetLastAlbumName());
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_ALBUMLISTFRAGMENT) != null)
		{
			OpenPhotoAlbumListScreen(false);
			return;
		}
		
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_CHANNELFRAGMENT) != null)
		{
			OpenVideoListScreen(false, BashgidApplication.GetLastChannelName());
			return;
		}		
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_CATALOGFRAGMENT) != null)
		{
			OpenCatalogScreen(false);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_MAINFRAGMENT) != null)
		{
			OpenMainScreen(false);
			return;
		}
		
		
		super.onBackPressed();

	}

	


	@Override
	protected void onResume()
	{

		super.onResume();
	}

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

}
