package br.ruspotlight;

import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

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

import br.ruspotlight.api.UFRNClient;
import br.ruspotlight.api.ServiceGenerator;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "RUSpotlight/MainAct";

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.ic_home_black_24dp,
            R.drawable.ic_insert_chart_black_24dp,
            R.drawable.ic_person_black_24dp
    };

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }

    @Override
    protected void onResume() {
        super.onResume();

        Uri uri = getIntent().getData();
        if (uri != null && uri.toString().startsWith(UFRNClient.REDIRECT_URI)) {
            String code = uri.getQueryParameter("code");
            if (code != null) {
                Log.d(TAG, "CODE: " + code);
                UFRNClient.getInstance().getToken(code);
            } else if (uri.getQueryParameter("error") != null) {
                // TODO Handle error on query
            }
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_login) {
            Intent intent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse(ServiceGenerator.API_BASE_URL + "/authz-server/oauth/authorize" +
                            "?client_id=" + UFRNClient.CLIENT_ID +
                            "&response_type=" + UFRNClient.RESPONSE_TYPE +
                            "&redirect_uri=" + UFRNClient.REDIRECT_URI));
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new HomeFragment(), "HOME");
        adapter.addFragment(new StatisticsFragment(), "STATISTICS");
        adapter.addFragment(new ProfileFragment(), "PROFILE");
        viewPager.setAdapter(adapter);
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }
}

@@ -45,24 +45,91 @@
 ent.Intent;%0A
+import android.graphics.Color;%0Aimport android.graphics.PorterDuff;%0A
 import andro
@@ -1309,24 +1309,72 @@
 d.toolbar);%0A
+        toolbar.setTitleTextColor(Color.WHITE);%0A
         setS
@@ -1685,21 +1685,22 @@
 setupTab
-Icons
+Layout
 ();%0A    
@@ -3450,171 +3450,1303 @@
 pTab
-Icons() %7B%0A        tabLayout.getTabAt(0).setIcon(tabIcons%5B0%5D);%0A        tabLayout.getTabAt(1).setIcon(tabIcons%5B1%5D);%0A        tabLayout.getTabAt(2).setIcon(tabIcons%5B2%5D
+Layout() %7B%0A        final String unselectedColor = %22#5B0E0C%22;%0A%0A        final String%5B%5D tabTitles = %7B %22In%C3%ADcio%22, %22Estat%C3%ADsticas%22, %22Conta%22 %7D;%0A%0A        toolbar.setTitle(tabTitles%5B0%5D);%0A%0A        tabLayout.getTabAt(0).setIcon(tabIcons%5B0%5D);%0A        tabLayout.getTabAt(1).setIcon(tabIcons%5B1%5D);%0A        tabLayout.getTabAt(2).setIcon(tabIcons%5B2%5D);%0A%0A        tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);%0A        tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor(unselectedColor), PorterDuff.Mode.SRC_IN);%0A        tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor(unselectedColor), PorterDuff.Mode.SRC_IN);%0A%0A%0A        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() %7B%0A            @Override%0A            public void onTabSelected(TabLayout.Tab tab) %7B%0A                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);%0A                toolbar.setTitle(tabTitles%5Btab.getPosition()%5D);%0A            %7D%0A%0A            @Override%0A            public void onTabUnselected(TabLayout.Tab tab) %7B%0A                tab.getIcon().setColorFilter(Color.parseColor(unselectedColor), PorterDuff.Mode.SRC_IN);%0A            %7D%0A%0A            @Override%0A            public void onTabReselected(TabLayout.Tab tab) %7B%0A            %7D%0A        %7D
 );%0A 

Add styles to toolbar and tabs