import React, {
  forwardRef,
  ReactElement,
  useImperativeHandle,
  useState,
} from "react";
import Question, { QuestionChildProps } from "./Question";
import Slider from "../ui/Slider";
import { View } from "react-native";
import Text from "../ui/Text";

const MIN_AGE = 18;
const MAX_AGE = 100;

export interface AgeQuestionRef {
  getAnswer: () => number;
}

export default forwardRef<AgeQuestionRef, QuestionChildProps>(
  (props, ref): ReactElement => {
    const [answer, setAnswer] = useState<number>(MIN_AGE);

    useImperativeHandle(ref, () => ({
      getAnswer: () => answer,
    }));

    return (
      <Question question="Quel âge avez-vous ?" {...ref} {...props}>
        <View className="flex-row justify-center">
          {answer <= MIN_AGE ? (
            <Text className="mt-8" size="4xl">
              - de
            </Text>
          ) : null}
          {answer >= MAX_AGE ? (
            <Text className="mt-8" size="4xl">
              + de
            </Text>
          ) : null}
          <Text size="8xl" weight="bold">
            {answer}
          </Text>
          <Text className="mt-8 ml-1" size="4xl">
            ans
          </Text>
        </View>
        <Slider
          minimumValue={MIN_AGE}
          maximumValue={MAX_AGE}
          onValueChange={setAnswer}
        />
      </Question>
    );
  }
);
