package com.example.octodroid.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.octodroid.R;
import com.example.octodroid.data.SessionManager;
import com.example.octodroid.data.prefs.OctodroidPrefs;
import com.example.octodroid.data.prefs.OctodroidPrefsSchema;
import com.example.octodroid.fragments.RepositoryEventListFragment;
import com.example.octodroid.fragments.RepositoryEventListFragmentAutoBundle;
import com.example.octodroid.intent.RequestCode;
import com.example.octodroid.views.components.ViewPagerAdapter;
import com.rejasupotaro.octodroid.models.Repository;
import com.rejasupotaro.octodroid.models.Resource;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.repository_view_pager_tabs)
    TabLayout tabLayout;
    @Bind(R.id.repository_view_pager)
    ViewPager viewPager;
    @Bind(R.id.empty_view_container)
    View emptyViewContainer;

    public static void launch(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        if (SessionManager.isLoggedIn(this)) {
            SessionManager.login(this);
            setupActionBar();
            setupViews();
        } else {
            LoginActivity.launch(this);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                return true;
            case R.id.action_add_repository:
                RepositoryListActivity.launch(this);
                return true;
            case R.id.action_logout:
                SessionManager.logout(this);
                LoginActivity.launch(this);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RequestCode.ADD_REPOSITORY:
                setupViews();
                break;
            default:
                // do nothing
                break;
        }
    }

    public void setupActionBar() {
        getSupportActionBar().setTitle("");
    }

    private void setupViews() {
        OctodroidPrefs octodroidPrefs = OctodroidPrefsSchema.get(this);
        if (octodroidPrefs.getSeletedSerializedRepositories().isEmpty()) {
            emptyViewContainer.setVisibility(View.VISIBLE);
        } else {
            emptyViewContainer.setVisibility(View.GONE);

            ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
            for (String serializedRepositories : octodroidPrefs.getSeletedSerializedRepositories()) {
                Repository repository = Resource.fromJson(serializedRepositories, Repository.class);

                RepositoryEventListFragment fragment = RepositoryEventListFragmentAutoBundle
                        .createFragmentBuilder(repository)
                        .build();

                pagerAdapter.addFragment(fragment, repository.getFullName());
            }

            viewPager.setAdapter(pagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
        }
    }

    @OnClick(R.id.add_repository_button)
    void onAddRepositoryButtonClick() {
        RepositoryListActivity.launch(this);
    }
}

@@ -911,16 +911,68 @@
 ource;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import b
@@ -1301,16 +1301,585 @@
 ainer;%0A%0A
+    private List%3CRepository%3E repositories = new ArrayList%3C%3E();%0A%0A    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() %7B%0A        @Override%0A        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) %7B%0A        %7D%0A%0A        @Override%0A        public void onPageSelected(int position) %7B%0A            Repository repository = repositories.get(position);%0A            setTitle(repository);%0A        %7D%0A%0A        @Override%0A        public void onPageScrollStateChanged(int state) %7B%0A        %7D%0A    %7D;%0A%0A
     publ
@@ -2407,38 +2407,8 @@
 s);%0A
-            setupActionBar();%0A
     
@@ -3523,29 +3523,30 @@
     %7D%0A%0A    p
-ublic
+rivate
  void setupA
@@ -3546,20 +3546,35 @@
  set
-upActionBar(
+Title(Repository repository
 ) %7B%0A
@@ -3616,10 +3616,32 @@
 tle(
-%22%22
+repository.getFullName()
 );%0A 
@@ -3884,16 +3884,43 @@
 ISIBLE);
+%0A%0A            setTitle(%22%22);
 %0A       
@@ -4530,36 +4530,32 @@
 , repository.get
-Full
 Name());%0A       
@@ -4543,24 +4543,70 @@
 getName());%0A
+                repositories.add(repository);%0A
             
@@ -4672,46 +4672,159 @@
     
-tabLayout.setupWithViewPager(viewPager
+viewPager.addOnPageChangeListener(onPageChangeListener);%0A            tabLayout.setupWithViewPager(viewPager);%0A%0A            setTitle(repositories.get(0)
 );%0A 

Set repository name as action bar title