import {
    useImperativeHandle,
    useLayoutEffect,
    forwardRef,
    useEffect,
    useState,
    Fragment,
    useRef
} from "react";
import {
    Platform,
    Animated,
    Easing,
    View
} from "react-native";
import {
    type EmbeddedMenuButton as EmbeddedMenuButtonType
} from "./components/menuButton/type";
import {
    type IEmbeddedMenuRef
} from "./type";
import type IEmbeddedMenuProps from "./type";
import stylesheet, {
    useStyles
} from "./stylesheet";
import MenuButton from "./components/menuButton";
import {
    NCoreUIKitEmbeddedMenu,
    NCoreUIKitTheme
} from "../../core/hooks";
import type {
    RefForwardingComponent
} from "../../types";
import {
    PanelLeftClose,
    PanelLeftOpen
} from "lucide-react-native";
import {
    SafeAreaView
} from "react-native-safe-area-context";
import Button from "../button";
import Seperator from "../seperator";
import SiteLogo from "../siteLogo";

const Menu: RefForwardingComponent<IEmbeddedMenuRef, IEmbeddedMenuProps> = ({
    isWorkWithSafeAreaView = true,
    isWorkWithAnimation = true,
    renderHeader: RenderHeader,
    renderFooter: RenderFooter,
    close: closeAction,
    maxWidth = 300,
    minWidth = 75,
    siteLogoProps,
    customTheme,
    navigation,
    isCollapse,
    onClosed,
    onOpened,
    buttons,
    onClose,
    onOpen,
    style,
    id,
    ...props
}, ref) => {
    const {
        colors,
        spaces
    } = NCoreUIKitTheme.useContext(customTheme);

    const [
        isActive,
        setIsActive
    ] = useState(false);

    const {
        headerContentContainer: headerContentContainerDynamicStyle,
        headerContainer: headerContainerDynamicStyle,
        collapseButton: collapseButtonDynamicStyle,
        seperator: seperatorDynamicStyle,
        container: containerDynamicStyle,
        siteLogo: siteLogoDynamicStyle
    } = useStyles({
        isCollapse: isActive,
        colors,
        spaces
    });

    const animatedX = useRef(new Animated.Value(!isCollapse ? minWidth : maxWidth)).current;

    useImperativeHandle(
        ref,
        () => ({
            close,
            open
        }),
        []
    );

    useEffect(() => {
        if(isCollapse !== undefined) setIsActive(isCollapse);
    }, [isCollapse]);

    useLayoutEffect(() => {
        if(isActive) {
            if(isWorkWithAnimation) {
                openAnimation();
            } else {
                if(onOpen) onOpen({
                    id
                });

                animatedX.setValue(maxWidth);

                if(onOpened) onOpened({
                    id
                });
            }
        } else {
            if(isWorkWithAnimation) {
                closeAnimation();
            } else {
                if(onClose) onClose({
                    id
                });

                animatedX.setValue(minWidth);

                if(onClosed) onClosed({
                    id
                });
            }
        }
    }, [
        isActive
    ]);

    const open = () => {
        setIsActive(true);
    };

    const close = () => {
        setIsActive(false);
    };

    const openAnimation = () => {
        if(onOpen) onOpen({
            id
        });

        Animated.timing(animatedX, {
            useNativeDriver: Platform.OS !== "web",
            easing: Easing.linear,
            toValue: maxWidth,
            duration: 300
        }).start(({
            finished
        }) => {
            if(finished) {
                if(onOpened) onOpened({
                    id
                });
            }
        });
    };

    const closeAnimation = () => {
        if(onClose) onClose({
            id
        });

        Animated.timing(animatedX, {
            useNativeDriver: Platform.OS !== "web",
            easing: Easing.linear,
            toValue: minWidth,
            duration: 300
        }).start(({
            finished
        }) => {
            if(finished) {
                if(onClosed) onClosed({
                    id
                });
            }
        });
    };

    const updateMenuButtonCollabs = ({
        depthMap,
        status
    }: {
        depthMap: Array<number>;
        status: boolean;
    }) => {
        const state = NCoreUIKitEmbeddedMenu.get({
            id
        });

        if(!state) {
            return;
        }

        const recursivelyCatch = (item: Array<EmbeddedMenuButtonType>, index = 0): Array<EmbeddedMenuButtonType> => {
            const tIndex = depthMap[index];

            return item.map((c_item, c_index) => {
                if(c_index !== tIndex) {
                    return c_item;
                }

                if(index === depthMap.length - 1) {
                    return {
                        ...c_item,
                        isCollapse: status
                    };
                }

                return {
                    ...c_item,
                    subButtons: recursivelyCatch(c_item.subButtons as Array<EmbeddedMenuButtonType>, index + 1)
                };
            });
        };

        const newButtons = recursivelyCatch(state.buttons);

        state.buttons = newButtons;

        NCoreUIKitEmbeddedMenu.update({
            menuData: state,
            id: id
        });
    };

    const renderToggleCollapseForCollabsed = () => {
        if(isActive) {
            return null;
        }

        return <Button
            isCustomPadding={true}
            spreadBehaviour="free"
            type="neutral"
            size="medium"
            icon={({
                color,
                size
            }) => {
                return <PanelLeftOpen
                    color={colors.content.icon[color]}
                    size={size * 2}
                />;
            }}
            style={{
                ...collapseButtonDynamicStyle
            }}
            onPress={() => {
                setIsActive(!isActive);
            }}
        />;
    };

    const renderToggleCollapseForExpended = () => {
        if(!isActive) {
            return null;
        }

        return <Button
            isCustomPadding={true}
            spreadBehaviour="free"
            type="neutral"
            size="medium"
            icon={({
                color,
                size
            }) => {
                return <PanelLeftClose
                    color={colors.content.icon[color]}
                    size={size * 2}
                />;
            }}
            style={{
                ...collapseButtonDynamicStyle
            }}
            onPress={() => {
                setIsActive(!isActive);
            }}
        />;
    };

    const renderHeader = () => {
        if(RenderHeader) {
            return <RenderHeader
                isCollapse={isActive}
                maxWidth={maxWidth}
                minWidth={minWidth}
            />;
        }

        return <View
            style={[
                stylesheet.headerContainer,
                headerContainerDynamicStyle
            ]}
        >
            {renderToggleCollapseForCollabsed()}
            <View
                style={[
                    stylesheet.headerContentContainer,
                    headerContentContainerDynamicStyle
                ]}
            >
                <SiteLogo
                    {...siteLogoProps}
                    title={isActive ? siteLogoProps?.title : undefined}
                    size={isActive ? siteLogoProps?.size : "small"}
                    style={[
                        siteLogoProps?.style,
                        siteLogoDynamicStyle
                    ]}
                />
                {renderToggleCollapseForExpended()}
            </View>
            <Seperator
                style={[
                    stylesheet.seperator,
                    seperatorDynamicStyle
                ]}
            />
        </View>;
    };

    const renderButtons = () => {
        if(!buttons || !buttons.length) {
            return null;
        }

        return buttons.map((buttonItem, buttonIndex) => {
            return <MenuButton
                key={`embedded-menu-button-${id}-${buttonIndex}`}
                updateMenuButtonCollabs={updateMenuButtonCollabs}
                closeAction={closeAction}
                buttonIndex={buttonIndex}
                isMenuCollabs={isActive}
                depthMap={[buttonIndex]}
                buttonItem={buttonItem}
                navigation={navigation}
                id={id}
            />;
        });
    };

    const renderFooter = () => {
        if(!RenderFooter) {
            return null;
        }

        return <RenderFooter
            isCollapse={isActive}
            maxWidth={maxWidth}
            minWidth={minWidth}
        />;
    };

    const renderSafeAreaContext = () => {
        if(!isWorkWithSafeAreaView) {
            return <Fragment>
                {renderHeader()}
                <View
                    style={[
                        stylesheet.content
                    ]}
                >
                    {renderButtons()}
                </View>
                {renderFooter()}
            </Fragment>;
        }

        return <SafeAreaView
            style={[
                stylesheet.safeAreaView
            ]}
        >
            {renderHeader()}
            <View
                style={[
                    stylesheet.content
                ]}
            >
                {renderButtons()}
            </View>
            {renderFooter()}
        </SafeAreaView>;
    };

    const renderContent = () => {
        return <Animated.View
            {...props}
            style={[
                style,
                stylesheet.container,
                containerDynamicStyle,
                {
                    width: animatedX
                }
            ]}
        >
            {renderSafeAreaContext()}
        </Animated.View>;
    };

    return renderContent();
};
export default forwardRef(Menu);
