package io.gomatcha.matcha;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import io.gomatcha.matcha.proto.view.android.PbPagerView;

class MatchaPagerView extends MatchaChildView {
    SlidingTabLayout tabStrip;
    ViewPager viewPager;
    MatchaPagerAdapter pagerAdapter;
    MatchaViewNode viewNode;

    static {
        MatchaView.registerView("gomatcha.io/matcha/view/android PagerView", new MatchaView.ViewFactory() {
            @Override
            public MatchaChildView createView(Context context, MatchaViewNode node) {
                return new MatchaPagerView(context, node);
            }
        });
    }

    public MatchaPagerView(Context context, MatchaViewNode node) {
        super(context);
        viewNode = node;

        pagerAdapter = new MatchaPagerAdapter();

        RelativeLayout.LayoutParams tabParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        tabStrip = new SlidingTabLayout(context);
        tabStrip.setId(generateViewId());
        tabStrip.setBackgroundColor(0xff00ffff);
        addView(tabStrip, tabParams);

        RelativeLayout.LayoutParams contentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        contentParams.addRule(RelativeLayout.BELOW, tabStrip.getId());
        viewPager = new ViewPager(context);
        viewPager.setId(generateViewId());
        viewPager.setBackgroundColor(0xff0000ff);
        viewPager.setAdapter(pagerAdapter);
        addView(viewPager, contentParams);

        tabStrip.setDistributeEvenly(true);
        tabStrip.setViewPager(viewPager);
    }

    @Override
    public void setNativeState(byte[] nativeState) {
        super.setNativeState(nativeState);
        try {
            PbPagerView.PagerView proto  = PbPagerView.PagerView.parseFrom(nativeState);
            pagerAdapter.protoChildViews = proto.getChildViewsList();
            pagerAdapter.notifyDataSetChanged();
            tabStrip.setViewPager(viewPager);
        } catch (InvalidProtocolBufferException e) {
        }
    }

    @Override
    public boolean isContainerView() {
        return true;
    }

    @Override
    public void setChildViews(List<View> childViews) {
        pagerAdapter.childViews = childViews;
        pagerAdapter.notifyDataSetChanged();
        tabStrip.setViewPager(viewPager);
    }

    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
    public static int generateViewId() {
        for (;;) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }

    public class MatchaPagerAdapter extends PagerAdapter {
        List<View> childViews;
        List<PbPagerView.PagerChildView> protoChildViews;

        @Override
        public int getCount() {
            if (childViews == null) {
                return 0;
            }
            return childViews.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return object == view;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (childViews == null) {
                View v = new View(container.getContext());
                container.addView(v);
                return v;
            }
            View v = childViews.get(position);
            container.addView(v);
            return v;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return protoChildViews.get(position).getTitle();
        }
    }
}

@@ -137,24 +137,60 @@
 .ViewPager;%0A
+import android.util.DisplayMetrics;%0A
 import andro
@@ -203,16 +203,16 @@
 w.View;%0A
-
 import a
@@ -631,24 +631,59 @@
 de viewNode;
+%0A    RelativeLayout relativeLayout;
 %0A%0A    static
@@ -1158,16 +1158,103 @@
 apter();
+%0A        relativeLayout = new RelativeLayout(context);%0A        addView(relativeLayout);
 %0A%0A      
@@ -1538,32 +1538,47 @@
 0ffff);%0A        
+relativeLayout.
 addView(tabStrip
@@ -1582,32 +1582,264 @@
 rip, tabParams);
+%0A        if (android.os.Build.VERSION.SDK_INT %3E= 21) %7B%0A            float ratio = (float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT;%0A            tabStrip.setElevation(4 * ratio);%0A        %7D
 %0A%0A        Relati
@@ -2230,24 +2230,24 @@
 apter);%0A
-
         
 addView(
@@ -2230,32 +2230,47 @@
 apter);%0A        
+relativeLayout.
 addView(viewPage

Fix crash in MatchaPagerView