import { View, Text, FlatList } from "react-native";
import React, { useState } from "react";
import CreateLyricsHeader from "./components/CreateLyricsHeader";
import { Palette } from "../../styles";
import { FONT_FAMILY } from "../../styles/Fonts";
import { EMOTION_CONVEY } from "../../data/data";
import ItemContainer from "../../components/ItemContainer/ItemContainer";

const EmotionConvey = ({
  selected: selectedProp,
  setSelected: setSelectedProp,
}) => {
  const [containerLayout, setContainerLayout] = useState(null);
  const [internalSelected, setInternalSelected] = useState(null);
  const selected = selectedProp ?? internalSelected;
  const setSelected = setSelectedProp ?? setInternalSelected;

  const onPressSelect = (item) => {
    // item is full object { title, description, color }
    if (selected?.title === item.title) {
      setSelected(null);
    } else {
      setSelected({ title: item.title, description: item.description });
    }
  };

  return (
    <View style={{ flex: 1, gap: 10, marginTop: 16 }}>
      <CreateLyricsHeader
        title={`Quelle émotion veux-tu\ntransmettre ?`}
        subTitle="Sélectionne une seule intention émotionnelle."
      />

      <View
        style={{ flex: 1 }}
        onLayout={(event) => setContainerLayout(event.nativeEvent.layout)}
      >
        <ItemContainer height={containerLayout?.height}>
          <FlatList
            data={EMOTION_CONVEY}
            contentContainerStyle={{
              gap: 16,
              flexGrow: 1,
              zIndex: 2,
              paddingTop: 5,
            }}
            renderItem={({ item, index }) => {
              const selectedItem = selected?.title === item.title;

              return (
                <View style={{ paddingHorizontal: 5 }}>
                  <CreateLyricsHeader
                    colors={item.color}
                    tint={selectedItem ? "default" : "dark"}
                    onPress={() => onPressSelect(item)}
                  >
                    <Text
                      style={{
                        fontSize: 16,
                        color: Palette.white,
                        fontFamily: FONT_FAMILY.InterRegular,
                      }}
                    >
                      <Text style={{ fontFamily: FONT_FAMILY.InterBold }}>
                        {item.title}
                      </Text>{" "}
                      : {item.description}
                    </Text>
                  </CreateLyricsHeader>
                </View>
              );
            }}
          />
        </ItemContainer>
      </View>
    </View>
  );
};

export default EmotionConvey;
