package com.angcyo.uiview.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.angcyo.uiview.R;
import com.angcyo.uiview.utils.ScreenUtil;
import com.angcyo.uiview.view.UIIViewImpl;

/**
 * 用来控制状态栏的padding
 * Created by angcyo on 2016-11-05.
 */

public class TitleBarLayout extends FrameLayout {

    boolean enablePadding = true;
    boolean fitActionBar = false;
    boolean isScreenHeight = false;

    /**
     * 允许的最大高度, 如果为-2px,那么就是屏幕高度的一半, 如果是-3px,那么就是屏幕高度的三分之, 以此内推
     */
    private int maxHeight = -1;

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

    public TitleBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarLayout);
        maxHeight = typedArray.getDimensionPixelOffset(R.styleable.TitleBarLayout_r_max_height, -1);
        enablePadding = UIIViewImpl.isLollipop();
        fitActionBar = typedArray.getBoolean(R.styleable.TitleBarLayout_r_fit_action_bar_height, fitActionBar);
        enablePadding = typedArray.getBoolean(R.styleable.TitleBarLayout_r_fit_status_bar_height, enablePadding);
        isScreenHeight = typedArray.getBoolean(R.styleable.TitleBarLayout_r_is_screen_height, isScreenHeight);

        resetMaxHeight();
        typedArray.recycle();

        initLayout();
    }

    private void initLayout() {

    }

    private void resetMaxHeight() {
        if (maxHeight < -1) {
            int num = Math.abs(maxHeight);
            maxHeight = ScreenUtil.screenHeight / num;
        }
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
        resetMaxHeight();
        requestLayout();
    }

    public void setEnablePadding(boolean enablePadding) {
        this.enablePadding = enablePadding;
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        if (getChildCount() > 1) {
            throw new IllegalArgumentException("Need Only One Child View.");
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int statusBarHeight = getResources().getDimensionPixelSize(R.dimen.status_bar_height);
        int actionBarHeight = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
        int topHeight = 0;
        int viewHeight = 0;

        if (fitActionBar && enablePadding) {
            topHeight = statusBarHeight + actionBarHeight;
        } else if (enablePadding) {
            topHeight = statusBarHeight;
        } else if (fitActionBar) {
            topHeight = actionBarHeight;
        } else {
            topHeight = 0;
        }

        setPadding(getPaddingLeft(), topHeight, getPaddingRight(), getPaddingBottom());
        if (maxHeight > 0) {
            maxHeight += topHeight;
        }

        if (maxHeight > 0) {
            super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Math.min(maxHeight, heightSize + topHeight), heightMode));
        } else {
            viewHeight = heightSize + topHeight;
            if (heightMode == MeasureSpec.EXACTLY) {
                int childWidth = widthSize;
                if (getChildCount() > 0) {
                    if (heightSize == ScreenUtil.screenHeight || isScreenHeight) {
                        heightSize -= topHeight;
                    }
                    getChildAt(0).measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(heightSize - getPaddingBottom(), heightMode));
                    childWidth = getChildAt(0).getMeasuredWidth();
                }
                if (widthMode == MeasureSpec.EXACTLY) {
                    setMeasuredDimension(widthSize, viewHeight);
                } else {
                    setMeasuredDimension(childWidth, viewHeight);
                }
            } else {
                super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(viewHeight, heightMode));
            }
        }
    }
}

@@ -851,17 +851,51 @@
 attrs);%0A
-%0A
+        if (!isInEditMode()) %7B%0A    
         
@@ -985,32 +985,36 @@
 ayout);%0A        
+  
+  
 maxHeight = type
@@ -1090,32 +1090,36 @@
 t, -1);%0A        
+    
 enablePadding = 
@@ -1152,16 +1152,20 @@
         
+   
+ 
 fitActio
@@ -1260,16 +1260,20 @@
 onBar);%0A
+    
         
@@ -1378,16 +1378,20 @@
 dding);%0A
+    
         
@@ -1494,32 +1494,36 @@
 ight);%0A%0A        
+    
 resetMaxHeight()
@@ -1516,32 +1516,36 @@
 setMaxHeight();%0A
+    
         typedArr
@@ -1558,16 +1558,25 @@
 ycle();%0A
+        %7D
 %0A       

fix base in edit model