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

const MIN_HEIGHT = 120;
const MAX_HEIGHT = 250;

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

export default forwardRef<HeightQuestionRef, QuestionChildProps>(
  ({ ...props }, ref): ReactElement => {
    const [answer, setAnswer] = useState<number>(MIN_HEIGHT);
    const UNITS = ["cm", "inch"];
    const [unit, setUnit] = useState<string>("cm");

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

    return (
      <Question question="Quel est votre taille ?" {...ref} {...props}>
        <SegmentedControl
          values={UNITS}
          selectedIndex={UNITS.indexOf(unit)}
          onValueChange={setUnit}
        />
        <View className="flex-row justify-center">
          {answer <= MIN_HEIGHT ? (
            <Text className="mt-8" size="4xl">
              - de
            </Text>
          ) : null}
          {answer >= MAX_HEIGHT ? (
            <Text className="mt-8" size="4xl">
              + de
            </Text>
          ) : null}
          <Text size="8xl" weight="bold">
            {answer}
          </Text>
          <Text className="mt-8" size="4xl">
            {unit}
          </Text>
        </View>
        <Slider
          minimumValue={MIN_HEIGHT}
          maximumValue={MAX_HEIGHT}
          onValueChange={setAnswer}
        />
      </Question>
    );
  }
);
