import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {Platform, Text, BackHandler, ToastAndroid, StyleSheet} from 'react-native';
import {connect} from 'react-redux';
import {createNativeStackNavigator} from '@react-navigation/native-stack';

// 忽略不必要的警告日志
import {LogBox} from 'react-native';

LogBox.ignoreLogs([
    'Non-serializable values were found in the navigation state',
    'componentWillReceiveProps',
    'EventEmitter.removeListener',
]);

import Welcome from './pages/Welcome';
import HomeStack from './pages/Desktop/Index';

const Stack = createNativeStackNavigator();
let lastBackPressed = 0;
const NavigationOptions = {
    headerStyle: {
        backgroundColor: '#5394e4',
        height: 40,
    },
    headerShadowVisible: false, // 取消导航下面的阴影
    headerTitleAlign: 'center', // 标题文字居中
    headerTintColor: '#fff',
    headerTitleStyle: {
        fontSize: 20,
        marginTop: Platform.OS === 'ios' ? 10 : 0,
    },
    headerBackTitle: Platform.OS === 'ios' ? ' ' : null,         // ~注意~ 这个地方是隐藏返回按钮文字的
};


@connect(({app, router}) => ({app, router}))
class Router extends React.PureComponent {
    // componentDidMount() {
    //     BackHandler.addEventListener('hardwareBackPress', this.backAction);
    // }
    //
    // componentWillUnmount() {
    //     BackHandler.removeEventListener('hardwareBackPress', this.backAction);
    // }
    //
    // backAction = () => {
    //     const currentScreen = getActiveRouteName(this.props.navigation.getState());
    //     const MainRouteNames = ['DesktopHome'];
    //     if (MainRouteNames.indexOf(currentScreen) > -1) {
    //         if (lastBackPressed && lastBackPressed + 2000 >= Date.now()) {
    //             //最近2秒内按过back键，可以退出应用。
    //             BackHandler.exitApp();
    //             return false;
    //         }
    //         lastBackPressed = Date.now();
    //         ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);
    //         return true;
    //     }
    // };

    render() {
        const StackDict = {
            ...HomeStack,
        };
        let stackList = [];
        for (let key in StackDict) {
            const item = {
                name: key,
                screen: StackDict[key].screen,
                options: {
                    ...NavigationOptions, ...StackDict[key].navigationOptions, ...StackDict[key].screen.navigationOptions,
                },
            };
            stackList.push(item);
        }
        return (
            <NavigationContainer>
                <Stack.Navigator>
                    {/*<Stack.Screen name="Welcome" component={Welcome} options={{headerShown: false}}/>*/}
                    {/*<Stack.Screen name="DesktopHome" component={DesktopHome} options={NavigationOptions}/>*/}
                    {/*<Stack.Screen name="SetPower" component={SetPower} options={{headerShown: true}}/>*/}
                    {
                        stackList.map((stack, index) => (
                            <Stack.Screen key={index} name={stack.name} component={stack.screen}
                                          options={stack.options}/>
                        ))
                    }

                </Stack.Navigator>
            </NavigationContainer>

        );
    }
}

const styles = StyleSheet.create({
    iconFont: {
        fontFamily: 'iconfont',
        fontSize: 20,
        color: '#757575',
    },
    iconFontFocused: {
        fontFamily: 'iconfont',
        fontSize: 20,
        color: '#2b90ea',
    },
});
export default Router;
