package com.aberigle.android.sliderview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

/**
 * Created by aberigle on 06/04/15.
 */
public class SlidingTabLayout extends HorizontalScrollView {

    private static final float TAB_VIEW_PADDING_DIPS = 16;

    private final SlidingTabStrip strip;

    private final int selectedTextColor;
    private final int textColor;

    private ViewPager           viewpager;
    private PagerChangeListener pagerListener;
    private ViewPager.OnPageChangeListener onPageChangeListener;


    public SlidingTabLayout(Context context) { this(context, null); }

    public SlidingTabLayout(Context context, AttributeSet attributes) {
        super(context, attributes);
        setHorizontalScrollBarEnabled(false);
        setFillViewport(true);

        selectedTextColor = context.getResources().getColor(R.color.white);
        textColor = context.getResources().getColor(R.color.bright_foreground_dark_disabled);

        strip = new SlidingTabStrip(context, Color.WHITE);

        addView(strip);

    }

    @Override
    public void setElevation(float elevation) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) super.setElevation(elevation);
        else ViewCompat.setElevation(this, elevation);
    }

    private void populateFromPager() {
        PagerAdapter adapter = viewpager.getAdapter();
        int          padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);

        TextView tab;

        if (adapter != null) for (int i = 0; i < adapter.getCount(); i++) {
            tab = new TextView(getContext());
            final int index = i;
            tab.setGravity(Gravity.CENTER);
            tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            tab.setTypeface(Typeface.DEFAULT_BOLD);
            tab.setTextColor(textColor);
            tab.setAllCaps(true);
            tab.setText(adapter.getPageTitle(i));
            tab.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    viewpager.setCurrentItem(index);
                }
            });
            tab.setPadding(padding,padding,padding,padding);
            strip.addView(tab);
        }

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (viewpager != null) {
            pagerListener.onPageSelected(viewpager.getCurrentItem());
            scrollToTab(viewpager.getCurrentItem(), 0);
        }
    }


    private int resolveAttr(int attrId) {
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(attrId, outValue, true);
        return outValue.data;
    }

    private void scrollToTab(int index, float positionOffset) {
        final int childCount = strip.getChildCount();
        if (childCount == 0 || index < 0 || index >= childCount) return;

        View selectedChild = strip.getChildAt(index);
        if (selectedChild != null) {
            int left = selectedChild.getLeft();

            if (index < (childCount - 1)) {
                View nextTitle = strip.getChildAt(index + 1);
                left = (int) (positionOffset * nextTitle.getLeft() + (1.0f - positionOffset) * left);
            }

            int targetScrollX = left + (selectedChild.getWidth() / 2) - (getWidth() / 2);
            scrollTo(targetScrollX, 0);
        }
    }

    public void setViewpager(ViewPager pager) {
        viewpager = pager;

        if (pager != null) pager.setOnPageChangeListener(pagerListener = new PagerChangeListener());

        populateFromPager();
    }

    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    private class PagerChangeListener implements ViewPager.OnPageChangeListener {

        private TextView oldPos;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            strip.onViewPagerChanged(position, positionOffset);
            scrollToTab(position, positionOffset);
            onPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
            TextView selected = (TextView) strip.getChildAt(position);
            if (oldPos != null) if (!oldPos.equals(selected)) oldPos.setTextColor(textColor);
            oldPos = selected;
            selected.setTextColor(selectedTextColor);
            onPageChangeListener.onPageSelected(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            onPageChangeListener.onPageScrollStateChanged(state);
        }
    }

}

@@ -132,16 +132,64 @@
 peface;%0A
+import android.graphics.drawable.ColorDrawable;%0A
 import a
@@ -331,24 +331,65 @@
 .ViewPager;%0A
+import android.support.v7.app.ActionBar;%0A
 import andro
@@ -881,32 +881,43 @@
 Pager           
+           
 viewpager;%0A    p
@@ -943,16 +943,27 @@
 istener 
+           
 pagerLis
@@ -1034,16 +1034,122 @@
 stener;%0A
+    private ActionBar                      bar;%0A    private ColorDrawable                  barBackground;%0A
 %0A%0A    pu
@@ -3150,201 +3150,192 @@
  %7D%0A%0A
-%0A    private int resolveAttr(int attrId) %7B%0A        TypedValue outValue = new TypedValue();%0A        getContext().getTheme().resolveAttribute(attrId, outValue, true);%0A        return outValue.data
+    @Override%0A    public void setBackgroundColor(int color) %7B%0A        super.setBackgroundColor(color);%0A        if (bar != null) ((ColorDrawable) barBackground.mutate()).setColor(color)
 ;%0A  
@@ -4407,32 +4407,291 @@
 istener;%0A    %7D%0A%0A
+    public void attachToActionBar(ActionBar bar) %7B%0A        this.bar = bar;%0A        bar.setElevation(0);        ;%0A        barBackground = new ColorDrawable(((ColorDrawable) getBackground()).getColor());%0A        bar.setBackgroundDrawable(barBackground);%0A    %7D%0A%0A
     private clas

allow to attach an action bar to the slider