package com.txomon.openwrt.android;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.txomon.openwrt.rpc.UbusRpcException;
import com.txomon.rx.Events;

import java.util.Map;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


public class CustomCallFragment extends Fragment {
    private static final String TAG = "CustomCallFragment";


    public CustomCallFragment() {
        // Required empty public constructor
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final Activity main = getActivity();
        final View view = inflater.inflate(R.layout.fragment_custom_call, container, false);
        final EditText ubusMethod = (EditText) view.findViewById(R.id.customCallFragmentMethodText);
        final EditText ubusObject = (EditText) view.findViewById(R.id.customCallFragmentPathText);
        final Button sendButton = (Button) view.findViewById(R.id.customCallFragmentSendButton);

        final Observable<String> ubusObjectText = Events.text(ubusObject);
        final Observable<String> ubusMethodText = Events.text(ubusMethod);
        final Observable<Object> sendCallClick = Events.click(sendButton);

                //   .subscribeOn(AndroidSchedulers.mainThread())
                //   .observeOn(Schedulers.io())
        sendCallClick
                .observeOn(Schedulers.io())
                .map(new Func1<Object, Object>() {
                    @Override
                    public Object call(Object o) {
                        OnCustomCallFragmentInteractionListener activity = (OnCustomCallFragmentInteractionListener) main;
                        Object ret;
                        try {
                            ret = activity.makeUbusRpcClientCall(
                                    ubusObject.getText().toString(),
                                    ubusMethod.getText().toString(),
                                    null);
                        } catch (UbusRpcException e) {
                            return null;
                        }
                        return ret;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(((OnCustomCallFragmentInteractionListener) main).getCallResultObserver());
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            OnCustomCallFragmentInteractionListener mListener = (OnCustomCallFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnCustomCallFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public interface OnCustomCallFragmentInteractionListener {
        public Object makeUbusRpcClientCall(String method, String path, Map arguments) throws UbusRpcException;

        public Observer<Object> getCallResultObserver();
    }

}

@@ -125,16 +125,41 @@
 agment;%0A
+import android.util.Log;%0A
 import a
@@ -445,24 +445,46 @@
 x.Observer;%0A
+import rx.Subscriber;%0A
 import rx.an
@@ -1710,24 +1710,16 @@
 tton);%0A%0A
-        
         
@@ -1764,32 +1764,24 @@
 inThread())%0A
-        
         //  
@@ -1892,17 +1892,21 @@
        .
-m
+flatM
 ap(new F
@@ -1918,23 +1918,35 @@
 Object, 
+Observable%3C
 Object%3E
+%3E
 () %7B%0A   
@@ -1999,22 +1999,34 @@
  public 
+Observable%3C
 Object
+%3E
  call(Ob
@@ -2031,24 +2031,301 @@
 Object o) %7B%0A
+                        return Observable.create(%0A                                new Observable.OnSubscribe%3CObject%3E() %7B%0A                                    @Override%0A                                    public void call(Subscriber%3C? super Object%3E subscriber) %7B%0A                
             
@@ -2463,28 +2463,8 @@
     
-Object ret;%0A        
     
@@ -2472,32 +2472,95 @@
            try %7B
+%0A                                            subscriber.onNext(
 %0A               
@@ -2576,13 +2576,31 @@
     
-ret =
+                       
  act
@@ -2660,24 +2660,48 @@
             
+             
+           
 ubusObject.g
@@ -2753,24 +2753,48 @@
             
+              
+          
 ubusMethod.g
@@ -2854,15 +2854,56 @@
     
+                        
 null)
+)
 ;%0A
+                
     
@@ -2935,132 +2935,389 @@
 ch (
-UbusRpcException e) %7B%0A                            return null;%0A                        %7D%0A                        return ret;
+Throwable e) %7B%0A                                            Log.d(TAG,%22Catching throwable in Observable%22 );%0A                                            subscriber.onError(e);%0A                                        %7D%0A                                        subscriber.onCompleted();%0A                                    %7D%0A                                %7D%0A                        );%0A
 %0A   

rework finished, now trying to fix exceptions