import React, { useState, useEffect } from 'react';
import { Alert, AsyncStorage, Image, ScrollView, Text, TouchableOpacity, View } from 'react-native';
import { MaterialIcons, Entypo } from '@expo/vector-icons';

import { styles } from './styles';
import { DrawerActions, useNavigation } from '@react-navigation/core';
import api from '../../services/api';

const CustomDrawer: React.FC = () => {
    const navigation: any = useNavigation();

    
    const [mostrar_gerentes, setMostrarGerentes] = useState("flex");
    const [mostrar_rh, setMostrarRH] = useState("flex");
    const [mostrar_tesoureiro, setMostrarTesoureiro] = useState("flex");


    async function loadNivel() {
        const nivel:any = await AsyncStorage.getItem('@nivel');
        
        if(nivel.replace(/['"]+/g, '') == 'Tesoureiro'){           
            setMostrarGerentes('none'); 
            setMostrarRH('none'); 
            setMostrarTesoureiro('flex');            
        }else if(nivel.replace(/['"]+/g, '') == 'Secretario'){
            setMostrarGerentes('none'); 
            setMostrarRH('none'); 
            setMostrarTesoureiro('none');
        }else if(nivel.replace(/['"]+/g, '') == 'RH'){
            setMostrarGerentes('none'); 
            setMostrarRH('flex'); 
            setMostrarTesoureiro('none');
        }else if(nivel.replace(/['"]+/g, '') == 'Recepcionista'){
            setMostrarGerentes('none'); 
            setMostrarRH('none'); 
            setMostrarTesoureiro('none');
        }
    }

    async function logout() {
        const user = await AsyncStorage.getItem('@user');  
        Alert.alert('Sair', `Você tem certeza que quer sair?`, [
            {
                text: 'Não',
                style: 'cancel',
            },

            {
                text: 'Sim',
                onPress: async () => {
                    try {
                        await api.get(`api/login/logout.php?user=${user}`);
                        await AsyncStorage.clear();                        
                        navigation.navigate('Login');
                    } catch (error) {
                        Alert.alert('Não foi possivel sair, tente novamente!')
                    }
                }
            }
        ])
    }

    

    useEffect(() => {
        loadNivel();          
            
    }, [])

    return (
        <View style={{ flex: 1, backgroundColor: '#fff' }}>
            <Image style={styles.logo} source={require('../../assets/logo2.png')} />

            <View style={{ width: '90%', backgroundColor: '#c1c1c1', height: 0.5, alignSelf: 'center', marginBottom: 5, marginTop: 13 }}></View>

            <ScrollView
                style={styles.container}
            >

<View style={{display:mostrar_tesoureiro}}>    
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Movimentacoes")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="credit-card" size={30} color="gray" />

                        <Text style={styles.PagesText}>Movimentacoes</Text>
                    </TouchableOpacity> 
                   
                </View>

                <View>
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Pessoas")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="people-alt" size={30} color="gray" />

                        <Text style={styles.PagesText}>Clientes</Text>
                    </TouchableOpacity>

                   
                </View>

                <View>
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Fornecedores")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="person-outline" size={30} color="gray" />

                        <Text style={styles.PagesText}>Fornecedores</Text>
                    </TouchableOpacity>
                    </View>


                    <View style={{display:mostrar_rh}}>    
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Funcionarios")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="people-outline" size={30} color="gray" />

                        <Text style={styles.PagesText}>Funcionários</Text>
                    </TouchableOpacity> 
                   
                </View>


                <View style={{display:mostrar_gerentes}}>    
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Usuarios")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="person" size={30} color="gray" />

                        <Text style={styles.PagesText}>Usuários</Text>
                    </TouchableOpacity> 
                   
                </View>


                <View>
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Tarefas")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="lock-clock" size={30} color="gray" />

                        <Text style={styles.PagesText}>Tarefas</Text>
                    </TouchableOpacity>

                   
                </View>


                <View style={{display:mostrar_tesoureiro}}>    
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Pagar")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="money-off" size={30} color="gray" />

                        <Text style={styles.PagesText}>Contas à Pagar</Text>
                    </TouchableOpacity> 
                   
                </View>


                <View style={{display:mostrar_tesoureiro}}>    
                    <TouchableOpacity
                        style={styles.Pages}
                        onPress={() => {
                            navigation.navigate("Receber")
                            navigation.dispatch(DrawerActions.closeDrawer())
                        }}
                    >
                        <MaterialIcons style={styles.iconRegistered} name="money" size={30} color="gray" />

                        <Text style={styles.PagesText}>Contas à Receber</Text>
                    </TouchableOpacity> 
                   
                </View>

            </ScrollView>

            <View style={styles.footer}>
                <TouchableOpacity
                    onPress={() => logout()}
                    style={styles.Sair}
                >
                    <MaterialIcons name="subdirectory-arrow-left" size={25} color="gray" />
                    <Text style={styles.SairText}>Sair da conta</Text>
                </TouchableOpacity>
            </View>
        </View>
    );
}

export default CustomDrawer;