package eu.dubedout.vincent.viewpagerindicatorslibraries.components.viewpagerindicatoractivity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;

import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.IconPageIndicator;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.LinePageIndicator;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.UnderlinePageIndicator;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import eu.dubedout.vincent.viewpagerindicatorslibraries.DemoPagerAdapter;
import eu.dubedout.vincent.viewpagerindicatorslibraries.GenericData;
import eu.dubedout.vincent.viewpagerindicatorslibraries.PageData;
import eu.dubedout.vincent.viewpagerindicatorslibraries.R;

public class ViewPagerIndicatorActivity extends AppCompatActivity {

    @BindView(R.id.activity_view_pager_indicator_container) LinearLayout container;
    @BindView(R.id.activity_view_pager_indicator_title) TitlePageIndicator indicatorTitle;
    @BindView(R.id.activity_view_pager_indicator_viewpager_title) ViewPager viewpagerTitle;

    @BindView(R.id.activity_view_pager_indicator_tab) TabPageIndicator indicatorTab;
    @BindView(R.id.activity_view_pager_indicator_viewpager_tab) ViewPager viewpagerTab;

    @BindView(R.id.activity_view_pager_indicator_line) LinePageIndicator indicatorLine;
    @BindView(R.id.activity_view_pager_indicator_viewpager_line) ViewPager viewpagerLine;

    @BindView(R.id.activity_view_pager_indicator_underline) UnderlinePageIndicator indicatorUnderline;
    @BindView(R.id.activity_view_pager_indicator_viewpager_underline) ViewPager viewpagerUnderline;

    @BindView(R.id.activity_view_pager_indicator_circle) CirclePageIndicator indicatorCircle;
    @BindView(R.id.activity_view_pager_indicator_viewpager_circle) ViewPager viewpagerCircle;

    @BindView(R.id.activity_view_pager_indicator_icon) IconPageIndicator indicatorIcon;
    @BindView(R.id.activity_view_pager_indicator_viewpager_icon) ViewPager viewpagerIcon;

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

        viewpagerTitle.setAdapter(getAdapter());
        indicatorTitle.setViewPager(viewpagerTitle);

        viewpagerTab.setAdapter(getAdapter());
        indicatorTab.setViewPager(viewpagerTab);

        viewpagerLine.setAdapter(getAdapter());
        indicatorLine.setViewPager(viewpagerLine);

        viewpagerUnderline.setAdapter(getAdapter());
        indicatorUnderline.setViewPager(viewpagerUnderline);

        viewpagerCircle.setAdapter(getAdapter());
        indicatorCircle.setViewPager(viewpagerCircle);

        viewpagerIcon.setAdapter(getIconAdapter());
        indicatorIcon.setViewPager(viewpagerIcon);
    }

    @NonNull
    private DemoPagerAdapter getAdapter() {
        return new DemoPagerAdapter(new GenericData(this).getContent());
    }

    private PagerAdapter getIconAdapter() {
        return new DemoIconPagerAdapter(new GenericData(this).getContent());
    }

    class DemoIconPagerAdapter extends DemoPagerAdapter implements IconPagerAdapter {
        public DemoIconPagerAdapter(List<PageData> list) {
            super(list);
        }

        int[] icons = new int[]{R.drawable.brightness_1, R.drawable.brightness_3, R.drawable.brightness_4, R.drawable.brightness_5};

        @Override
        public int getIconResId(int index) {
            return icons[index % getCount()];
        }
    }


}

@@ -2506,24 +2506,113 @@
 ind(this);%0A%0A
+        ViewPager.SimpleOnPageChangeListener pagerSyncronizer = getPagerSynchronizer();%0A%0A
         view
@@ -2696,24 +2696,90 @@
 pagerTitle);
+%0A        indicatorTitle.setOnPageChangeListener(pagerSyncronizer);
 %0A%0A        vi
@@ -2863,16 +2863,80 @@
 gerTab);
+%0A        indicatorTab.setOnPageChangeListener(pagerSyncronizer);
 %0A%0A      
@@ -3023,24 +3023,89 @@
 wpagerLine);
+%0A        indicatorLine.setOnPageChangeListener(pagerSyncronizer);
 %0A%0A        vi
@@ -3207,16 +3207,86 @@
 erline);
+%0A        indicatorUnderline.setOnPageChangeListener(pagerSyncronizer);
 %0A%0A      
@@ -3383,16 +3383,83 @@
 Circle);
+%0A        indicatorCircle.setOnPageChangeListener(pagerSyncronizer);
 %0A%0A      
@@ -3555,16 +3555,81 @@
 rIcon);%0A
+        indicatorIcon.setOnPageChangeListener(pagerSyncronizer);%0A
     %7D%0A%0A 
@@ -3888,24 +3888,646 @@
 ());%0A    %7D%0A%0A
+    private ViewPager.SimpleOnPageChangeListener getPagerSynchronizer() %7B%0A        return new ViewPager.SimpleOnPageChangeListener() %7B%0A            @Override%0A            public void onPageSelected(int position) %7B%0A                viewpagerTitle.setCurrentItem(position, true);%0A                viewpagerTab.setCurrentItem(position, true);%0A                viewpagerLine.setCurrentItem(position, true);%0A                viewpagerUnderline.setCurrentItem(position, true);%0A                viewpagerCircle.setCurrentItem(position, true);%0A                viewpagerIcon.setCurrentItem(position, true);%0A            %7D%0A        %7D;%0A    %7D%0A%0A
     class De

synchronize all pagers