package lib.morkim.mfw.ui;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.UUID;

import lib.morkim.mfw.app.MorkimApp;
import lib.morkim.mfw.domain.Model;

public abstract class MorkimFragment<A extends MorkimApp<M, ?>, M extends Model, V extends UpdateListener, C extends Controller, P extends Presenter>
        extends Fragment
        implements Viewable<A, M, V, C, P> {

    private C controller;
    private P presenter;

    private UUID id;

    protected int layoutId() {
        return 0;
    }

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

        id = (savedInstanceState == null) ? UUID.randomUUID() : UUID.fromString(savedInstanceState.getString(VIEWABLE_ID));

        getMorkimContext().acquireController(this);
    }

    @Override
    public Bundle getBundledData() {
        return getArguments();
    }

    @Override
    public void attachController(C controller) {

        presenter = createPresenter();

        this.controller = controller;
        this.controller.attachViewable(this);
        this.presenter.setController(this.controller);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putString(VIEWABLE_ID, id.toString());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(layoutId(), container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        controller.bindViews();
    }

    @Override
    public void onStop() {
        super.onStop();

        controller.unbindViews();
    }

    @Override
    public A getMorkimContext() {
        //noinspection unchecked
        return (A) getActivity().getApplication();
    }

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

    @Override
    public C getController() {
        return controller;
    }

    @Override
    public void keepScreenOn(boolean keepOn) {
        ((Screen) getActivity()).keepScreenOn(keepOn);
    }

    @Override
    public UUID getInstanceId() {
        return id;
    }
}

@@ -111,101 +111,8 @@
 dle;
-%0Aimport android.view.LayoutInflater;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;
 %0A%0Aim
@@ -440,16 +440,40 @@
 private 
+UUID id;%0A%0A    protected 
 C contro
@@ -484,21 +484,23 @@
 ;%0A    pr
-ivate
+otected
  P prese
@@ -506,38 +506,16 @@
 enter;%0A%0A
-    private UUID id;%0A%0A
     prot
@@ -1047,25 +1047,24 @@
 resenter();%0A
-%0A
         this
@@ -1085,24 +1085,25 @@
 controller;%0A
+%0A
         this
@@ -1173,21 +1173,16 @@
 troller(
-this.
 controll
@@ -1222,340 +1222,358 @@
 lic 
-void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A%0A        outState.putString(VIEWABLE_ID, id.toString());%0A    %7D%0A%0A    @Override%0A    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) %7B%0A        return inflater.inflate(layoutId(), container, false)
+A getMorkimContext() %7B%0A        //noinspection unchecked%0A        return (A) getActivity().getApplication();%0A    %7D%0A%0A    @Override%0A    public Context getContext() %7B%0A        return getActivity();%0A    %7D%0A%0A    @Override%0A    public C getController() %7B%0A        return controller;%0A    %7D%0A%0A    @Override%0A    public V getUpdateListener() %7B%0A        return (V) this
 ;%0A  
@@ -1820,114 +1820,39 @@
 lic 
-A getMorkimContext() %7B%0A        //noinspection unchecked%0A        return (A) getActivity().getApplication();
+void keepScreenOn(boolean b) %7B%0A
 %0A   
@@ -1880,34 +1880,34 @@
  public 
-Context getContext
+UUID getInstanceId
 () %7B%0A   
@@ -1914,37 +1914,26 @@
      return 
-getActivity()
+id
 ;%0A    %7D%0A%0A   
@@ -1958,250 +1958,284 @@
 lic 
-C getController() %7B%0A        return controller;%0A    %7D%0A%0A    @Override%0A    public void keepScreenOn(boolean keepOn) %7B%0A        ((Screen) getActivity()).keepScreenOn(keepOn
+void registerPermissionListener(String s, onPermissionResultListener onPermissionResultListener) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState
 );%0A
+%0A
     
-%7D%0A%0A
     
-@Override%0A    public UUID getInstanceId() %7B%0A        return id
+outState.putString(VIEWABLE_ID, id.toString())
 ;%0A  

refactor morkim fragment