import React, { useContext } from 'react';
import PeerView from '../../components/video/PeerView';
/* import AloneHere from '../../components/AloneHere';
import WaitingRoomMessage from '../../components/WaitingRoomMessage'; */
import { ConfigServiceContext } from '../../utils/ConfigProvider';
import { Flex } from '../../themes/components';
import { useWindowDimensions } from 'react-native';
import PaginationDots from '../../components/Pagination';
import { useSelector } from 'react-redux';
import { useOrientation } from '../../utils/browser-utils';
const peersPerColumn = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(2),
    5: Array(5).fill(2),
    4: Array(4).fill(2),
    3: [1, 2, 2],
    2: [1, 1],
    1: [1],
};
const peersPerRowMobile = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(2),
    5: [2, ...Array(4).fill(2)],
    4: Array(4).fill(2),
    3: [1, 2, 2],
    2: [1, 1],
    1: [1],
};
const peersPerRow = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(3),
    5: [...Array(2).fill(2), ...Array(3).fill(3)],
    4: Array(4).fill(2),
    3: Array(3).fill(2),
    2: Array(2).fill(2),
    1: [1],
};
/*  Config for mobile, heights for different grid configurations
    */
const peersPerColumnMobile = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(3),
    5: [...Array(2).fill(2), ...Array(3).fill(3)],
    4: Array(4).fill(2),
    3: Array(3).fill(2),
    2: Array(2).fill(2),
    1: [1],
};
const MultiSpeakerGridLayout = (props) => {
    const { sessionConfig } = useContext(ConfigServiceContext);
    const windowDimensions = useWindowDimensions();
    let { width, height } = sessionConfig.theme.dimensions || windowDimensions;
    const isMobile = width < 1024;
    const { orientation, orientationType } = useOrientation();
    const mobileLandscape = orientation === 'LANDSCAPE';
    const { peers, waitingRoom, widthOffset, } = props;
    let heightOffset = 0;
    // Fixed height header and control bars 56px Header + 56px Control bar
    if (sessionConfig.theme.header.is_enabled) {
        heightOffset -= 72;
    }
    if (sessionConfig.theme.control_bar.is_enabled
        && (!waitingRoom || sessionConfig.theme.waiting_room.enable_preview)) {
        heightOffset -= 54;
    }
    // for mobile landscape, the parent grid height width are reverse, and rotated 90 degree,
    // but we want the video to not be rotated, so mobile width is subtracted with header, controlbar
    if (mobileLandscape) {
        // [height,width] = [width,height];
        height += widthOffset;
        width += heightOffset + 72;
    }
    else {
        height += heightOffset;
        width -= widthOffset;
    }
    const [paginationHeightToSubtract, setPaginationHeightToSubtract] = React.useState(0);
    const isAlone = peers.length === 1;
    // show alone component when isAlone, not on mobile
    // and not in waiting room, and not disabled by config
    const showAloneComponent = isAlone && !isMobile && !waitingRoom;
    // when in waitingRoom, extra grid items would be 0 if
    // on mobile if showSelfVideo is not set false, othewise 1
    const waitingOffset = waitingRoom
        && (!isMobile || !sessionConfig.theme.waiting_room.enable_preview);
    // if either alone component or waiting component is there, no. of grid +1
    const peerOffset = (showAloneComponent || waitingOffset) && 1;
    // total number of grid items
    let videoGridNumber = peers.length + peerOffset;
    if (videoGridNumber > 6) {
        videoGridNumber = 9;
    }
    const joinedPeers = useSelector((state) => state.peers.joinedPeers);
    const noOfPages = Math.ceil(((joinedPeers.length + 1) / 6));
    React.useEffect(() => {
        if (noOfPages > 1) {
            if (peers.length === 1) {
                setPaginationHeightToSubtract(16); // 2 + 16 + 18
            }
            else if (peers.length <= 4) {
                setPaginationHeightToSubtract(7); // 4 + 14 = 18
            }
            else if (peers.length === 5) {
                setPaginationHeightToSubtract(12);
            }
            else {
                setPaginationHeightToSubtract(4); // 6 + 12 = 18
            }
        }
    }, [peers, noOfPages]);
    const calcHeightDivisions = (divisions, padding) => Math.floor((height - 6) / divisions);
    const calcWidthDivisions = (divisions, padding) => Math.floor((width - 6) / divisions);
    const getHeight = (videoGridNum, index) => {
        if (isMobile && !mobileLandscape) {
            return calcHeightDivisions(peersPerColumnMobile[videoGridNum][index], 3);
        }
        return calcHeightDivisions(peersPerColumn[videoGridNum][index], mobileLandscape ? 3 : 5);
    };
    const getWidth = (videoGridNum, index) => {
        if (isMobile && !mobileLandscape) {
            return calcWidthDivisions(peersPerRowMobile[videoGridNum][index], 3);
        }
        return calcWidthDivisions(peersPerRow[videoGridNum][index], mobileLandscape ? 3 : 5);
    };
    const peersMap = () => peers.map((peer, index) => {
        /* return (<Box height={getHeight(videoGridNumber, index + peerOffset)}
            width={getWidth(videoGridNumber, index
                + peerOffset)} bg={`#${5+index}${5+index}${5+index}`}>
        </Box>)
        */
        if (peer) {
            const height = getHeight(videoGridNumber, index + peerOffset) - paginationHeightToSubtract;
            return (<PeerView key={`peer_${peer.id}`} height={height - (mobileLandscape ? 2 : 0)} width={getWidth(videoGridNumber, index + peerOffset)} peer={peer} totalPeers={peers.length} isMobile={isMobile}/>);
        }
        return null;
    });
    // eslint-disable-next-line no-nested-ternary
    const direction = mobileLandscape ? (videoGridNumber === 5 ? 'row' : 'column') : (videoGridNumber === 5 ? 'column' : 'row');
    return (<Flex position="relative" height={height} width={width} bg="secondary.1000" marginLeft={mobileLandscape
        && orientationType !== 'landscapeLeft'
        && sessionConfig.theme.header.is_enabled ? 56 : 0} padding={isMobile ? 3 : 5}>
			<Flex direction={direction} flexWrap="wrap" justify="center">
				
				
				{(!waitingRoom || (sessionConfig.theme.waiting_room.enable_preview && !isMobile))
        ? (<>
							{peersMap()}

						</>) : null}

			</Flex>
			<Flex width={width} justify="center" position="absolute" bottom="2">
				<PaginationDots />
			</Flex>
		</Flex>);
};
MultiSpeakerGridLayout.defaultProps = {
    waitingRoom: false,
    widthOffset: 0,
};
export default MultiSpeakerGridLayout;
//# sourceMappingURL=MultiSpeakerGridLayout.js.map