import { IObjectParam, IObjectValue } from '../../interfaces/objects'
import TCBParameter from './TCBParameter'
import TableValue from './TableValue'
import { Accordion, AccordionHeader, AccordionItem, AccordionPanel, Table, TableBody, TableCell, TableCellLayout, TableRow, Text } from '@fluentui/react-components'

interface ObjectParameterProps {
    showLabel?: boolean;
    param: IObjectParam;
    map_id: string;
}

const ObjectParameter = ({
    param,
    map_id
}: ObjectParameterProps) => {

    const Parameter = (type: string, name: string, values: IObjectValue[], vtable: string, unit: string | null) => {
        switch (type) {
            case 'bit':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='boolean' />
                )
            case 'int':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='number' />
                )
            case 'smallint':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='number' />
                )
            case 'bigint':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='number' />
                )
            case 'tinyint':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='number' />
                )
            case 'smalldatetime':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='number' />
                )
            // TODO: Calculate from calc procedures
            case 'calculate':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='value' />
                )
            case 'GTCB':
                return (
                    <TCBParameter map_id={map_id} values={values} vtable={vtable} name={name} />
                )
            case 'TCB':
                return (
                    <TCBParameter map_id={map_id} values={values} vtable={vtable} name={name} />
                )
            case type.match(/varchar\((\d+)\)/)?.input:
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='string' />
                )
            case type.match(/numeric\((\d+),(\d+)\)/)?.input:
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='number' unit={unit} />
                )
            case 'year':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='number' />
                )
            case 'uniqueidentifier':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='value' />
                )
            case 'group':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='value' />
                )
            case 'groupcalculate':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='value' />
                )
            case 'array':
                return (
                    <TableValue map_id={map_id} values={values} name={name} type='value' />
                )
            default:
                return (
                    <TableRow>
                        <TableCell>
                            <TableCellLayout>
                                {name}
                            </TableCellLayout>
                        </TableCell>

                        <TableCell>
                            <TableCellLayout>
                                {values}
                            </TableCellLayout>
                        </TableCell>
                    </TableRow>
                )
        }
    }

    return (
        <>
            {param && param.parameters && Array.isArray(param.parameters) && param.parameters.length > 0 ?
                <TableRow>
                    <TableCell colSpan={2} style={{ padding: 0 }}>
                        <TableCellLayout>
                            <Accordion collapsible defaultOpenItems={param.id_param} style={{ width: '100%' }}>
                                <AccordionItem style={{ width: '100%' }} value={param.id_param}>
                                    <AccordionHeader size='small'>
                                        <Text weight='bold' size={200} style={{ textWrap: 'wrap' }}>{param.name}</Text>
                                    </AccordionHeader>

                                    <AccordionPanel style={{ width: '100%', margin: 0 }}>
                                        <Table size='extra-small'>
                                            <TableBody>
                                                {param.parameters.length > 0 &&
                                                    param.parameters.map((p, index) => (
                                                        <ObjectParameter key={`child-param-${param.id_param}-${index}`} param={p} map_id={map_id} />
                                                    ))
                                                }
                                            </TableBody>
                                        </Table>
                                    </AccordionPanel>
                                </AccordionItem>
                            </Accordion>
                        </TableCellLayout>
                    </TableCell>
                </TableRow>

                :
                param && Parameter(param.format, param.name, param.values, param.vtable, param.unit)
            }

        </>
    )
}

export default ObjectParameter