package cn.bjtu.nourriture.fragments;

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

import cn.bjtu.nourriture.R;
import cn.bjtu.nourriture.api.NourritureService;
import cn.bjtu.nourriture.api.ServiceFactory;
import cn.bjtu.nourriture.model.Message;
import cn.bjtu.nourriture.model.Register;
import cn.bjtu.nourriture.model.User;
import retrofit.HttpException;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by sylflo on 12/9/15.
 */
public class RegisterFragment extends Fragment {

    private static final String TAG = "RegisterFragment";

    View inflatedView = null;
    EditText username = null;
    EditText email = null;
    EditText password = null;
    Button submit = null;

    public static RegisterFragment newInstance() {
        RegisterFragment fragment = new RegisterFragment();
        return fragment;
    }

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflatedView = inflater.inflate(R.layout.register_fragment, container, false);

        username = (EditText) inflatedView.findViewById(R.id.input_register_username);
        email = (EditText) inflatedView.findViewById(R.id.input_register_email);
        password = (EditText) inflatedView.findViewById(R.id.input_register_password);
        submit = (Button) inflatedView.findViewById(R.id.button_register);

        return this.inflatedView;
    }

    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        Log.e(getClass().toString(), "registerragment created");

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Register registerUser = new Register(username.getText().toString(),
                        password.getText().toString(), email.getText().toString());

                //Log.d(TAG, username.getText().toString());
                NourritureService service = ServiceFactory.createRetrofitService(NourritureService.class);
                Observable<Message> observable = service.registerUser(registerUser);

                observable
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<Message>() {
                            @Override
                            public void onCompleted() {
                                // handle completed
                                Log.d(TAG, " onCompleted");
                              /*  Toast.makeText(mContext, "Register success",
                                        Toast.LENGTH_LONG).show();*/
                            }

                            @Override
                            public void onError(Throwable e) {
                                Log.e(TAG, "onError");
                                // handle error


                               /* if (e instanceof HttpException) {
                                    ErrorLogin error = ErrorUtils.parseError(((HttpException) e).response().errorBody(), ServiceGenerator.getRetrofit());
                                }*/
                            }

                            @Override
                            public void onNext(Message message) {
                                // handle response
                                Log.d(TAG, "onNext " + message.getMessage());

                            }
                        });


            }
        });


    }

}

@@ -2907,728 +2907,263 @@
   //
- handle completed%0A                                Log.d(TAG, %22 onCompleted%22);%0A                              /*  Toast.makeText(mContext, %22Register success%22,%0A                                        Toast.LENGTH_LONG).show();*/%0A                            %7D%0A%0A                            @Override%0A                            public void onError(Throwable e) %7B%0A                                Log.e(TAG, %22onError%22);%0A                                // handle error%0A%0A%0A                               /* if (e instanceof HttpException) %7B%0A                                    ErrorLogin error = ErrorUtils.parseError(((HttpException) e).response().errorBody(), ServiceGenerator.getRetrofit());%0A                                %7D*/
+Do nothing%0A                            %7D%0A%0A                            @Override%0A                            public void onError(Throwable e) %7B%0A                                e.printStackTrace();%0A                                Log.e(TAG, e.getMessage());
 %0A   
@@ -3330,81 +3330,36 @@
     
-// handle response%0A                                Log.d(TAG, %22onNext %22 +
+Toast.makeText(getContext(),
  mes
@@ -3375,20 +3375,45 @@
 essage()
+, Toast.LENGTH_LONG).show(
 );%0A
-%0A
         

add a toast for register