import { useNavigation, useRoute } from '@react-navigation/native';
import React, { useState } from 'react';
import { View, ScrollView, Text } from 'react-native';

import FormButtons from '../../../../../../../../../../components/Button/FormButtons';
import { NavProps, RouteProps } from '../../../../../../../../../../components/Routes/Routes';
import Switch from '../../../../../../../../../../components/Switch/Switch';
import TextField from '../../../../../../../../../../components/TextField/TextField';
import {
  useParticipantCampInsertNewPost,
  useParticipants,
} from '../../../../../../../../../../hooks/useCamps';
import { useUserInfo } from '../../../../../../../../../../hooks/useStorage';
import { showFeedback } from '../../../../../../../../../../services/feedback/showFeedback';

const CampParticipantAddNew = () => {
  const { campId } = useRoute<RouteProps<'CampParticipantAddNew'>>().params;
  const navigation = useNavigation<NavProps>();
  const [userInfo] = useUserInfo();

  const [firstName, setFirstName] = useState('');
  const [lastName, setLastName] = useState('');
  const [nickName, setNickName] = useState('');
  const [regNumber, setRegNumber] = useState('');
  const [note, setNote] = useState('');
  const [isForeign, setIsForeign] = useState(false);

  const { refetchList } = useParticipants(userInfo.token, campId);
  const { mutate: addParticipant } = useParticipantCampInsertNewPost(
    userInfo?.token,
    campId,
    refetchList,
    navigation.goBack,
  );

  return (
    <View className="m-4 flex-1 justify-start bg-primary-bg">
      <ScrollView>
        <View className="mb-4">
          <TextField title="Jméno" value={firstName} setValue={setFirstName} />
        </View>
        <View className="mb-4">
          <TextField title="Přímení" value={lastName} setValue={setLastName} />
        </View>
        <View className="mb-4">
          <TextField title="Přezdívka" value={nickName} setValue={setNickName} />
        </View>
        <View className="mb-4">
          <TextField title="Identifikační číslo" value={regNumber} setValue={setRegNumber} number />
        </View>
        <View className="mb-4">
          <TextField title="Poznámka" value={note} setValue={setNote} multiline />
        </View>
        <View className="flex-row items-center justify-between">
          <Text className="font-bold">Je cizinec</Text>
          <Switch setIsEnabled={setIsForeign} isEnabled={isForeign} />
        </View>
        <View>
          <FormButtons
            onSubmit={() => {
              let error;
              if (!lastName) error = 'Zadejte přímení.';
              if (!firstName) error = 'Zadejte jméno.';
              if (error) {
                showFeedback({
                  title: error,
                  type: 'danger',
                });
                return;
              }
              const payload = {
                firstName,
                lastName,
                nickName,
                note,
                regNumber,
                isForeign,
              };
              addParticipant(payload);
            }}
          />
        </View>
      </ScrollView>
    </View>
  );
};

export default CampParticipantAddNew;
