package lib.morkim.mfw.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import lib.morkim.mfw.R;
import lib.morkim.mfw.app.MorkimApp;

public abstract class Screen extends AppCompatActivity implements Viewable {

	public static final String KEY_SCREEN_TRANSITION = "screen.transition";
	
	private static final String TAG_CONTROLLER_FRAGMENT = "controller.fragment.tag";

	protected Navigation navigation;
	protected Controller controller;
	protected Presenter presenter;

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

		setCustomTilteBar();

		int layoutId = layoutId();
		if (layoutId > 0)
			setContentView(layoutId);

		controller = ((MorkimApp) getApplication()).acquireController(this);
		presenter = ((MorkimApp) getApplication()).acquirePresenter(this);

		int transitionOrdinal = getIntent().getIntExtra(KEY_SCREEN_TRANSITION, Transition.NONE.ordinal());
		Transition transition = Transition.values()[transitionOrdinal];
		animateTransition(transition);
	}
	
	protected abstract Controller createController();
	protected abstract Presenter createPresenter();

	protected int layoutId() {
		return 0;
	}

	private void animateTransition(Transition transition) {
		switch (transition) {
		case RIGHT:
			overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
			break;
		case LEFT:
			overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
			break;
		default:
			break;
		}
	}

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

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

		keepScreenOn(false);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (isFinishing()) {
			((MorkimApp) getApplication()).destroyController(this);
			((MorkimApp) getApplication()).destroyPresenter(this);
		}
	}

	protected void setCustomTilteBar() {

	}

	@Override
	public void keepScreenOn(boolean keepOn) {

		if (keepOn) 
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		else
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}
	
	public Controller getController() {
		return controller;
	}

	@Override
	public Context getContext() {
		return this;
	}
}

@@ -497,26 +497,25 @@
 gation;%0A
+%0A
 %09pr
-otected
+ivate
  Control
@@ -533,23 +533,21 @@
 ler;%0A%09pr
-otec
+iva
 te
-d
  Present
@@ -1115,110 +1115,8 @@
 ;%0A%09%7D
-%0A%09%0A%09protected abstract Controller createController();%0A%09protected abstract Presenter createPresenter();
 %0A%0A%09p
@@ -2095,17 +2095,27 @@
 ON);%0A%09%7D%0A
-%09
+%0A%09@Override
 %0A%09public
@@ -2111,24 +2111,77 @@
 ide%0A%09public 
+Context getContext() %7B%0A%09%09return this;%0A%09%7D%0A%0A%09protected 
 Controller g
@@ -2227,44 +2227,40 @@
 %7D%0A%0A%09
-@Override%0A%09public Context
+protected Presenter
  get
-Context
+Presenter
 () %7B
@@ -2265,23 +2265,28 @@
  %7B%0A%09%09return 
-this
+presenter
 ;%0A%09%7D%0A%7D%0A

Remove view model in presenter