/*
 * Copyright (C) Contributors to the Suwayomi project
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import { useLingui } from '@lingui/react/macro';
import { msg } from '@lingui/core/macro';
import type { ValueToDisplayData } from '@/base/Base.types.ts';
import type {
    IReaderSettings,
    IReaderSettingsWithDefaultFlag,
    ReadingDirection,
} from '@/features/reader/Reader.types.ts';
import type { TapZoneInvertMode } from '@/features/reader/tap-zones/TapZoneLayout.types.ts';
import { ButtonSelectInput } from '@/base/components/inputs/ButtonSelectInput.tsx';
import type { SelectButtonDefaultableProps } from '@/base/components/buttons/SelectButton.tsx';

enum TapZonesInvertOption {
    NONE,
    HORIZONTAL,
    VERTICAL,
    BOTH,
}

const VALUE_TO_DISPLAY_DATA: ValueToDisplayData<TapZonesInvertOption> = {
    [TapZonesInvertOption.NONE]: {
        title: msg`None`,
        icon: null,
    },
    [TapZonesInvertOption.HORIZONTAL]: {
        title: msg`Horizontal`,
        icon: null,
    },
    [TapZonesInvertOption.VERTICAL]: {
        title: msg`Vertical`,
        icon: null,
    },
    [TapZonesInvertOption.BOTH]: {
        title: msg`Both`,
        icon: null,
    },
};

const TAP_ZONES_INVERT_OPTION_VALUES = Object.values(TapZonesInvertOption).filter((value) => typeof value === 'number');

const TAP_ZONES_INVERT_OPTION_TO_SETTING: Record<TapZonesInvertOption, TapZoneInvertMode> = {
    [TapZonesInvertOption.NONE]: {
        vertical: false,
        horizontal: false,
    },
    [TapZonesInvertOption.HORIZONTAL]: {
        vertical: false,
        horizontal: true,
    },
    [TapZonesInvertOption.VERTICAL]: {
        vertical: true,
        horizontal: false,
    },
    [TapZonesInvertOption.BOTH]: {
        vertical: true,
        horizontal: true,
    },
};

const convertTapZoneInvertModeToOption = ({ vertical, horizontal }: TapZoneInvertMode): TapZonesInvertOption => {
    if (vertical && horizontal) {
        return TapZonesInvertOption.BOTH;
    }

    if (vertical) {
        return TapZonesInvertOption.VERTICAL;
    }

    if (horizontal) {
        return TapZonesInvertOption.HORIZONTAL;
    }

    return TapZonesInvertOption.NONE;
};

export const ReaderSettingTapZoneInvertMode = ({
    tapZoneInvertMode,
    setTapZoneInvertMode,
    ...buttonSelectInputProps
}: Pick<IReaderSettingsWithDefaultFlag, 'tapZoneInvertMode'> &
    Pick<SelectButtonDefaultableProps<ReadingDirection>, 'isDefaultable' | 'onDefault'> & {
        setTapZoneInvertMode: (invert: IReaderSettings['tapZoneInvertMode']) => void;
    }) => {
    const { t } = useLingui();

    const tapZonesInvertOption = convertTapZoneInvertModeToOption(tapZoneInvertMode.value);

    return (
        <ButtonSelectInput<TapZonesInvertOption>
            {...buttonSelectInputProps}
            label={t`Invert tap zones`}
            value={tapZoneInvertMode.isDefault ? undefined : tapZonesInvertOption}
            defaultValue={tapZoneInvertMode.isDefault ? tapZonesInvertOption : undefined}
            values={TAP_ZONES_INVERT_OPTION_VALUES}
            setValue={(value) => setTapZoneInvertMode(TAP_ZONES_INVERT_OPTION_TO_SETTING[value])}
            valueToDisplayData={VALUE_TO_DISPLAY_DATA}
        />
    );
};
