package aaronyi.mylib.navbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import aaronyi.mylib.R;


public class BottomNavBar extends LinearLayout {

    private Context mContext;

    private static final int TRANSLATE_DURATION_MILLIS = 200;

    private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
    private boolean mVisible = true;

    private List<NavTab> mTabs = new ArrayList<>();

    private LinearLayout mTabLayout;

    private LayoutParams mTabParams;
    private int mCurrentPosition = 0;
    private OnTabSelectedListener mListener;

    //
    private int mIconSize;
    private int mTitleSize;
    private int mSelectColor;
    private int mUnSelectColor;
    //
    private static final int DEF_ICON_SIZE = 27;
    private static final int DEF_TITLE_SIZE = 15;
    private static final int DEF_SELECT_COLOR = Color.parseColor("#F44336");
    private static final int DEF_UN_SELECT_COLOR = Color.parseColor("#c9c9c9");

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

    public BottomNavBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        setOrientation(VERTICAL);
        mTabLayout = new LinearLayout(context);
        mTabLayout.setBackgroundColor(Color.WHITE);
        mTabLayout.setOrientation(LinearLayout.HORIZONTAL);
        addView(mTabLayout, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mTabParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        mTabParams.weight = 1;
        //
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BottomNavBar);
        int n = typedArray.getIndexCount();
        int selectColor =0;
        int unSelectColor =0;
        for (int i = 0; i < n; i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.BottomNavBar_iconSize) {
                mIconSize = typedArray.getDimensionPixelSize(attr, DEF_ICON_SIZE);
            }else if (attr == R.styleable.BottomNavBar_tabTitleSize) {
                mTitleSize = typedArray.getDimensionPixelSize(attr, DEF_TITLE_SIZE);
            } else if (attr == R.styleable.BottomNavBar_selectColor) {
                selectColor = typedArray.getColor(attr, DEF_SELECT_COLOR);
            } else if (attr == R.styleable.BottomNavBar_unSelectColor) {
                unSelectColor = typedArray.getColor(attr, DEF_UN_SELECT_COLOR);
            }
        }
        typedArray.recycle();
        //
        this.mIconSize = mIconSize == 0 ? DEF_ICON_SIZE : mIconSize;
        this.mTitleSize = mTitleSize == 0 ? DEF_TITLE_SIZE : mTitleSize;
        this.mSelectColor = selectColor == 0 ? DEF_SELECT_COLOR : selectColor;
        this.mUnSelectColor = unSelectColor == 0 ? DEF_UN_SELECT_COLOR : unSelectColor;
    }

    public BottomNavBar setUnSelectColor(int mUnSelectColor) {
        this.mUnSelectColor = mUnSelectColor;
        return this;
    }

    public BottomNavBar setSelectColor(int mSelectColor) {
        this.mSelectColor = mSelectColor;
        return this;
    }

    public BottomNavBar setTitleSize(int mTitleSize) {
        this.mTitleSize = mTitleSize;
        return this;
    }

    public BottomNavBar setIconSize(int mIconSize) {
        this.mIconSize = mIconSize;
        return this;
    }

    public BottomNavBar addItem(int icon, String title){
        final NavTab tab =new NavTab(mContext,icon,mIconSize,title,mTitleSize,mSelectColor,mUnSelectColor);
        tab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener == null) return;

                int pos = tab.getTabPosition();
                if (mCurrentPosition == pos) {
                    mListener.onTabReselected(pos);
                } else {
                    mListener.onTabSelected(pos, mCurrentPosition);
                    tab.setSelected(true);
                    mListener.onTabUnselected(mCurrentPosition);
                    mTabs.get(mCurrentPosition).setSelected(false);
                    mCurrentPosition = pos;
                }
            }
        });
        tab.setTabPosition(mTabLayout.getChildCount());
        tab.setLayoutParams(mTabParams);
        mTabLayout.addView(tab);
        mTabs.add(tab);
        return this;
    }

    public BottomNavBar addItem(final NavTab tab) {
        tab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener == null) return;

                int pos = tab.getTabPosition();
                if (mCurrentPosition == pos) {
                    mListener.onTabReselected(pos);
                } else {
                    mListener.onTabSelected(pos, mCurrentPosition);
                    tab.setSelected(true);
                    mListener.onTabUnselected(mCurrentPosition);
                    mTabs.get(mCurrentPosition).setSelected(false);
                    mCurrentPosition = pos;
                }
            }
        });
        tab.setTabPosition(mTabLayout.getChildCount());
        tab.setLayoutParams(mTabParams);
        mTabLayout.addView(tab);
        mTabs.add(tab);
        return this;
    }

    public void setOnTabSelectedListener(OnTabSelectedListener onTabSelectedListener) {
        mListener = onTabSelectedListener;
    }

    public void setCurrentItem(final int position) {
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                mTabLayout.getChildAt(position).performClick();
            }
        });
    }

    public int getCurrentItemPosition() {
        return mCurrentPosition;
    }

    /**
     * 获取 Tab
     * @param index 参数
     * @return 返回值
     */
    public NavTab getItem(int index) {
        if (mTabs.size() < index) return null;
        return mTabs.get(index);
    }

    public interface OnTabSelectedListener {
        void onTabSelected(int position, int prePosition);

        void onTabUnselected(int position);

        void onTabReselected(int position);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        return new SavedState(superState, mCurrentPosition);
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());

        if (mCurrentPosition != ss.position) {
            mTabLayout.getChildAt(mCurrentPosition).setSelected(false);
            mTabLayout.getChildAt(ss.position).setSelected(true);
        }
        mCurrentPosition = ss.position;
    }

    static class SavedState extends BaseSavedState {
        private int position;

        public SavedState(Parcel source) {
            super(source);
            position = source.readInt();
        }

        public SavedState(Parcelable superState, int position) {
            super(superState);
            this.position = position;
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(position);
        }

        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }


    public void hide() {
        hide(true);
    }

    public void show() {
        show(true);
    }

    public void hide(boolean anim) {
        toggle(false, anim, false);
    }

    public void show(boolean anim) {
        toggle(true, anim, false);
    }

    public boolean isVisible() {
        return mVisible;
    }

    private void toggle(final boolean visible, final boolean animate, boolean force) {
        if (mVisible != visible || force) {
            mVisible = visible;
            int height = getHeight();
            if (height == 0 && !force) {
                ViewTreeObserver vto = getViewTreeObserver();
                if (vto.isAlive()) {
                    // view树完成测量并且分配空间而绘制过程还没有开始的时候播放动画。
                    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        @Override
                        public boolean onPreDraw() {
                            ViewTreeObserver currentVto = getViewTreeObserver();
                            if (currentVto.isAlive()) {
                                currentVto.removeOnPreDrawListener(this);
                            }
                            toggle(visible, animate, true);
                            return true;
                        }
                    });
                    return;
                }
            }
            int translationY = visible ? 0 : height;
            if (animate) {
                animate().setInterpolator(mInterpolator)
                        .setDuration(TRANSLATE_DURATION_MILLIS)
                        .translationY(translationY);
            } else {
                ViewCompat.setTranslationY(this, translationY);
            }
        }
    }
}
