import { FontAwesome5 } from '@expo/vector-icons';
import { useNavigation, useRoute } from '@react-navigation/native';
import React, { useLayoutEffect, useMemo, useState } from 'react';
import { View } from 'react-native';

import IconButton from '../../../../../../../../components/Button/IconButton';
import DetailList from '../../../../../../../../components/List/DetailList';
import Loader from '../../../../../../../../components/Loader/Loader';
import ConfirmationModal from '../../../../../../../../components/Modal/ConfirmationModal';
import InfoModal from '../../../../../../../../components/Modal/InfoModal';
import { NavProps, RouteProps } from '../../../../../../../../components/Routes/Routes';
import { getDateAsString } from '../../../../../../../../helpers/stringHelpers';
import {
  useEventDetail,
  useEventFunctionAllGeneralGet,
  useEventGeneralUpdateStatePost,
} from '../../../../../../../../hooks/useEvents';
import { useUserInfo } from '../../../../../../../../hooks/useStorage';

const EventState = () => {
  const { id, eventName, isRelation } = useRoute<RouteProps<'EventState'>>().params;
  const navigation = useNavigation<NavProps>();
  const [userInfo] = useUserInfo();

  const { detail, isLoading, refetch } = useEventDetail(userInfo.token, id);

  const { mutate: updateEvent } = useEventGeneralUpdateStatePost(userInfo.token, id, refetch);
  const [isDeletedModalVisible, setIsDeletedModalVisible] = useState(false);
  const [isInfoModalVisible, setIsInfoModalVisible] = useState(false);

  const { data: functions, isLoading: isFunctionLoading } = useEventFunctionAllGeneralGet(
    userInfo.token,
    id,
  );
  const canBeUpdated = useMemo(() => {
    return functions?.every(f => f.name.length);
  }, [functions]);

  useLayoutEffect(() => {
    navigation.setOptions({
      title: eventName,
      headerRight: () => {
        if (!isRelation) return null;
        return (
          <View className="flex-row items-center">
            <IconButton
              classNames="mr-2"
              variant="secondary"
              onPress={() =>
                canBeUpdated ? setIsDeletedModalVisible(true) : setIsInfoModalVisible(true)
              }
            >
              <FontAwesome5 name="edit" size={24} />
            </IconButton>
          </View>
        );
      },
    });
  }, [navigation, eventName, canBeUpdated, isRelation]);

  return (
    <View className="mx-4 mb-4 flex-1 justify-start bg-primary-bg">
      {!isLoading && !isFunctionLoading ? (
        <DetailList
          title={{
            text: 'Stav a odevzdání',
          }}
          items={[
            {
              title: 'Stav:',
              value: detail?.state,
            },
            ...(detail?.personClosed
              ? [
                  {
                    title: 'Osoba, která akci uzavřela:',
                    value: detail?.personClosed,
                  },
                ]
              : []),
            ...(detail?.dateClosed
              ? [
                  {
                    title: 'Datum uzavření:',
                    value: getDateAsString(new Date(detail?.dateClosed)),
                  },
                ]
              : []),
          ]}
        />
      ) : (
        <Loader />
      )}
      <ConfirmationModal
        isVisible={isDeletedModalVisible}
        setIsVisible={setIsDeletedModalVisible}
        title={`${detail?.stateId === 'closed' ? 'Uzavřít' : 'Otevřít'} akci`}
        text={`Opravdu chcete akci ${detail?.displayName} ${
          detail?.stateId === 'closed' ? 'uzavřít' : 'otevřít'
        }?`}
        onConfirm={() => updateEvent(detail.isClosed)}
      />
      <InfoModal
        isVisible={isInfoModalVisible}
        setIsVisible={setIsInfoModalVisible}
        title={`${detail?.stateId === 'closed' ? 'Uzavřít' : 'Otevřít'} akci`}
        text="Nejporve musíte zadat všechny členy týmu."
      />
    </View>
  );
};

export default EventState;
