import NavbarBack from 'components/NavbarBack';
import Navigation from 'components/Navigation';
import NoResult from 'components/NoResult';
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import {
  RefreshControl,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import Clock from 'svg/Clock';
import { fakeNotifications, NotificationsData } from '../lib/data';
import NotificationsModal from './NotificationsModal';

interface NotificationsProps {}

const Notifications = (props: NotificationsProps) => {
  const [refreshing, setRefreshing] = React.useState(false);
  const [modalVisible, setModalVisible] = React.useState(false);
  const { t } = useTranslation();
  const [selectedOrder, setSelectedOrder] =
    React.useState<NotificationsData | null>(null);

  const onRefresh = React.useCallback(() => {
    setRefreshing(true);
    setTimeout(() => {
      setRefreshing(false);
    }, 1500);
  }, []);

  const openModal = React.useCallback((item: NotificationsData) => {
    setSelectedOrder(item);
    setModalVisible(true);
  }, []);

  const refreshControl = React.useMemo(
    () => <RefreshControl refreshing={refreshing} onRefresh={onRefresh} />,
    [refreshing, onRefresh],
  );

  if (!(fakeNotifications.length > 0)) {
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <NavbarBack title={t('Bildirishnomalar')} />
        <View style={styles.container}>
          <NoResult />
        </View>
        <Navigation />
      </SafeAreaView>
    );
  }

  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={styles.container}>
        <NavbarBack title={t('Bildirishnomalar')} />
        <ScrollView
          keyboardShouldPersistTaps="handled"
          refreshControl={refreshControl}
        >
          {fakeNotifications.map(item => (
            <TouchableOpacity
              onPress={() => openModal(item)}
              style={styles.card}
              key={item.id}
            >
              <Text style={styles.text}>{item.message}</Text>
              <Clock color="#000000" />
            </TouchableOpacity>
          ))}
        </ScrollView>
        <NotificationsModal
          visible={modalVisible}
          setVisible={setModalVisible}
          selectedOrder={selectedOrder}
        />
        <Navigation />
      </View>
    </SafeAreaView>
  );
};

export default Notifications;

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  header: {
    paddingHorizontal: 20,
    paddingTop: 5,
  },
  title: {
    fontSize: 24,
    fontWeight: 'bold',
    color: '#333',
  },
  card: {
    borderBottomWidth: 1,
    borderColor: '#D8DADC',
    width: '95%',
    marginLeft: 'auto',
    marginRight: 'auto',
    marginTop: 20,
    paddingBottom: 18,
    paddingLeft: 12,
    paddingRight: 12,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
  },
  text: {
    fontSize: 18,
    fontWeight: '400',
    width: '80%',
  },
});
