// Path: indicator/src/main/java/com/fuzz/indicator/proxy/ProxyReference.java
// public interface ProxyReference {
//     /**
//      * Modifies the passed-in value to represent a valid child position in this
//      * {@link CutoutViewIndicator}.
//      * @param proposed    a proposed position index
//      * @return the corrected value
//      */
//     int fixPosition(int proposed);
// 
// }
// 
// Path: indicator/src/main/java/com/fuzz/indicator/proxy/ViewProvidingStateProxy.java
// public interface ViewProvidingStateProxy extends StateProxy {
//     /**
//      * In some use cases, it may be handy to have a reference to the original
//      * View when binding a representation thereof. See
//      * {@link CutoutCellGenerator#onBindChild(View, CutoutViewLayoutParams, View)}
//      * for details.
//      *
//      * @param cviPosition    position of a child within the CutoutViewIndicator
//      * @return the view which directly inspired the indicator's child
//      */
//     @Nullable
//     View getOriginalViewFor(int cviPosition);
// }


import android.database.DataSetObserver;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.fuzz.indicator.proxy.IndicatorOffsetEvent;
import com.fuzz.indicator.proxy.ProxyReference;
import com.fuzz.indicator.proxy.StateProxy;
import com.fuzz.indicator.proxy.ViewProvidingStateProxy;
/*
 * Copyright 2016 Philip Cohn-Cort
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fuzz.indicator;



/**
 * {@link StateProxy} wrapper around a {@link ViewPager}. All calls
 * are delegated to this object.
 * <p>
 *     Due to the rather strict register/unregister nature of the
 *     {@link android.database.Observable Observable} object backing each
 *     {@link PagerAdapter}, every call to
 *     {@link #associateWith(DataSetObserver)} must be paired with one to
 *     {@link #disassociateFrom(DataSetObserver)}.
 * </p>
 *
 * @author Philip Cohn-Cort (Fuzz)
 */
public class ViewPagerStateProxy implements ViewProvidingStateProxy {

    @NonNull
    private final ViewPager pager;

    @NonNull
    private OnViewPagerChangeListener pageChangeListener;

    public ViewPagerStateProxy(@NonNull ViewPager pager, @NonNull CutoutViewIndicator cvi) {
        this.pager = pager;
        this.pageChangeListener = new OnViewPagerChangeListener(cvi);
    }

    @Override
    public float getCurrentPosition() {
        // Seriously. They called this the 'CurrentItem'. Can you believe it?
        return pager.getCurrentItem();
    }

    @Override
    public int getCellCount() {
        return pager.getAdapter().getCount();
    }

    @Override
// Path: indicator/src/main/java/com/fuzz/indicator/proxy/ProxyReference.java
// public interface ProxyReference {
//     /**
//      * Modifies the passed-in value to represent a valid child position in this
//      * {@link CutoutViewIndicator}.
//      * @param proposed    a proposed position index
//      * @return the corrected value
//      */
//     int fixPosition(int proposed);
// 
// }
// 
// Path: indicator/src/main/java/com/fuzz/indicator/proxy/ViewProvidingStateProxy.java
// public interface ViewProvidingStateProxy extends StateProxy {
//     /**
//      * In some use cases, it may be handy to have a reference to the original
//      * View when binding a representation thereof. See
//      * {@link CutoutCellGenerator#onBindChild(View, CutoutViewLayoutParams, View)}
//      * for details.
//      *
//      * @param cviPosition    position of a child within the CutoutViewIndicator
//      * @return the view which directly inspired the indicator's child
//      */
//     @Nullable
//     View getOriginalViewFor(int cviPosition);
// }


// Path: indicator/src/main/java/com/fuzz/indicator/ViewPagerStateProxy.java
import android.database.DataSetObserver;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.fuzz.indicator.proxy.IndicatorOffsetEvent;
import com.fuzz.indicator.proxy.ProxyReference;
import com.fuzz.indicator.proxy.StateProxy;
import com.fuzz.indicator.proxy.ViewProvidingStateProxy;

/*
 * Copyright 2016 Philip Cohn-Cort
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fuzz.indicator;



/**
 * {@link StateProxy} wrapper around a {@link ViewPager}. All calls
 * are delegated to this object.
 * <p>
 *     Due to the rather strict register/unregister nature of the
 *     {@link android.database.Observable Observable} object backing each
 *     {@link PagerAdapter}, every call to
 *     {@link #associateWith(DataSetObserver)} must be paired with one to
 *     {@link #disassociateFrom(DataSetObserver)}.
 * </p>
 *
 * @author Philip Cohn-Cort (Fuzz)
 */
public class ViewPagerStateProxy implements ViewProvidingStateProxy {

    @NonNull
    private final ViewPager pager;

    @NonNull
    private OnViewPagerChangeListener pageChangeListener;

    public ViewPagerStateProxy(@NonNull ViewPager pager, @NonNull CutoutViewIndicator cvi) {
        this.pager = pager;
        this.pageChangeListener = new OnViewPagerChangeListener(cvi);
    }

    @Override
    public float getCurrentPosition() {
        // Seriously. They called this the 'CurrentItem'. Can you believe it?
        return pager.getCurrentItem();
    }

    @Override
    public int getCellCount() {
        return pager.getAdapter().getCount();
    }

    @Override
    public IndicatorOffsetEvent resendPositionInfo(ProxyReference cvi, float assumedIndicatorPosition) {