package com.itant.shiwushu.tool;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.BottomNavigationView;
import android.view.Window;
import android.view.WindowManager;

import com.itant.shiwushu.R;
import com.itant.shiwushu.manager.ThemeManager;

/**
 * Created by Jason on 2018/10/6.
 */

public class ThemeTool {
    public static void changeStatusColor(Activity activity, int themeMode) {
        int color = setNextColorPrimaryDark(activity, themeMode);
        if (Build.VERSION.SDK_INT >= 21) {
            if (color != -1) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(color);
            }
        }
    }

    /**
     * 获取深色颜色
     * @return
     */
    private static int setNextColorPrimaryDark(Activity activity, int themeMode) {
        // 设置底部tab颜色
        int defaultColor = Color.parseColor(ColorTool.getColorHexString(activity, R.color.gray_tab_unchecked));
        int selectedColor;
        int[][] states = new int[][] {
                //new int[] { android.R.attr.state_enabled}, // enabled
                //new int[] {-android.R.attr.state_enabled}, // disabled
                //new int[] {-android.R.attr.state_checked}, // unchecked
                //new int[] { android.R.attr.state_pressed}  // pressed
                new int[] {-android.R.attr.state_checked},
                new int[] {android.R.attr.state_checked}
        };
        int[] colors;
        BottomNavigationView navigationView = activity.findViewById(R.id.navigation);
        int colorPrimaryDark;
        if (themeMode == ThemeManager.THEME_MODE_DEFAULT) {
            selectedColor = Color.parseColor(ColorTool.getColorHexString(activity, R.color.red_2));
            colorPrimaryDark = activity.getResources().getColor(R.color.red);
        } else if (themeMode == ThemeManager.THEME_MODE_RED) {
            selectedColor = Color.parseColor(ColorTool.getColorHexString(activity, R.color.blue_alipay));
            colorPrimaryDark = activity.getResources().getColor(R.color.blue_alipay_dark);
        } else {
            selectedColor = Color.parseColor(ColorTool.getColorHexString(activity, R.color.colorPrimary));
            colorPrimaryDark = activity.getResources().getColor(R.color.colorPrimaryDark);
        }
        colors = new int[] {defaultColor, selectedColor};
        ColorStateList stateList = new ColorStateList(states, colors);
        navigationView.setItemIconTintList(stateList);
        navigationView.setItemTextColor(stateList);

        //navigationView.getMenu().findItem(navigationView.getSelectedItemId()).getIcon().setColorFilter(selectedColor, PorterDuff.Mode.SRC_IN);


        return colorPrimaryDark;
    }


    public static void setStatusColor(Activity activity, int themeMode) {
        int color = getCurrentPrimaryDark(activity, themeMode);
        if (Build.VERSION.SDK_INT >= 21) {
            if (color != -1) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(color);
            }
        }
    }
    /**
     * 获取深色颜色
     * @return
     */
    private static int getCurrentPrimaryDark(Activity activity, int themeMode) {
        int colorPrimaryDark;
        if (themeMode == ThemeManager.THEME_MODE_DEFAULT) {
            colorPrimaryDark = activity.getResources().getColor(R.color.colorPrimaryDark);
        } else if (themeMode == ThemeManager.THEME_MODE_RED) {
            colorPrimaryDark = activity.getResources().getColor(R.color.red);
        } else {
            colorPrimaryDark = activity.getResources().getColor(R.color.blue_alipay_dark);
        }
        return colorPrimaryDark;
    }
}
