import { FC, useEffect, useState } from 'react';
import { LocalizeText, WiredFurniType } from '../../../../api';
import { Text } from '../../../../common';
import { useWired } from '../../../../hooks';
import { WiredSourcesSelector } from '../WiredSourcesSelector';
import { WiredSelectorBaseView } from './WiredSelectorBaseView';

export const WiredSelectorUsersOnFurniView: FC<{}> = () =>
{
    const { trigger = null, setIntParams = null } = useWired();
    const [ filterExisting, setFilterExisting ] = useState(false);
    const [ invert, setInvert ] = useState(false);
    const [ furniSource, setFurniSource ] = useState<number>(() =>
    {
        if(trigger?.intData?.length > 0) return trigger.intData[0];
        return (trigger?.selectedItems?.length ?? 0) > 0 ? 100 : 0;
    });

    useEffect(() =>
    {
        if(!trigger) return;

        setFurniSource((trigger.intData.length > 0) ? trigger.intData[0] : ((trigger.selectedItems?.length ?? 0) > 0 ? 100 : 0));
        setFilterExisting((trigger.intData.length > 1) ? (trigger.intData[1] === 1) : false);
        setInvert((trigger.intData.length > 2) ? (trigger.intData[2] === 1) : false);
    }, [ trigger ]);

    const save = () => setIntParams([
        furniSource,
        filterExisting ? 1 : 0,
        invert ? 1 : 0
    ]);

    return (
        <WiredSelectorBaseView
            hasSpecialInput={ true }
            requiresFurni={ WiredFurniType.STUFF_SELECTION_OPTION_BY_ID_BY_TYPE_OR_FROM_CONTEXT }
            save={ save }
            hideDelay={ true }
            footer={ <WiredSourcesSelector showFurni={ true } furniSource={ furniSource } onChangeFurni={ setFurniSource } /> }>
            <div className="flex flex-col gap-2">
                <Text bold>{ LocalizeText('wiredfurni.params.selector_options_selector') }</Text>

                <label className="flex items-center gap-2">
                    <input
                        type="checkbox"
                        className="form-check-input"
                        checked={ filterExisting }
                        onChange={ event => setFilterExisting(event.target.checked) } />
                    <Text small>{ LocalizeText('wiredfurni.params.selector_option.0') }</Text>
                </label>

                <label className="flex items-center gap-2">
                    <input
                        type="checkbox"
                        className="form-check-input"
                        checked={ invert }
                        onChange={ event => setInvert(event.target.checked) } />
                    <Text small>{ LocalizeText('wiredfurni.params.selector_option.1') }</Text>
                </label>
            </div>
        </WiredSelectorBaseView>
    );
};
