package com.maxiee.heartbeat.ui.common.mdheaderview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.maxiee.heartbeat.R;
import com.maxiee.heartbeat.common.ThemeUtils;

/**
 * Created by maxiee on 15/11/16.
 */
public class MDHeaderView extends ViewGroup {

    public static final int TRI_NUMS = 6;

    private int mColorPrimary;
    private int mColorPrimaryDark;
    private int mColorAccent;
    private float[] mSizes;
    private TriView[] mViews;
    private GradientDrawable mGD;

    public MDHeaderView(Context context) {
        super(context);
        init(context);
    }

    public MDHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MDHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mColorPrimary = ThemeUtils.getAttributeColor(context, R.attr.colorPrimary);
        mColorPrimaryDark = ThemeUtils.getAttributeColor(context, R.attr.colorPrimaryDark);
        mColorAccent = ThemeUtils.getAttributeColor(context, R.attr.colorAccent);
        float redBase = (Color.red(mColorPrimary));
        float greenBase = (Color.green(mColorPrimary));
        float blueBase = (Color.blue(mColorPrimary));
        float redStep = (Color.red(mColorAccent) - redBase) / TRI_NUMS;
        float greenStep = (Color.green(mColorAccent) - greenBase) / TRI_NUMS;
        float blueStep = (Color.blue(mColorAccent) - blueBase) / TRI_NUMS;
        mViews = new TriView[TRI_NUMS];
        mSizes = new float[TRI_NUMS];
        for (int i = TRI_NUMS - 1; i >= 0; i--) {
            mSizes[i] = (float) Math.abs(Math.sin(Math.PI / 4 + Math.PI / (TRI_NUMS * 1.5f) * i));
            mViews[i] = new TriView(context);
            mViews[i].setColor(Color.rgb(
                    (int) (redBase + redStep * (TRI_NUMS - i)),
                    (int) (greenBase + greenStep * (TRI_NUMS - i)),
                    (int) (blueBase + blueStep * (TRI_NUMS - i))));
            addView(mViews[i]);
        }
        mGD = new GradientDrawable(
                GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {mColorPrimaryDark, mColorPrimary});
        setBackgroundDrawable(mGD);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getWidth();
        int height = getHeight();
        int weight = width / (TRI_NUMS + 2);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(childCount - 1 - i);
            childView.layout(
                    weight * i,
                    (int) (b - mSizes[i] * height),
                    (int) (i * weight + mSizes[i] * height),
                    b);
        }
    }
}

@@ -47,16 +47,95 @@
 rview;%0A%0A
+import android.animation.AnimatorSet;%0Aimport android.animation.ObjectAnimator;%0A
 import a
@@ -158,16 +158,16 @@
 ontext;%0A
-
 import a
@@ -697,16 +697,49 @@
 mViews;%0A
+    private AnimatorSet%5B%5D mSets;%0A
     priv
@@ -1898,32 +1898,75 @@
 loat%5BTRI_NUMS%5D;%0A
+        mSets = new AnimatorSet%5BTRI_NUMS%5D;%0A
         for (int
@@ -2094,24 +2094,66 @@
 .5f) * i));%0A
+            mSets%5Bi%5D = new AnimatorSet();%0A
             
@@ -2665,24 +2665,123 @@
 GD);%0A    %7D%0A%0A
+    @Override%0A    protected void onAttachedToWindow() %7B%0A        super.onAttachedToWindow();%0A    %7D%0A%0A
     @Overrid
@@ -3213,28 +3213,11 @@
  (b 
-- mSizes%5Bi%5D * height
++ 1
 ),%0A 
@@ -3296,17 +3296,360 @@
         
-b
+(int) (b + mSizes%5Bi%5D * height + 1));%0A            mSets%5Bi%5D.playTogether(%0A                    ObjectAnimator.ofFloat(mViews%5Bi%5D, %22translationY%22, -mSizes%5Bi%5D * height),%0A                    ObjectAnimator.ofFloat(mViews%5Bi%5D, %22alpha%22, 0f, 1f)%0A            );%0A            mSets%5Bi%5D.setStartDelay(150 + 50 * i);%0A            mSets%5Bi%5D.setDuration(600).start(
 );%0A     

add animation.