import * as React from 'react'
import type { StoryObj, Meta } from '@storybook/react-webpack5'
import { GridEditorInputTime } from '.'
import { Grid } from '../grids'
import { Calendar } from '@planview/pv-icons'
import { theme } from '@planview/pv-utilities'
import { GridCellDefault } from '../components'
import type { GridConfirmPayload } from '../types'
import type { InputType } from '@storybook/csf'

const hideControl: InputType = { control: false }

export default {
    title: 'pv-grid/Components/Editors/GridEditorInputTime',
    component: GridEditorInputTime,
    parameters: {
        controls: {
            exclude: ['label', 'mode', 'rowId', 'columnId'],
        },
    },
    argTypes: {
        columnId: hideControl,
        rowId: hideControl,
        tabIndex: hideControl,
        value: hideControl,
    },
} satisfies Meta<typeof GridEditorInputTime>

type TimeRow = {
    id: string
    input: string
}

export const Default: StoryObj<typeof GridEditorInputTime> = {
    render: function Component(args) {
        const [rows, setRows] = React.useState<TimeRow[]>([
            { id: '1', input: '1:20' },
            { id: '2', input: '1' },
            { id: '3', input: '16:30' },
        ])

        return (
            <Grid
                selectionMode="none"
                columns={[
                    {
                        id: 'input',
                        label: 'Input time Editor',
                        sortable: false,
                        width: 300,
                        cell: {
                            editable: true,
                            Renderer(props) {
                                return (
                                    <GridCellDefault
                                        {...props}
                                        icon={args.icon}
                                    />
                                )
                            },
                            Editor(props) {
                                return (
                                    <GridEditorInputTime {...args} {...props} />
                                )
                            },
                        },
                    },
                ]}
                rows={rows}
                onCellChange={(
                    confirm: GridConfirmPayload<TimeRow, 'input'>
                ) => {
                    setRows((r) =>
                        r.map((row) => {
                            if (row.id === confirm.rowId) {
                                return {
                                    ...row,
                                    input: confirm.nextValue,
                                }
                            }
                            return row
                        })
                    )
                }}
            />
        )
    },
    args: {
        placeholder: 'This is a placeholder',
        icon: 'no icon' as any,
    },
    argTypes: {
        icon: {
            control: { type: 'radio' },
            options: ['no icon', 'with icon', 'with coloured icon'],
            mapping: {
                'no icon': null,
                'with icon': <Calendar color={theme.iconNormal} />,
                'with coloured icon': <Calendar color={theme.iconWarning} />,
            },
        },
    },
    parameters: {
        docs: {
            source: {
                code: `
const [rows, setRows] = React.useState<TimeRow[]>([
    { id: '1', input: '1:20' },
    { id: '2', input: '1' },
    { id: '3', input: '16:30' },
])

return (
    <Grid
        selectionMode="none"
        columns={[
            {
                id: 'input',
                label: 'Input time Editor',
                sortable: false,
                width: 300,
                cell: {
                    editable: true,
                    Renderer(props) {
                        return (
                            <GridCellDefault
                                {...props}
                                icon={args.icon}
                            />
                        )
                    },
                    Editor(props) {
                        return (
                            <GridEditorInputTime {...args} {...props} />
                        )
                    },
                },
            },
        ]}
        rows={rows}
        onCellChange={(
            confirm: GridConfirmPayload<TimeRow, 'input'>
        ) => {
            setRows((r) =>
                r.map((row) => {
                    if (row.id === confirm.rowId) {
                        return {
                            ...row,
                            input: confirm.nextValue,
                        }
                    }
                    return row
                })
            )
        }}
    />
)
                `,
            },
        },
    },
}
