import {
    useLayoutEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import stylesheet from "./stylesheet";
import {
    NCoreUIKitLocalize,
    NCoreUIKitTheme,
    PageContainer,
    CodeViewer,
    TextInput,
    Header,
    Switch,
    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 HeaderPage = () => {
    const {
        radiuses,
        borders,
        spaces,
        colors
    } = NCoreUIKitTheme.useContext();

    const {
        localize
    } = NCoreUIKitLocalize.useContext();

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

    const [
        title,
        setTitle
    ] = useState<string>("Header Example");

    const [
        isGoBackEnable,
        setIsGoBackEnable
    ] = useState<boolean>(true);

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

    return <PageContainer
        scrollViewProps={{
            contentContainerStyle: stylesheet.scrollContent
        }}
        scrollViewStyle={[stylesheet.container]}
        isWorkWithHeaderSpace={false}
        isScrollable={true}
    >
        <View
            style={[stylesheet.contentContainer]}
        >
            <Text
                variant="bodyMediumSize"
                color="mid"
                style={stylesheet.marginContainer}
            >
                {localize("header-desc")}
            </Text>
            <View
                style={[
                    stylesheet.paddedMarginContainer,
                    {
                        borderColor: colors.content.border.subtle,
                        marginBottom: spaces.spacingMd,
                        borderRadius: radiuses.form,
                        borderWidth: borders.line,
                        padding: spaces.spacingMd
                    }
                ]}
            >
                <Header
                    isWrapSafeareaContext={false}
                    isGoBackEnable={isGoBackEnable}
                    navigation={navigation}
                    title={title}
                />
            </View>
            <View
                style={{
                    marginBottom: spaces.spacingMd
                }}
            >
                <Text
                    variant="headlineSmallSize"
                    style={{
                        marginBottom: spaces.spacingSm
                    }}
                >
                    {localize("usage")}
                </Text>
                <CodeViewer
                    code={"import {\n    Header\n} from \"ncore-ui-kit\";\n\n<Header\n    title=\"Header Example\"\n    isGoBackEnable={true}\n/>"}
                    language="tsx"
                />
            </View>
            <TextInput
                placeholder={localize("header-enterTitle")}
                title={localize("header-titleLabel")}
                style={{
                    marginBottom: spaces.spacingMd
                }}
                spreadBehaviour="stretch"
                onChangeText={setTitle}
                value={title}
            />
            <View
                style={stylesheet.rowSpaceBetween}
            >
                <Switch
                    onPress={() => setIsGoBackEnable(!isGoBackEnable)}
                    title={localize("header-toggleGoBack")}
                    isActive={isGoBackEnable}
                    spreadBehaviour="free"
                />
            </View>
        </View>
    </PageContainer>;
};
export default HeaderPage;
