/**
 * Small presentational pieces shared by the Βεβαίωση sections, built on the shared form components.
 * Kept at module level on purpose: defining them inside a component would remount
 * the inputs on every render and they would lose focus on each keystroke.
 */
import React, {ReactNode} from 'react';
import CustomLabel from "../../../../components/UI/Forms/CustomLabel/CustomLabel";
import CustomTextField from "../../../../components/UI/Forms/CustomTextField/CustomTextField";
// GUESS: folder name; CustomTextArea imports '../../FormError/FormError', so it sits next to CustomTextField
import CustomTextArea from "../../../../components/UI/Forms/CustomTextArea/CustomTextArea";
import MakeCustomRadioSelect from "../../../../components/UI/Forms/CustomSelect/MakeCustomRadioSelect";
import {LookupOption} from "../../../../models/Ked/Vevaiosi";

/* ------------------------------ TextField ------------------------------ */

type TextFieldProps = {
    label: string; value: string; onChange: (v: string) => void;
    required?: boolean; placeholder?: string; error?: string; disabled?: boolean;
    inputMode?: 'text' | 'numeric' | 'decimal'; maxLength?: number; hint?: ReactNode; multiline?: boolean;
    // fires when focus leaves the input (focus out)
    onBlur?: () => void;
};

// The error is NOT passed as hasError: CustomTextField / CustomTextArea would render it through FormError,
// and View scrolls to the first `.error-message`. Rendering it here keeps that scroll working
// and avoids showing the message twice.
// onBlur sits on the wrapper: React's onBlur bubbles (focusout), so it catches the input's blur
// without CustomTextField / CustomTextArea having to support the prop.
export const TextField = ({
                              label, value, onChange, required, placeholder = 'Παρακαλώ συμπληρώστε',
                              error, disabled, inputMode, maxLength, hint, multiline, onBlur
                          }: TextFieldProps) => (
    <div className="vf-field" onBlur={onBlur}>
        <CustomLabel>
            {label}{required ? ' *' : ''}
            {hint && <span className="vf-hint"> {hint}</span>}
        </CustomLabel>
        {multiline
            ? <CustomTextArea value={value} onChange={onChange} readOnly={disabled}
                              placeholder={disabled ? '' : placeholder} maxLength={maxLength}/>
            : <CustomTextField value={value} onChange={onChange} readOnly={disabled}
                               placeholder={placeholder} maxLength={maxLength}
                // CustomTextField only accepts 'text' | 'numeric'; 'numeric' has no decimal key
                // on iOS, so amounts fall back to the full keyboard
                               inputMode={inputMode === 'decimal' ? 'text' : inputMode}/>}
        {error && <p className="error-message">{error}</p>}
    </div>
);

/* ----------------------------- SelectField ----------------------------- */

type SelectFieldCommon = {
    label: string; options: LookupOption[];
    required?: boolean; error?: string; disabled?: boolean;
};
// numeric lookups (KAE, relations): the selected value is converted to a number
type NumericSelectProps = SelectFieldCommon & {
    asString?: false; value: number | ''; onChange: (v: number) => void;
};
// string lookups (signer AFM): the value is kept as a string, so leading zeros survive
type StringSelectProps = SelectFieldCommon & {
    asString: true; value: number | string; onChange: (v: string) => void;
};

export const SelectField = (props: NumericSelectProps | StringSelectProps) => {
    const {label, value, options, required, error, disabled} = props;
    const handleChange = (v: any) =>
        props.asString ? props.onChange(String(v)) : props.onChange(Number(v));

    return (
        <div className="vf-field">
            <CustomLabel>{label}{required ? ' *' : ''}</CustomLabel>
            {/* CustomRadioSelect renders hasError itself as <p class="error-message">, so no second copy here */}
            <MakeCustomRadioSelect
                theme="form-options"
                placeholder="Παρακαλώ επιλέξτε"
                value={value}
                disabled={disabled}
                hasError={error}
                handleChange={handleChange}
                options={options as any}
            />
        </div>
    );
};

/* --------------------------- row action button -------------------------- */

// "ΔΙΑΓΡΑΦΗ" button aligned with the inputs of a repeatable row (codebtors, KAE).
// The empty label takes the same space as the field labels, so the button lines up with the inputs
// whatever the label height is, and an error under a field doesn't move it.
export const DeleteRowButton = ({onClick}: { onClick: () => void }) => (
    <div className="col-12 col-md-2">
        <div className="vf-field">
            <CustomLabel>&nbsp;</CustomLabel>
            <button type="button" className="vf-btn vf-btn--ghost vf-del-btn" onClick={onClick}>ΔΙΑΓΡΑΦΗ</button>
        </div>
    </div>
);

/* -------------------------------- icons -------------------------------- */

export const DownloadIcon = () => (
    <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor"
         strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
        <path d="M12 3v12m0 0l-5-5m5 5l5-5M4 21h16"/>
    </svg>
);

export const TrashIcon = () => (
    <svg width="17" height="17" viewBox="0 0 24 24" fill="currentColor">
        <path d="M9 3h6l1 2h4v2H4V5h4l1-2zm-3 6h12l-1 12H7L6 9z"/>
    </svg>
);
