package com.ym.library.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.ym.library.R;

import java.util.ArrayList;

public class BottomNavigationHelper {

    private static int mSelectedId = 1;
    private static ArrayList<NavigationBottomTab> mBottomNavigationTabs = new ArrayList<>();
    private static ArrayList<Integer> mBottomNavigationIds = new ArrayList<>();

    private static NavigationBottomView.OnTabSelectedListener mTabSelectedListener;

    public static void setTabSelectedListener(NavigationBottomView.OnTabSelectedListener tabSelectedListener) {
        mTabSelectedListener = tabSelectedListener;
    }

    public static void selectTabInternal(int id) {
        mSelectedId = id;
        for (int i = 0; i < 4; i++) {
            if (i == mSelectedId) {
                mBottomNavigationTabs.get(getPosition(mSelectedId)).select();
            } else {
                if (mSelectedId == 2) {
                    mBottomNavigationTabs.get(getPosition(i)).taskUnselect();
                } else {
                    mBottomNavigationTabs.get(getPosition(i)).unSelect();
                }
            }
        }
        if (mTabSelectedListener != null) {
            mTabSelectedListener.onTabSelected(id);
        }
    }

    public static int getPosition(int id) {
        int pos = mBottomNavigationIds.indexOf(id);
        return pos == -1 ? 0 : pos;
    }

    public static void switchMode(String codeDay) {
        for (NavigationBottomTab tab : mBottomNavigationTabs) {
            if (tab.mId != 13) {
                tab.setNavigationIcon(codeDay);
//                tab.setSelectColor(Color.parseColor("#" + WeatherDrawableUtils.INSTANCE.getNavigationTextColor(codeDay)));
            }

            if (tab.mId == 13) {
//                tab.setSelectColor(AppliContext.get().getResources().getColor(R.color.white));
            }
        }
    }

    public static int getSelectedId() {
        return mSelectedId;
    }

    static void bindTabWithData(NavigationBottomView view, BottomNavigationItem data) {
        Context context = view.getContext();
        String title = data.getTitle();
        Drawable icon = data.getIcon(context);
        Drawable selectIcon = data.getSelectedIcon(context);
        Drawable taskIcon = data.getTaskIcon(context);
        int color = R.color.white;
        int seletColor = R.color.color_ff3333;
        NavigationBottomTab tabView = new NavigationBottomTab(context, title, icon, selectIcon, taskIcon, color, seletColor, data.getmId(), data.isShowDrop());
        mBottomNavigationIds.add(data.getmId());
        view.addView(tabView);
        tabView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectTabInternal(v.getId());
            }
        });
        mBottomNavigationTabs.add(tabView);

    }

    public static void clear() {
        mBottomNavigationTabs.clear();
        mBottomNavigationIds.clear();
    }

    public static void setDropShowOrHint(boolean isClockIn) {
        if (mBottomNavigationTabs != null && mBottomNavigationTabs.size() > 3) {
            mBottomNavigationTabs.get(2).setDropShowOrHint(isClockIn);
        }
    }

    public static void setBottomTaskNum(int tabId,int num){
        mBottomNavigationTabs.get(getPosition(tabId)).setBottomNum(num);
    }

}
