package com.codepath.apps.codepathtweets.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
import com.codepath.apps.codepathtweets.R;
import com.codepath.apps.codepathtweets.adapters.TweetsPagerAdapter;
import com.codepath.apps.codepathtweets.fragments.ComposeFragment;
import com.codepath.apps.codepathtweets.fragments.TweetsListFragment;
import com.codepath.apps.codepathtweets.models.Tweet;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TimelineActivity extends AppCompatActivity implements ComposeFragment.ComposeDialogListener {

    private static final int REQUEST_CODE = 20;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabs)
    PagerSlidingTabStrip tabs;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    // Instance of the progress action-view
    MenuItem miActionProgressItem;

    TweetsListFragment mFragmentUserTimeline;
    TweetsListFragment mFragmentHomeTimeline;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timeline);
        ButterKnife.bind(this);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Get the viewpager
        // Set the viewpage adapter for the pager
        TweetsPagerAdapter tweetsPagerAdapter = new TweetsPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(tweetsPagerAdapter);
        // Find the pager sliding tabs
        // Attach the pager tabs to the viewpager
        tabs.setViewPager(viewpager);

        FragmentManager mgr = this.getSupportFragmentManager();
        mFragmentUserTimeline = (TweetsListFragment) mgr.findFragmentById(R.id.fragmentUserTimeline);

        String tagHomeTimelineFragment = tweetsPagerAdapter.getFragmentTag(R.id.viewpager, 0);
        mFragmentHomeTimeline = (TweetsListFragment) mgr.findFragmentByTag(tagHomeTimelineFragment);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_timeline, menu);
        MenuItem searchItem = menu.findItem(R.id.miSearch);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            /**
             * Called when the user submits the query. This could be due to a key press on the
             * keyboard or due to pressing a submit button.
             * The listener can override the standard behavior by returning true
             * to indicate that it has handled the submit request. Otherwise return false to
             * let the SearchView handle the submission by launching any associated intent.
             *
             * @param query the query text that is to be submitted
             * @return true if the query has been handled by the listener, false to let the
             * SearchView perform the default action.
             */
            @Override
            public boolean onQueryTextSubmit(String query) {
                // perform query here
                Toast.makeText(getApplicationContext(), query, Toast.LENGTH_SHORT).show();

                // workaround to avoid issues with some emulators and keyboard devices firing twice if a keyboard enter is used
                // see https://code.google.com/p/android/issues/detail?id=24599
                searchView.clearFocus();

                Intent intent = new Intent(TimelineActivity.this, SearchActivity.class);
                intent.putExtra("query", query);
                startActivity(intent);
                return true;
            }

            /**
             * Called when the query text is changed by the user.
             *
             * @param newText the new content of the query text field.
             * @return false if the SearchView should perform the default action of showing any
             * suggestions if available, true if the action was handled by the listener.
             */
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // Store instance of the menu item containing progress
        miActionProgressItem = menu.findItem(R.id.miActionProgress);
        // Extract the action-view from the menu item
        ProgressBar v =  (ProgressBar) MenuItemCompat.getActionView(miActionProgressItem);
        // Return to finish
        return super.onPrepareOptionsMenu(menu);
    }

    public void onClickCompose(MenuItem item) {
        Toast.makeText(getApplicationContext(), "Compose new tweet", Toast.LENGTH_SHORT).show();
        FragmentManager fm = getSupportFragmentManager();
        ComposeFragment composeFragment = ComposeFragment.newInstance();
        composeFragment.show(fm, "fragment_set_filter");
    }

    public void onClickProfile(MenuItem item) {
        loadProfile();
    }

    private void loadProfile() {
        Intent intent = new Intent(this, ProfileActivity.class);
        startActivity(intent);
    }

    public void onClickMessage(MenuItem item) {

    }

    @Override
    public void onFinishEditDialog(Intent intent) {
        //mTweetCursorAdapter.notifyDataSetChanged();
        Tweet tweet = (Tweet) intent.getParcelableExtra("composeTweet");
        mFragmentHomeTimeline.insert(tweet, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
//            mTweetCursorAdapter.notifyDataSetChanged();
            Tweet tweet = (Tweet) data.getParcelableExtra("replyTweet");
            mFragmentUserTimeline.insert(tweet, 0);
        }
    }
}

@@ -838,24 +838,54 @@
 els.Tweet;%0A%0A
+import org.parceler.Parcels;%0A%0A
 import butte
@@ -897,24 +897,24 @@
 e.BindView;%0A
-
 import butte
@@ -1418,24 +1418,92 @@
 meTimeline;%0A
+    FragmentManager mgr;%0A    TweetsPagerAdapter tweetsPagerAdapter;%0A
     @Overrid
@@ -1881,35 +1881,16 @@
         
-TweetsPagerAdapter 
 tweetsPa
@@ -2141,32 +2141,16 @@
         
-FragmentManager 
 mgr = th
@@ -2288,204 +2288,8 @@
 );%0A%0A
-        String tagHomeTimelineFragment = tweetsPagerAdapter.getFragmentTag(R.id.viewpager, 0);%0A        mFragmentHomeTimeline = (TweetsListFragment) mgr.findFragmentByTag(tagHomeTimelineFragment);%0A
     
@@ -5745,16 +5745,31 @@
 (Tweet) 
+Parcels.unwrap(
 intent.g
@@ -5797,24 +5797,56 @@
 mposeTweet%22)
+);%0A        getHomelineFragment()
 ;%0A        mF
@@ -5882,32 +5882,379 @@
 eet, 0);%0A    %7D%0A%0A
+    private void getHomelineFragment() %7B%0A        if (mFragmentHomeTimeline == null) %7B%0A            String tagHomeTimelineFragment = tweetsPagerAdapter.getFragmentTag(R.id.viewpager, 0);%0A            mFragmentHomeTimeline = (TweetsListFragment) mgr.findFragmentByTag(tagHomeTimelineFragment);%0A        %7D%0A//        return mFragmentHomeTimeline;%0A    %7D%0A%0A
     @Override%0A  
@@ -6497,16 +6497,31 @@
 (Tweet) 
+Parcels.unwrap(
 data.get
@@ -6549,16 +6549,17 @@
 yTweet%22)
+)
 ;%0A      
@@ -6560,37 +6560,72 @@
             
-mFragmentUser
+getHomelineFragment();%0A            mFragmentHome
 Timeline.ins

Fix Compose and Reply