package io.github.droidkaigi.confsched2017.view.fragment;

import com.sys1yagi.fragmentcreator.annotation.Args;
import com.sys1yagi.fragmentcreator.annotation.FragmentCreator;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import javax.inject.Inject;

import io.github.droidkaigi.confsched2017.databinding.FragmentSessionFeedbackBinding;
import io.github.droidkaigi.confsched2017.model.SessionFeedback;
import io.github.droidkaigi.confsched2017.viewmodel.SessionFeedbackViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@FragmentCreator
public class SessionFeedbackFragment extends BaseFragment implements SessionFeedbackViewModel.Callback {

    public static final String TAG = SessionFeedbackFragment.class.getSimpleName();

    @Inject
    SessionFeedbackViewModel viewModel;

    @Inject
    CompositeDisposable compositeDisposable;

    @Args
    int sessionId;

    private FragmentSessionFeedbackBinding binding;

    public SessionFeedbackFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SessionFeedbackFragmentCreator.read(this);
        viewModel.setCallback(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        getComponent().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentSessionFeedbackBinding.inflate(inflater, container, false);
        binding.setViewModel(viewModel);

        initView();
        return binding.getRoot();
    }

    private void initView() {
        Disposable disposable = viewModel.findSession(sessionId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        session -> {
                            // TODO
                        },
                        throwable -> Log.e(TAG, "Failed to find session.", throwable)
                );
        compositeDisposable.add(disposable);
    }

    @Override
    public void onPause() {
        super.onPause();
        compositeDisposable.dispose();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.destroy();
    }

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

    @Override
    public void onClickSubmitFeedback() {
        SessionFeedback sessionFeedback = new SessionFeedback(sessionId);
        viewModel.submitSessionFeedback(sessionFeedback)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(success -> onSubmitSuccess(), failure -> onSubmitFailure());
    }

    public void onSubmitSuccess() {
        // TODO: show success action
        if (isDetached())
            return;
        Toast.makeText(getContext(), "submit success", Toast.LENGTH_SHORT).show();
    }

    public void onSubmitFailure() {
        // TODO: show failure action
        if (isDetached())
            return;
        Toast.makeText(getContext(), "submit failure", Toast.LENGTH_SHORT).show();
    }
}

@@ -2946,32 +2946,56 @@
 ionId);%0A        
+compositeDisposable.add(
 viewModel.submit
@@ -3027,16 +3027,16 @@
 edback)%0A
-
         
@@ -3218,16 +3218,17 @@
 ilure())
+)
 ;%0A    %7D%0A
@@ -3305,54 +3305,8 @@
 ion%0A
-        if (isDetached())%0A            return;%0A
     
@@ -3423,24 +3423,24 @@
 Failure() %7B%0A
+
         // T
@@ -3468,54 +3468,8 @@
 ion%0A
-        if (isDetached())%0A            return;%0A
     

Apply review feedback!