package com.app.kongsin.sliduplayout;

import android.animation.Animator;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by kongsin on 3/18/2017.
 */

public class Item extends FrameLayout {

    private RelativeLayout  mRootItemLayout;
    private LinearLayout    mContentItem;
    private TextView        mItemText;
    private ImageView       mItemImage;
    private View            mShadow;

    public Item(Context context) {
        super(context);
        initView(null);
    }

    public Item(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public Item(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public Item(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(attrs);
    }

    private void initView(@Nullable AttributeSet attributeSet) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.item_layout, this);
        mRootItemLayout = (RelativeLayout) findViewById(R.id.rootItemGroup);
        mContentItem = (LinearLayout) findViewById(R.id.linearLayout_itemContent_itemLayout);
        mItemText = (TextView) findViewById(R.id.textView_itemText_itemLayout);
        mItemImage = (ImageView) findViewById(R.id.imageView_itemImage_itemLayout);
        mShadow = findViewById(R.id.view);

        if (attributeSet != null){
            int[] attrsArray = new int[] {
                    android.R.attr.id, // 0
                    android.R.attr.background, // 1
                    android.R.attr.layout_width, // 2
                    android.R.attr.layout_height // 3
            };
            TypedArray a = getContext().obtainStyledAttributes(attributeSet, attrsArray);
            int color = a.getColor(1, Color.DKGRAY);
            setBackgroundColor(color);
            a.recycle();
        }

        post(new Runnable() {
            @Override
            public void run() {
                final int rotation = getContext().getResources().getConfiguration().orientation;
                if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
                    mItemImage.setVisibility(GONE);
                }
            }
        });

    }

    public void showShadow(boolean show){
        mShadow.setVisibility(show ? VISIBLE : INVISIBLE);
    }

    public void showImage(final boolean show){
        final int rotation = getContext().getResources().getConfiguration().orientation;
        if (rotation == Configuration.ORIENTATION_PORTRAIT) {
            mItemImage.animate().alpha(show ? 1 : 0).start();
        } else {
            mItemImage.setVisibility(GONE);
        }
    }

    public void setItemText(String text) {
        this.mItemText.setText(text);
    }

    @Override
    public void setBackgroundColor(@ColorInt int color) {
        if (mContentItem != null)
        mContentItem.setBackgroundColor(color);
    }

    @Override
    public void setBackground(Drawable background) {
        if (mContentItem != null)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mContentItem.setBackground(background);
        } else {
            mContentItem.setBackgroundDrawable(background);
        }
    }

    @Override
    public void setBackgroundResource(@DrawableRes int resid) {
        if (mContentItem != null)
        mContentItem.setBackgroundResource(resid);
    }
}

@@ -2297,119 +2297,267 @@
     
-int%5B%5D attrs
+Typed
 Array 
+a 
 = 
-new int%5B%5D %7B%0A                    android.R.attr.id, // 0%0A                    android.R.attr.b
+getContext().obtainStyledAttributes(attributeSet, R.styleable.Item);%0A            int count = a.getIndexCount();%0A            for (int i = 0; i %3C count; i++) %7B%0A                switch (a.getIndex(i))%7B%0A                    case R.styleable.Item_tabB
 ackg
@@ -2565,14 +2565,9 @@
 ound
-, // 1
+:
 %0A   
@@ -2587,95 +2587,197 @@
     
-android.R.attr.layout_width, // 2%0A                    android.R.attr.layout_height // 3
+    int color = a.getColor(a.getIndex(i), Color.DKGRAY);%0A                        setBackgroundColor(color);%0A                        break;%0A                    case R.styleable.Item_tabText:
 %0A   
@@ -2789,11 +2789,8 @@
     
-%7D;%0A
     
@@ -2801,177 +2801,358 @@
     
-TypedArray a
+String text
  = 
+a.
 get
-Context().obtainStyledAttributes(attributeSet, attrsArray);%0A            int color = a.getColor(1, Color.DKGRAY);%0A            setBackgroundColor(color);
+String(a.getIndex(i));%0A                        mItemText.setText(text);%0A                        break;%0A                    case R.styleable.Item_tabIcon:%0A                        Drawable icon = a.getDrawable(a.getIndex(i));%0A                        setBackground(icon);%0A                        break;%0A                %7D%0A            %7D
 %0A   

update code