import {
    useLayoutEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    MarkdownViewer,
    PageContainer,
    TextAreaInput,
    CodeViewer,
    Header,
    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";

const MarkdownViewerPage = () => {
    const {
        radiuses,
        borders,
        spaces,
        colors
    } = NCoreUIKitTheme.useContext();

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

    const [
        content,
        setContent
    ] = useState<string>(
        "# Markdown Viewer\n\nThis is a **MarkdownViewer** component from *NCore UI Kit*.\n\n- It supports lists\n- And bold text",
    );

    useLayoutEffect(() => {
        navigation.setOptions({
            header: () => <Header
                title={localize("markdownViewer-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.marginContainer}
                variant="bodyMediumSize"
                color="mid"
            >
                {localize("markdownViewer-desc")}
            </Text>
            <TextAreaInput
                placeholder={localize("markdownViewer-enterContent")}
                title={localize("markdownViewer-contentLabel")}
                spreadBehaviour="stretch"
                onChangeText={setContent}
                isUpdateOnRealtime={true}
                value={content}
            />
            <View
                style={[
                    stylesheet.paddedMarginContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        borderRadius: radiuses.form,
                        marginTop: spaces.spacingMd,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <MarkdownViewer
                    content={content}
                />
            </View>
            <View
                style={{
                    marginTop: spaces.spacingMd
                }}
            >
                <Text
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                    variant="headlineSmallSize"
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    MarkdownViewer\n} from \"ncore-ui-kit\";\n\n<MarkdownViewer\n    content=\"# Hello World\"\n/>"}
                    language="tsx"
                />
            </View>
        </View>
    </PageContainer>;
};
export default MarkdownViewerPage;
