package com.xg.navigation.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.xg.navigation.NativeNavigationEmitter;
import com.xg.navigation.NavigationApplication;
import com.xg.navigation.constants.NavigationConstants;
import com.xg.navigation.delegates.bottom.BaseBottomDelegate;
import com.xg.navigation.delegates.bottom.BottomItemDelegate;
import com.xg.navigation.delegates.bottom.BottomTabBean;
import com.xg.navigation.delegates.bottom.ItemBuilder;
import com.xg.navigation.util.BitmapUtil;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 带有底部Tab的Fragment
 */
public class NavigationBottomDelegate extends BaseBottomDelegate {

    public static NavigationBottomDelegate newInstance(Bundle bundle) {

        if (bundle == null) {
            bundle = new Bundle();
        }

        NavigationBottomDelegate fragment = new NavigationBottomDelegate();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public LinkedHashMap<BottomTabBean, BottomItemDelegate> setItems(ItemBuilder builder) {
        final LinkedHashMap<BottomTabBean, BottomItemDelegate> items = new LinkedHashMap<>();

        Bundle bundle = getArguments();
        Bundle tabConfigs = bundle.getBundle("tabConfigs");

        if (tabConfigs == null) {
            return items;
        }

        int tabCount = tabConfigs.getInt("tabCount");
        Bundle pages = tabConfigs.getBundle("pages");

        if (pages == null) {
            return items;
        }

        for (int i = 0; i < tabCount; ++i) {
            String key = String.valueOf(i);
            Bundle currentPage = pages.getBundle(key);

            if (currentPage == null) {
                continue;
            }

            Bundle selectIconParams = currentPage.getBundle("selectTabBarIcon");
            Bundle unSelectIconParams = currentPage.getBundle("unSelectTabBarIcon");

            String pageType = currentPage.getString("pageType");
            String tabBarTitle = currentPage.getString("tabBarTitle");
            String pagePath = currentPage.getString("pagePath");
            String selectTabBarIcon = selectIconParams == null ? "" : selectIconParams.getString("uri");
            String unSelectTabBarIcon = unSelectIconParams == null ? "" : unSelectIconParams.getString("uri");

            if (TextUtils.isEmpty(pageType)) {
                continue;
            }
            if (tabBarTitle == null) {
                tabBarTitle = "";
            }

            switch (pageType) {
                case NavigationConstants.JS_PAGE: {
                    ReactItemContainer jsBridgeBaseBottomItemDelegate = new ReactItemContainer();
                    jsBridgeBaseBottomItemDelegate.setArguments(currentPage);
                    items.put(new BottomTabBean(BitmapUtil.loadImage(selectTabBarIcon),
                            BitmapUtil.loadImage(unSelectTabBarIcon), tabBarTitle), jsBridgeBaseBottomItemDelegate);
                    break;
                }
                case NavigationConstants.NATIVE_PAGE: {
                    BottomItemDelegate targetDelegate;
                    try {
                        Class targetClass = Class.forName(pagePath);
                        targetDelegate = (BottomItemDelegate) targetClass.newInstance();
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                    if (targetDelegate == null) {
                        continue;
                    }
                    targetDelegate.setArguments(currentPage);
                    items.put(new BottomTabBean(BitmapUtil.loadImage(selectTabBarIcon),
                            BitmapUtil.loadImage(unSelectTabBarIcon), tabBarTitle), targetDelegate);
                    break;
                }
                default:
                    break;
            }
        }

        return builder.addItems(items).build();
    }

    @Override
    public int setIndexDelegate() {

        Bundle bundle = getArguments();
        Bundle tabConfigs = bundle.getBundle("tabConfigs");

        if (tabConfigs == null) {
            return 0;
        }

        return tabConfigs.getInt("selectedIndex");
    }

    @Override
    public int setClickedColor() {

        int defaultColor = Color.parseColor("#ffff8800");

        Bundle tabConfig = getTabConfig();
        if (tabConfig == null) {
            return defaultColor;
        }

        int selectedColor;
        String tintColor = tabConfig.getString("tintColor");
        try {
            selectedColor = Color.parseColor(tintColor);
        } catch (Exception e) {
            selectedColor = defaultColor;
        }

        return selectedColor;
    }

    @Override
    public int setUnClickColor() {

        int defaultColor = Color.GRAY;

        Bundle tabConfig = getTabConfig();
        if (tabConfig == null) {
            return defaultColor;
        }

        int selectedColor;
        String tintColor = tabConfig.getString("unselectedItemTintColor");
        try {
            selectedColor = Color.parseColor(tintColor);
        } catch (Exception e) {
            selectedColor = defaultColor;
        }

        return selectedColor;

    }

    @Override
    public int setBarTintColor() {
        int defaultColor = Color.WHITE;

        Bundle tabConfig = getTabConfig();
        if (tabConfig == null) {
            return defaultColor;
        }

        int selectedColor;
        String tintColor = tabConfig.getString("barTintColor");
        try {
            selectedColor = Color.parseColor(tintColor);
        } catch (Exception e) {
            selectedColor = defaultColor;
        }

        return selectedColor;
    }

    @Override
    public boolean onBackPressedSupport() {
        if (getChildFragmentManager().getBackStackEntryCount() > 1) {
            getSupportDelegate().popChild();
            return true;
        } else {
            return super.onBackPressedSupport();
        }
    }

    @Override
    public void onClick(View v) {
        final int tabIndex = (int) v.getTag();


        Bundle bundle = getArguments();
        Bundle tabConfigs = bundle.getBundle("tabConfigs");

        if (tabConfigs == null) {
            return;
        }

        Bundle pages = tabConfigs.getBundle("pages");

        if (pages == null) {
            return;
        }

        Bundle currentPage = pages.getBundle(String.valueOf(tabIndex));
        if (currentPage == null) {
            return;
        }
        String pageType = currentPage.getString("pageType");
        String pagePath = currentPage.getString("pagePath");
        String uniqueId = currentPage.getString("uniqueId");

        WritableMap tabRoute = Arguments.createMap();
        tabRoute.putInt("index", tabIndex);
        tabRoute.putString("pagePath", pagePath);
        tabRoute.putString("pageType", pageType);
        tabRoute.putString("uniqueId", uniqueId);

        WritableMap tabBarRoute = Arguments.createMap();
        tabBarRoute.putString("pagePath", bundle.getString("pagePath"));
        tabBarRoute.putString("pageType", bundle.getString("pageType"));
        tabBarRoute.putString("uniqueId", bundle.getString("uniqueId"));

        WritableMap result = Arguments.createMap();
        result.putMap("tabRoute", tabRoute);
        result.putMap("tabBarRoute", tabBarRoute);

        ReactContext reactContext = ((NavigationApplication) getProxyActivity().getApplication()).getReactContext();
        NativeNavigationEmitter.sendEvent(reactContext, "XGNavigationTabClick", result);
    }

    private String generatorNavigationId() {
        return "#NavigationId" + UUID.randomUUID() + System.nanoTime();
    }

    public Map<String, Object> setNavigationParams() {
        return toMap();
    }

    private Bundle getTabConfig() {
        Bundle params = getArguments();
        return params.getBundle("tabConfigs");
    }
}
