import { StyleSheet, Text, View, Alert } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { confirmLogout } from '../helpers'

const Menu = ({ session, setSession, setEdit, showError, saveNote }) => {
  const logout = async () => {
    try {
      await AsyncStorage.clear();
      setSession(null)
    } catch(e) {
      showError('Error while logging out')
    }
  }

  return (
    <View style={styles.menu}>
      {
        saveNote
          ? (
            <>
              <Text
                style={styles.menuText}
                onPress={() => setEdit(null)}
              >
                Back
              </Text>
              <Text
                style={styles.menuText}
                onPress={saveNote}
              >
                Save
              </Text>
            </>
          ) : (
            <>
              <Text
                style={styles.menuText}
                onPress={() => setEdit({})}
              >
                New Note
              </Text>
              <Text
                style={styles.menuText}
              >
                Sort
              </Text>
              <Text
                style={styles.menuText}
                onPress={() => confirmLogout({ logout, email: session.email })}
              >
                {session.email}
              </Text>
            </>
          )
      }
    </View>
  )
}

const styles = StyleSheet.create({
  menu: {
    backgroundColor: '#333',
    height: 60,
    width: '100%',
    flexDirection: 'row',
    alignItems: 'flex-end',
    justifyContent: 'space-between',
    padding: 10,
  },
  menuText: {
    fontWeight: 'bold',
    paddingHorizontal: 15,
    color: 'white',
  },
});

export default Menu
