import { useState, useCallback } from 'react';
import { ModalType, ModalPosition, ModalConfig } from '../core/types';

export const useModalCommands = () => {
    const [modals, setModals] = useState<ModalConfig[]>([]);
    const [layout, setLayout] = useState<'horizontal' | 'vertical'>('horizontal');

    const calculatePositions = useCallback((count: number, layout: 'horizontal' | 'vertical') => {
        const positions: ModalPosition[] = [];
        const gap = 10;

        if (layout === 'horizontal') {
            const width = (100 - gap * (count - 1)) / count;
            for (let i = 0; i < count; i++) {
                positions.push({
                    x: i * (width + gap),
                    y: 0,
                    width: `${width}%`,
                    height: '100%'
                });
            }
        } else {
            const height = (100 - gap * (count - 1)) / count;
            for (let i = 0; i < count; i++) {
                positions.push({
                    x: 0,
                    y: i * (height + gap),
                    width: '100%',
                    height: `${height}%`
                });
            }
        }

        return positions;
    }, []);

    const createModal = useCallback((params: { type: ModalType; position?: ModalPosition; content: unknown }) => {
        const newModal: ModalConfig = {
            id: Date.now().toString(),
            type: params.type,
            position: params.position || {
                x: 0,
                y: 0,
                width: '100%',
                height: '100%'
            },
            content: params.content
        };

        return new Promise<ModalConfig>((resolve) => {
            setModals(prev => {
                const newModals = [...prev, newModal];
                resolve(newModal);
                return newModals;
            });
        });
    }, []);

    const removeModal = useCallback((id: string) => {
        setModals(prev => prev.filter(modal => modal.id !== id));
    }, []);

    const updateModalStyle = useCallback((id: string, style: { backgroundColor?: string }) => {
        setModals(prev =>
            prev.map(modal =>
                modal.id === id ? { ...modal, style: { ...modal.style, ...style } } : modal
            )
        );
    }, []);

    const splitScreen = useCallback((modalIds: string[], newLayout: 'horizontal' | 'vertical') => {
        setLayout(newLayout);
        setModals(prev => {
            const targetModals = prev.filter(modal => modalIds.includes(modal.id));
            const otherModals = prev.filter(modal => !modalIds.includes(modal.id));

            const positions = calculatePositions(targetModals.length, newLayout);
            const updatedTargetModals = targetModals.map((modal, index) => ({
                ...modal,
                position: positions[index]
            }));

            return [...otherModals, ...updatedTargetModals];
        });
    }, [calculatePositions]);

    const getModalsByPosition = useCallback((position: 'left' | 'right' | 'top' | 'bottom') => {
        return modals.filter(modal => {
            switch (position) {
                case 'left':
                    return modal.position.x === 0;
                case 'right':
                    return modal.position.x > 0;
                case 'top':
                    return modal.position.y === 0;
                case 'bottom':
                    return modal.position.y > 0;
                default:
                    return false;
            }
        });
    }, [modals]);

    return {
        modals,
        createModal,
        removeModal,
        updateModalStyle,
        splitScreen,
        getModalsByPosition,
        layout
    };
}; 