package com.example.offline.view;

import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.LifecycleRegistryOwner;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.example.offline.R;
import com.example.offline.domain.services.SyncCommentLifecycleObserver;
import com.example.offline.presentation.CommentsViewModel;
import com.example.offline.presentation.CommentsViewModelFactory;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dagger.android.AndroidInjection;

public class CommentsActivity extends AppCompatActivity implements LifecycleRegistryOwner {

    @Inject
    CommentsViewModelFactory viewModelFactory;

    @Inject
    SyncCommentLifecycleObserver syncCommentLifecycleObserver;

    @BindView(R.id.add_comment_edittext)
    EditText addCommentEditText;

    @BindView(R.id.comments_recycler_view)
    RecyclerView recyclerView;

    private CommentListAdapter recyclerViewAdapter;

    private CommentsViewModel viewModel;

    private LifecycleRegistry registry = new LifecycleRegistry(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comments_activity);

        ButterKnife.bind(this);

        getLifecycle().addObserver(syncCommentLifecycleObserver);

        viewModel = ViewModelProviders.of(this, viewModelFactory).get(CommentsViewModel.class);

        subscribeToComments();

        initRecyclerView();
    }

    @OnClick(R.id.add_comment_button)
    void onAddCommentButtonClicked() {

        hideKeyboard();

        // TODO add comment validation
        viewModel.addComment(addCommentEditText.getText().toString());

        clearEditText();
    }

    private void subscribeToComments() {
        viewModel.getComments().observe(this, commentList -> recyclerViewAdapter.updateCommentList(commentList));
    }

    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    private void clearEditText() {
        addCommentEditText.getText().clear();
    }

    private void initRecyclerView() {
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager recyclerViewLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(recyclerViewLayoutManager);

        recyclerViewAdapter = new CommentListAdapter(new ArrayList<>());
        recyclerView.setAdapter(recyclerViewAdapter);
    }

    @Override
    public LifecycleRegistry getLifecycle() {
        return registry;
    }
}

@@ -1432,24 +1432,116 @@
 try(this);%0A%0A
+    @Override%0A    public LifecycleRegistry getLifecycle() %7B%0A        return registry;%0A    %7D%0A%0A
     @Overrid
@@ -1595,24 +1595,24 @@
 nceState) %7B%0A
-
         Andr
@@ -1764,24 +1764,53 @@
 ind(this);%0A%0A
+        initRecyclerView();%0A%0A
         getL
@@ -1973,57 +1973,84 @@
     
-subscribeToComments();%0A%0A        initRecyclerView(
+viewModel.getComments().observe(this, recyclerViewAdapter::updateCommentList
 );%0A 
@@ -2305,170 +2305,8 @@
  %7D%0A%0A
-    private void subscribeToComments() %7B%0A        viewModel.getComments().observe(this, commentList -%3E recyclerViewAdapter.updateCommentList(commentList));%0A    %7D%0A%0A
     
@@ -3013,16 +3013,16 @@
 t%3C%3E());%0A
+
         
@@ -3076,99 +3076,7 @@
    %7D
-%0A%0A    @Override%0A    public LifecycleRegistry getLifecycle() %7B%0A        return registry;%0A    %7D
 %0A%7D%0A

use method reference to update data on RecyclerViewAdapter