import {
    useLayoutEffect,
    useRef
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    PageContainer,
    CodeViewer,
    Button,
    Header,
    Menu,
    Text
} from "ncore-ui-kit";
import {
    useNavigation
} from "@react-navigation/native";
import type RootStackParamList from "../../../navigation/type";
import type {
    NativeStackNavigationProp
} from "@react-navigation/native-stack";
import type {
    IMenuRef
} from "ncore-ui-kit";
import {
    SettingsIcon,
    HomeIcon
} from "lucide-react-native";

const MenuPage = () => {
    const {
        localize
    } = NCoreUIKitLocalize.useContext();

    const {
        spaces
    } = NCoreUIKitTheme.useContext();

    const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();

    const menuRef = useRef<IMenuRef>(null);

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("menu-title")}
                isWrapSafeareaContext={false}
                navigation={navigation}
                isGoBackEnable={true}
            />,
            headerShown: true
        });
    }, []);

    return <PageContainer
        scrollViewProps={{
            contentContainerStyle: stylesheet.scrollContent
        }}
        isWorkWithHeaderSpace={false}
        scrollViewStyle={[
            stylesheet.container
        ]}
        isScrollable={true}
    >
        <View
            style={[
                stylesheet.contentContainer
            ]}
        >
            <Text
                style={stylesheet.descText}
                variant="bodyMediumSize"
                color="mid"
            >
                {localize("menu-desc")}
            </Text>
            <Button
                onPress={() => menuRef.current?.open()}
                title={localize("menu-openMenu")}
                spreadBehaviour="stretch"
            />
            <View
                style={{
                    marginVertical: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    Menu\n} from \"ncore-ui-kit\";\n\n<Menu\n    buttons={[\n        { title: \"Settings\" },\n        { title: \"Home\" }\n    ]}\n/>"}
                    language="tsx"
                />
            </View>
        </View>
        <Menu
            navigation={navigation as unknown as NCoreUIKit.Navigation}
            buttons={[
                {
                    title: localize("menu-settings"),
                    icon: ({
                        color
                    }) => <SettingsIcon
                        color={color}
                    />
                },
                {
                    title: localize("menu-home"),
                    icon: ({
                        color
                    }) => <HomeIcon
                        color={color}
                    />
                }
            ]}
            id="menu-page-example"
            ref={menuRef}
        />
    </PageContainer>;
};
export default MenuPage;
