import useSWR from 'swr'
import { fetcher } from '../../http/axiosInstance'
import { BASE_URL } from '../../constants'
import { IObjectParam, IParam } from '../../interfaces/objects'
import TCBParameter from './TCBParameter'
import TableValue from './TableValue'
import { TableCell, TableCellLayout, TableRow } from '@fluentui/react-components'

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

const ObjectParameter = ({
    param,
    map_id
}: ObjectParameterProps) => {
    const { data: paramData } = useSWR(
        `/general/params/?param_id=${param.id_param}`,
        (url) => fetcher(url, BASE_URL.ems).then(res => res[0] as IParam),
        {
            revalidateOnFocus: false,
            revalidateIfStale: false
        }
    )

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

                        <TableCell>
                            <TableCellLayout>
                                {value as string}
                            </TableCellLayout>
                        </TableCell>
                    </TableRow>
                )
        }
    }

    return (
        <>
            {paramData &&
                Parameter(paramData.format, paramData.name, param.value, paramData.vtable, paramData.unit)
            }
        </>
    )
}

export default ObjectParameter