package de.nicidienase.chaosflix.touch.activities;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.transition.Slide;
import android.transition.TransitionInflater;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentConference;
import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentEvent;
import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentRecording;
import de.nicidienase.chaosflix.touch.ViewModelFactory;
import de.nicidienase.chaosflix.touch.fragments.ConferencesTabBrowseFragment;
import de.nicidienase.chaosflix.touch.fragments.EventDetailsFragment;
import de.nicidienase.chaosflix.touch.fragments.EventsListFragment;
import de.nicidienase.chaosflix.touch.viewmodels.BrowseViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by felix on 17.09.17.
 */

public class BrowseActivity extends AppCompatActivity implements
		ConferencesTabBrowseFragment.OnConferenceListFragmentInteractionListener,
		EventsListFragment.OnEventsListFragmentInteractionListener,
		EventDetailsFragment.OnEventDetailsFragmentInteractionListener {

	private static final String TAG = BrowseActivity.class.getSimpleName();
	CompositeDisposable mDisposables = new CompositeDisposable();
	private BrowseViewModel mViewModel;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_container_layout);

		mViewModel = ViewModelProviders.of(this, ViewModelFactory.INSTANCE).get(BrowseViewModel.class);

		if (savedInstanceState == null) {
			ConferencesTabBrowseFragment browseFragment
					= ConferencesTabBrowseFragment.newInstance(getNumColumns());
			FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
			ft.replace(R.id.fragment_container, browseFragment);
			ft.setReorderingAllowed(true);
			ft.commit();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		mDisposables.clear();
	}

	private int getNumColumns() {
		return getResources().getInteger(R.integer.num_columns);
	}

	@Override
	public void onConferenceSelected(long conferenceId) {
		EventsListFragment eventsListFragment = EventsListFragment.newInstance(conferenceId, getNumColumns());
		FragmentManager fm = getSupportFragmentManager();
		Fragment oldFragment = fm.findFragmentById(R.id.fragment_container);

		TransitionInflater transitionInflater = TransitionInflater.from(this);
		oldFragment.setExitTransition(
				transitionInflater.inflateTransition(android.R.transition.fade));
		eventsListFragment.setEnterTransition(
				transitionInflater.inflateTransition(android.R.transition.slide_right));

		Slide slideTransition = new Slide(Gravity.RIGHT);
		eventsListFragment.setEnterTransition(slideTransition);

		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fragment_container, eventsListFragment);
		ft.setReorderingAllowed(true);
		ft.addToBackStack(null);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		ft.commit();
	}

	@Override
	public void onEventSelected(PersistentEvent event, View v) {
		EventDetailsFragment detailsFragment = EventDetailsFragment.newInstance(event.getEventId());
		FragmentManager fm = getSupportFragmentManager();

		detailsFragment.setAllowEnterTransitionOverlap(true);
		detailsFragment.setAllowReturnTransitionOverlap(true);

		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fragment_container, detailsFragment);
		ft.addToBackStack(null);
		ft.setReorderingAllowed(true);

		View thumb = v.findViewById(R.id.imageView);
		ft.addSharedElement(thumb, ViewCompat.getTransitionName(thumb));

		ft.commit();
	}

	@Override
	public void onToolbarStateChange() {
		invalidateOptionsMenu();
	}

	@Override
	public void setActionbar(Toolbar toolbar) {
		setSupportActionBar(toolbar);
		toolbar.setTitle("");
	}

	@Override
	public void playItem(PersistentEvent event, PersistentRecording recording) {
		Intent i = new Intent(this, PlayerActivity.class);
		i.putExtra(PlayerActivity.EVENT_KEY, event);
		i.putExtra(PlayerActivity.RECORDING_KEY, recording);
		startActivity(i);
	}

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

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.action_about:
				showAboutPage();
				return true;
			case R.id.action_update_database:
//				mViewModel.updateDatabase();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	private void showAboutPage() {
		Intent intent = new Intent(this, AboutActivity.class);
		startActivity(intent);
	}
}

@@ -698,100 +698,8 @@
 .R;%0A
-import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentConference;%0A
 impo
@@ -1136,32 +1136,95 @@
 tsListFragment;%0A
+import de.nicidienase.chaosflix.touch.sync.DownloadJobService;%0A
 import de.nicidi
@@ -1276,161 +1276,8 @@
 del;
-%0Aimport io.reactivex.android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.disposables.CompositeDisposable;%0A%0A/**%0A * Created by felix on 17.09.17.%0A */
 %0A%0Apu
@@ -1622,71 +1622,8 @@
 ();%0A
-%09CompositeDisposable mDisposables = new CompositeDisposable();%0A
 %09pri
@@ -1643,18 +1643,17 @@
 ewModel 
-mV
+v
 iewModel
@@ -1827,107 +1827,8 @@
 );%0A%0A
-%09%09mViewModel = ViewModelProviders.of(this, ViewModelFactory.INSTANCE).get(BrowseViewModel.class);%0A%0A
 %09%09if
@@ -2162,89 +2162,103 @@
 %09%09%7D%0A
-%09%7D%0A%0A%09@Override%0A%09protected void onStop() %7B%0A%09%09super.onStop();%0A%09%09mDisposables.clear(
+%0A%09%09viewModel = ViewModelProviders.of(this, ViewModelFactory.INSTANCE).get(BrowseViewModel.class
 );%0A%09
@@ -4742,39 +4742,245 @@
 se:%0A
-//
 %09%09%09%09
-mViewModel.updateDatabase
+Intent i = new Intent(this, DownloadJobService.class);%0A%09%09%09%09i.putExtra(DownloadJobService.Companion.getENTITY_KEY(),%0A%09%09%09%09%09%09DownloadJobService.Companion.getENTITY_KEY_EVERYTHING());%0A%09%09%09%09startService(i);%0A//%09%09%09%09viewModel.updateEverything
 ();%0A

use syncService