import React, { useImperativeHandle } from "react";
import Question, { QuestionChildProps } from "./Question";
import Text from "../ui/Text";
import SegmentedControl from "../ui/SegmentedControl";
import { View } from "react-native";

const ANSWERS = ["1", "2", "3", "4", "5"];

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

export interface FrequencyQuestionProps extends QuestionChildProps {
  isMale: boolean;
}

export default React.forwardRef<FrequencyQuestionRef, FrequencyQuestionProps>(
  ({ isMale, ...props }, ref): React.ReactElement => {
    const [answer, setAnswer] = React.useState("1");

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

    return (
      <Question
        question="Nombre de séance(s) par semaine ?"
        {...ref}
        {...props}
      >
        <View className="items-center">
          <Text size="2xl">
            Je suis {isMale ? "prêt" : "prête"} à m'entraîner
          </Text>
          <Text size="9xl" weight="bold">
            {answer}
          </Text>
          <Text size="2xl">fois par semaine !</Text>
        </View>
        <SegmentedControl
          values={ANSWERS}
          selectedIndex={ANSWERS.indexOf(answer)}
          onValueChange={setAnswer}
        />
      </Question>
    );
  }
);
