/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.common.widget;

import com.ntels.onecable.common.util.LogUtil;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

/**
 * <pre>
 * 1. ClassName :   CustomViewPager
 * 2. FileName  :   CustomViewPager.java
 * 3. Package   :   com.ntels.onecable.common.widget
 * 4. Date      :   2016-12-06
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class CustomViewPager extends ViewPager {

    private static final String TAG = CustomViewPager.class.getSimpleName();

    private View mCurrentView;

    public CustomViewPager(Context context) {

        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {

        super(context, attrs);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        for(int i = 0; i < getChildCount(); i++)
        {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if(h > height)
            {
                height = h;
            }
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        /*if (mCurrentView == null) {
            LogUtil.i(TAG, "mCurrentView - " + mCurrentView);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        mCurrentView.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if (h > height) {
            height = h;
        }
        LogUtil.i(TAG, "Measured height - " + height);
        heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);*/
    }

    /**
     * Measure current view.
     *
     * @param currentView Current view
     */
    public void measureCurrentView(View currentView) {

        mCurrentView = currentView;
        requestLayout();
    }

    public int measureFragment(View view) {

        if (view == null)
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}