package com.mikepenz.materialdrawer.model;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.annotation.LayoutRes;
import android.support.v4.view.ViewCompat;
import android.view.View;

import com.mikepenz.fastadapter.utils.ViewHolderFactory;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.view.IconicsImageView;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.R;
import com.mikepenz.materialdrawer.icons.MaterialDrawerFont;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;

/**
 * Created by mikepenz on 03.02.15.
 * NOTE: The arrow will just animate (and rotate) on APIs higher than 11 as the ViewCompat will skip this on API 10
 */
public class ExpandableDrawerItem extends BasePrimaryDrawerItem<ExpandableDrawerItem, ExpandableDrawerItem.ViewHolder> {

    private Drawer.OnDrawerItemClickListener mOnDrawerItemClickListener;

    @ColorInt
    private Integer mArrowColor;

    public ExpandableDrawerItem withArrowColor(@ColorInt int color) {
        mArrowColor = color;
        return this;
    }

    @Override
    public int getType() {
        return R.id.material_drawer_item_expandable;
    }

    @Override
    @LayoutRes
    public int getLayoutRes() {
        return R.layout.material_drawer_item_expandable;
    }


    @Override
    public ExpandableDrawerItem withOnDrawerItemClickListener(Drawer.OnDrawerItemClickListener onDrawerItemClickListener) {
        mOnDrawerItemClickListener = onDrawerItemClickListener;
        return this;
    }

    @Override
    public Drawer.OnDrawerItemClickListener getOnDrawerItemClickListener() {
        return mOnArrowDrawerItemClickListener;
    }

    /**
     * our internal onDrawerItemClickListener which will handle the arrow animation
     */
    private Drawer.OnDrawerItemClickListener mOnArrowDrawerItemClickListener = new Drawer.OnDrawerItemClickListener() {
        @Override
        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
            if (drawerItem instanceof AbstractDrawerItem && drawerItem.isEnabled()) {
                if (((AbstractDrawerItem) drawerItem).getSubItems() != null) {
                    if (((AbstractDrawerItem) drawerItem).isExpanded()) {
                        ViewCompat.animate(view.findViewById(R.id.material_drawer_arrow)).rotation(180).start();
                    } else {
                        ViewCompat.animate(view.findViewById(R.id.material_drawer_arrow)).rotation(0).start();
                    }
                }
            }

            return mOnDrawerItemClickListener != null && mOnDrawerItemClickListener.onItemClick(view, position, drawerItem);
        }
    };

    @Override
    public void bindView(ViewHolder viewHolder) {
        Context ctx = viewHolder.itemView.getContext();
        //bind the basic view parts
        bindViewHelper(viewHolder);

        //make sure all animations are stopped
        viewHolder.arrow.setColor(mArrowColor != null ? mArrowColor : getIconColor(ctx));
        viewHolder.arrow.clearAnimation();
        if (!isExpanded()) {
            ViewCompat.setRotation(viewHolder.arrow, 0);
        } else {
            ViewCompat.setRotation(viewHolder.arrow, 180);
        }

        //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
        onPostBindView(this, viewHolder.itemView);
    }

    @Override
    public ViewHolderFactory<ViewHolder> getFactory() {
        return new ItemFactory();
    }

    public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
        public ViewHolder create(View v) {
            return new ViewHolder(v);
        }
    }

    protected static class ViewHolder extends BaseViewHolder {
        public IconicsImageView arrow;

        public ViewHolder(View view) {
            super(view);
            arrow = (IconicsImageView) view.findViewById(R.id.material_drawer_arrow);
            arrow.setIcon(new IconicsDrawable(view.getContext(), MaterialDrawerFont.Icon.mdf_expand_more).sizeDp(16).paddingDp(2).color(Color.BLACK));
        }
    }
}

@@ -140,24 +140,68 @@
 n.ColorInt;%0A
+import android.support.annotation.ColorRes;%0A
 import andro
@@ -537,16 +537,71 @@
 awer.R;%0A
+import com.mikepenz.materialdrawer.holder.ColorHolder;%0A
 import c
@@ -1083,40 +1083,167 @@
     
-@ColorInt%0A    private Integer mA
+protected ColorHolder arrowColor;%0A%0A    public ExpandableDrawerItem withArrowColor(@ColorInt int arrowColor) %7B%0A        this.arrowColor = ColorHolder.fromColor(a
 rrow
@@ -1239,33 +1239,61 @@
 Color(arrowColor
+)
 ;
+%0A        return this;%0A    %7D
 %0A%0A    public Exp
@@ -1328,28 +1328,39 @@
 olor
+Res
 (@Color
-Int
+Res
  int 
-c
+arrowC
 olor
+Res
 ) %7B%0A
@@ -1367,18 +1367,22 @@
         
-mA
+this.a
 rrowColo
@@ -1385,21 +1385,55 @@
 Color = 
-color
+ColorHolder.fromColorRes(arrowColorRes)
 ;%0A      
@@ -3345,18 +3345,22 @@
 etColor(
-mA
+this.a
 rrowColo
@@ -3375,10 +3375,14 @@
 l ? 
-mA
+this.a
 rrow
@@ -3386,16 +3386,30 @@
 rowColor
+.getColorInt()
  : getIc

Use ColorHolder