import {
    useEffect,
    useState
} from "react";
import {
    View
} from "react-native";
import type IMainHeader from "./type";
import stylesheet, {
    useStyles
} from "./stylesheet";
import {
    NCoreUIKitTheme
} from "../../core/hooks";
import {
    SafeAreaView
} from "react-native-safe-area-context";
import {
    Portal
} from "../../helpers/portalize";
import SiteLogo from "../siteLogo";

const MainHeader = ({
    isWorkWithSafeAreaView = true,
    isWorkWithStickySpace = false,
    backgroundColor = "default",
    isSetAutoHeaderSpace = true,
    isWorkWithSeperator = false,
    portalName = "main-header",
    isWorkWithPortal = false,
    isWorkWithSticky = false,
    renderRight: RenderRight,
    renderLeft: RenderLeft,
    customBackgroundColor,
    onContentHeight,
    customLocalize,
    siteLogoProps,
    customTheme,
    glassEffect,
    children,
    style,
    ...props
}: IMainHeader) => {
    const {
        borders,
        spaces,
        colors
    } = NCoreUIKitTheme.useContext();

    const {
        safeAreaView: safeAreaViewDynamicStyle,
        container: containerDynamicStyle
    } = useStyles({
        isWorkWithSafeAreaView,
        customBackgroundColor,
        isWorkWithSeperator,
        isWorkWithSticky,
        backgroundColor,
        glassEffect,
        borders,
        spaces,
        colors
    });

    const [
        contentHeight,
        setContentHeight
    ] = useState<null | number>(null);

    useEffect(() => {
        if(onContentHeight) onContentHeight(contentHeight);
    }, [
        contentHeight
    ]);

    const renderRight = () => {
        if(RenderRight) {
            return <RenderRight/>;
        }

        return <View/>;
    };

    const renderLeft = () => {
        if(RenderLeft) {
            return <RenderLeft/>;
        }

        return <SiteLogo
            customLocalize={customLocalize}
            customTheme={customTheme}
            isWorkWithFlex1={false}
            size="medium"
            {...siteLogoProps}
        />;
    };

    const renderContent = () => {
        return <View
            {...props}
            onLayout={(event) => {
                if(props && props.onLayout) {
                    props.onLayout(event);
                }

                if(isWorkWithSticky) {
                    const _contentHeight = event.nativeEvent.layout.height;

                    setContentHeight(_contentHeight);

                    if(isSetAutoHeaderSpace) {
                        NCoreUIKitTheme.updateConfigs({
                            headerSpace: _contentHeight
                        });
                    }
                }
            }}
            style={[
                style,
                stylesheet.container,
                containerDynamicStyle
            ]}
        >
            {renderLeft()}
            {renderRight()}
        </View>;
    };

    const renderContentContainer = () => {
        if(isWorkWithPortal) {
            return <Portal
                name={portalName}
            >
                {renderContent()}
            </Portal>;
        }

        return renderContent();
    };

    const renderSafeAreaView = () => {
        return <SafeAreaView
            style={[
                safeAreaViewDynamicStyle
            ]}
            edges={[
                "top"
            ]}
        >
            {renderContentContainer()}
        </SafeAreaView>;
    };

    const renderWithChildren = () => {
        return <View
            style={[
                stylesheet.baseContainer
            ]}
        >
            {isWorkWithSafeAreaView ? renderSafeAreaView() : renderContentContainer()}
            {
                isWorkWithSticky && isWorkWithStickySpace ?
                    <View
                        style={[
                            stylesheet.stickySpacer,
                            {
                                backgroundColor: customBackgroundColor ? colors.project[customBackgroundColor] : colors.content.container[backgroundColor],
                                height: contentHeight
                            }
                        ]}
                    />
                :
                    null
            }
            {children}
        </View>;
    };

    if(children) {
        return renderWithChildren();
    }

    return isWorkWithSafeAreaView ? renderSafeAreaView() : renderContentContainer();
};
export default MainHeader;
