package lib.morkim.mfw.ui;

import java.util.Observable;
import java.util.Observer;

import lib.morkim.mfw.app.AppContext;
import lib.morkim.mfw.app.MorkimApp;
import lib.morkim.mfw.domain.Model;
import lib.morkim.mfw.usecase.UseCase;
import lib.morkim.mfw.usecase.UseCaseProgress;
import lib.morkim.mfw.usecase.UseCaseResult;
import lib.morkim.mfw.usecase.UseCaseStateListener;

public abstract class Presenter implements Observer, UseCaseStateListener {

	private AppContext appContext;
	protected Viewable viewable;

	private ViewModel viewModel;

	public Presenter(Viewable viewable) {
		
		this.viewable = viewable;
		viewModel = new ViewModel();
	}
	
	void initialize(AppContext appContext) {

		this.appContext = appContext;

		initializeViewModel(viewModel);
	}

	protected abstract void initializeViewModel(ViewModel viewModel);

	public void unbindViewModel() {

		viewModel.unregister();
	}

	public void bindViewModel(Viewable viewable) {
		
		viewModel.register(viewable);
		viewModel.notifyView();
	}

	@Override
	public void update(Observable observable, Object data) {
		synchronized (this) {
			onUpdate(observable, data, viewModel);
			if (viewModel.hasListener())
				viewModel.notifyUiView();
		}
	}

	protected void onUpdate(Observable observable, Object data,
			ViewModel viewModel) {
	}

	public ViewModel getViewModel() {
		return viewModel;
	}

	public void setViewModel(ViewModel viewModel) {
		this.viewModel = viewModel;
	}

	public AppContext getAppContext() {
		return appContext;
	}

	public void setAppContext(AppContext appContext) {
		this.appContext = appContext;
	}

	protected String string(int resource) {
		return ((MorkimApp) appContext).getString(resource);
	}

	protected String string(int resource, Object ... args) {
		return ((MorkimApp) appContext).getString(resource, args);
	}
	
	protected Model getModel() {
		return appContext.getModel();
	}

	@Override
	public void onUseCaseStart(UseCase useCase) {}

	@Override
	public void onUseCaseUpdate(UseCaseProgress response) {}

	@Override
	public void onUseCaseComplete(UseCaseResult response) {}

	@Override
	public void onUseCaseCancel() {}
}
@@ -517,39 +517,8 @@
 e;%0A%0A
-%09private ViewModel viewModel;%0A%0A
 %09pub
@@ -583,16 +583,18 @@
 ewable;%0A
+//
 %09%09viewMo
@@ -697,16 +697,18 @@
 ntext;%0A%0A
+//
 %09%09initia
@@ -839,16 +839,18 @@
 el() %7B%0A%0A
+//
 %09%09viewMo
@@ -919,19 +919,19 @@
 able) %7B%0A
-%09%09%0A
+%0A//
 %09%09viewMo
@@ -954,16 +954,18 @@
 wable);%0A
+//
 %09%09viewMo
@@ -1079,16 +1079,18 @@
 this) %7B%0A
+//
 %09%09%09onUpd
@@ -1123,16 +1123,18 @@
 Model);%0A
+//
 %09%09%09if (v
@@ -1157,16 +1157,18 @@
 ener())%0A
+//
 %09%09%09%09view
@@ -1284,24 +1284,26 @@
 odel) %7B%0A%09%7D%0A%0A
+//
 %09public View
@@ -1317,32 +1317,34 @@
 etViewModel() %7B%0A
+//
 %09%09return viewMod
@@ -1339,36 +1339,42 @@
 turn viewModel;%0A
+//
 %09%7D%0A
-%0A
+//%0A//
 %09public void set
@@ -1402,24 +1402,26 @@
 iewModel) %7B%0A
+//
 %09%09this.viewM
@@ -1438,16 +1438,18 @@
 wModel;%0A
+//
 %09%7D%0A%0A%09pub

Remove view model in presenter