package com.nutrition.express.blogposts;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.nutrition.express.R;
import com.nutrition.express.common.CommonPagerAdapter;
import com.nutrition.express.likes.LikesFragment;
import com.nutrition.express.useraction.FollowBlogContract;
import com.nutrition.express.useraction.FollowBlogPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huang on 5/16/16.
 */
public class PostListActivity extends AppCompatActivity implements FollowBlogContract.View {
    private MenuItem followItem;
    private FollowBlogPresenter followBlogPresenter;
    private boolean followed = false;
    private String blogName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_posts);

        Intent intent = getIntent();
        blogName = intent.getStringExtra("blog_name");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(blogName);
        }
        CollapsingToolbarLayout collapsingToolbarLayout =
                (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
        collapsingToolbarLayout.setTitleEnabled(false);

        List<Fragment> list = new ArrayList<>();
        List<String> titles = new ArrayList<>();

        Bundle bundle = new Bundle();
        bundle.putString("blog_name", blogName);
        PostListFragment postListFragment = new PostListFragment();
        postListFragment.setArguments(bundle);
        LikesFragment likesFragment = new LikesFragment();
        likesFragment.setArguments(bundle);

        list.add(postListFragment);
        titles.add(getString(R.string.blog_post));
        list.add(likesFragment);
        titles.add(getString(R.string.page_user_like));

        CommonPagerAdapter pagerAdapter =
                new CommonPagerAdapter(getSupportFragmentManager(), list, titles);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(pagerAdapter);
        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(
                new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                        //hack code;
                        PostListFragment fragment = (PostListFragment) getSupportFragmentManager()
                                .findFragmentByTag("android:switcher:" + R.id.viewPager + ":"
                                        + viewPager.getCurrentItem());
                        if (fragment != null) {
                            fragment.scrollToTop();
                        }
                    }
                });

        followBlogPresenter = new FollowBlogPresenter(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean isAdmin = false;
        Intent intent = getIntent();
        if (intent != null) {
            isAdmin = intent.getBooleanExtra("is_admin", false);
        }
        if (!isAdmin) {
            getMenuInflater().inflate(R.menu.menu_blog, menu);
            followItem = menu.findItem(R.id.blog_follow);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.blog_follow:
                if (followed) {
                    followBlogPresenter.unfollow(blogName);
                } else {
                    followBlogPresenter.follow(blogName);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onFollow() {
        if (followItem != null) {
            followItem.setTitle(R.string.blog_unfollow);
            followed = true;
        }
    }

    @Override
    public void onUnfollow() {
        if (followItem != null) {
            followItem.setTitle(R.string.blog_follow);
            followed = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        followBlogPresenter.onDetach();
    }

    public void hideFollowItem() {
        if (followItem != null) {
            followItem.setVisible(false);
        }
    }
}

@@ -2065,32 +2065,38 @@
 gName);%0A        
+final 
 PostListFragment
@@ -2885,33 +2885,16 @@
 istener(
-%0A                
 new TabL
@@ -2899,25 +2899,16 @@
 bLayout.
-ViewPager
 OnTabSel
@@ -2925,25 +2925,12 @@
 ner(
-viewPager) %7B%0A    
+) %7B%0A
     
@@ -2937,20 +2937,16 @@
         
-    
 @Overrid
@@ -2947,24 +2947,16 @@
 verride%0A
-        
         
@@ -2980,11 +2980,9 @@
 nTab
-Res
+S
 elec
@@ -3010,32 +3010,35 @@
 ) %7B%0A            
+%7D%0A%0A
             //ha
@@ -3037,122 +3037,79 @@
     
-//hack code;%0A                        PostListFragment fragment = (PostListFragment) getSupportFragmentManager()%0A  
+@Override%0A            public void onTabUnselected(TabLayout.Tab tab) %7B%0A
     
@@ -3108,32 +3108,35 @@
 ) %7B%0A            
+%7D%0A%0A
                 
@@ -3135,191 +3135,121 @@
     
-      .findFragmentByTag(%22android:switcher:%22 + R.id.viewPager + %22:%22%0A                                        + viewPager.getCurrentItem());%0A                        if (fragment != null
+@Override%0A            public void onTabReselected(TabLayout.Tab tab) %7B%0A                if (tab.getPosition() == 0
 ) %7B%0A
@@ -3268,25 +3268,25 @@
         
-        f
+postListF
 ragment.
@@ -3320,38 +3320,8 @@
     
-        %7D%0A                    
 %7D%0A  
@@ -3318,32 +3318,38 @@
       %7D%0A        
+    %7D%0A
         %7D);%0A%0A   

fix bug