import React, { useCallback, useState } from 'react';
import { View } from 'react-native';
import type { MockCode } from '../../bluetooth/readMeter';
import type { ReadStrategy } from '../../parsing/ReadStrategy';
import ReadingResult from './components/ReadingResult';
import InteractionWithProbe from './components/InteractionWithProbe';
import styles from './styles';
import NoProbeWrapper from '../NoProbeWrapper';

interface StepsTranslations {
  titleWithDate: string;
  titleWithoutDate: string;
  interactionWithProbeDescription: string;
  interactionWithProbeTextButton: string;
  readingResultTextButton: string;
  titleSuccessWithDate: string;
  titleSuccessWithoutDate: string;
  onErrorWithDate: string;
  onErrorWithoutDate: string;
  meterFamilyTypePrepositionBadge: string;
}

interface Props {
  onClose: Function;
  translations: StepsTranslations;
  onSuccess: Function;
  loadingCurrentDevice: boolean;
  mockCode?: MockCode;
  readStrategy: ReadStrategy;
  reconnectionOrLinkWithProbeTranslations: any;
  goToProbeProbePairing: Function;
  time: String;
  date?: String;
}

const ChangeDatetime = ({
  onClose,
  translations,
  onSuccess,
  loadingCurrentDevice,
  readStrategy,
  mockCode,
  reconnectionOrLinkWithProbeTranslations,
  goToProbeProbePairing,
  time,
  date,
}: Props) => {
  const [readingResult, setReadingResult] = useState();
  const [error, setError] = useState('');

  const meterFamilyType = 'Elster';
  const changeDateAndTime = !!date && !!time;

  const onError = useCallback(() => {
    setError(changeDateAndTime ? translations.onErrorWithDate : translations.onErrorWithoutDate);
  }, [changeDateAndTime, translations.onErrorWithDate, translations.onErrorWithoutDate]);

  if (readingResult === null || error) {
    return (
      <View style={styles.container}>
        <ReadingResult
          translations={translations}
          closeProbeReading={onClose}
          meterFamilyType={meterFamilyType}
          error={error}
          onSuccess={onSuccess}
          readingResult={readingResult}
          changeDateAndTime={changeDateAndTime}
        />
      </View>
    );
  } else {
    return (
      <NoProbeWrapper
        reconnectionOrLinkWithProbeTranslations={reconnectionOrLinkWithProbeTranslations}
        goToProbeProbePairing={goToProbeProbePairing}
      >
        <View style={styles.container}>
          <InteractionWithProbe
            translations={translations}
            closeProbeReading={onClose}
            meterFamilyType={meterFamilyType}
            setReadingResult={setReadingResult}
            onError={onError}
            mockCode={mockCode}
            probeCommand={
              changeDateAndTime
                ? readStrategy.meterFamily.setDateAndTimeCommand?.(readStrategy?.meterPassword, time, date)
                : readStrategy.meterFamily.setTimeProbeCommand?.(readStrategy?.meterPassword, time)
            }
            loadingCurrentDevice={loadingCurrentDevice}
            changeDateAndTime={changeDateAndTime}
          />
        </View>
      </NoProbeWrapper>
    );
  }
};

export default ChangeDatetime;
