import React, { useEffect, useState } from 'react';

import {
    SafeAreaView,
    Text,
    View,
    ScrollView,
    TouchableOpacity,
    Image,
    ActivityIndicator,
    RefreshControl,
    StatusBar,
    AsyncStorage,
    Alert,
    
} from 'react-native';

import { MaterialIcons } from '@expo/vector-icons';
import { styles } from './style';
import { DrawerActions, useNavigation } from '@react-navigation/core';
import { AnimatedCircularProgress } from 'react-native-circular-progress';

import api from '../../services/api';
import Load from '../../components/Load';
import { useIsFocused } from '@react-navigation/native';

export default function Home() {
    const navigation: any = useNavigation();
    const isFocused = useIsFocused();

    const [dados, setDados] = useState<any>([]);
    const [isLoading, setIsLoading] = useState(true);
    const [refreshing, setRefreshing] = React.useState(false);
    const [usu, setUsu] = React.useState('');

    async function listarDados() {

        try {
            const user = await AsyncStorage.getItem('@user');    
            const response = await api.get(`api/dashboard/ListAllCards.php?user=${user}`);
            setDados(response.data);
            
        } catch (error) {
            console.log("Error");
        } finally {
            setIsLoading(false);
            setRefreshing(false);
            
        }
    }

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

    useEffect(() => {
        listarDados();
    }, [isFocused]);

    const onRefresh = () => {
        setRefreshing(true);
        listarDados();
       
    };

    const preffi = 25;


    return (
        <View style={{ flex: 1 }}>
            <StatusBar barStyle="light-content" />
            <View style={{ flex: 1 }}>
                <View style={styles.header}>
                    <View style={styles.containerHeader}>

                        <TouchableOpacity
                            style={styles.menu}
                            onPress={() => navigation.dispatch(DrawerActions.openDrawer())}
                        >
                            <MaterialIcons name="menu" size={35} color="black" />
                        </TouchableOpacity>

                        <Image style={styles.logo} source={require('../../assets/logo2.png')} />

                    </View>
                </View>

                {isLoading ?
                    <Load /> :

                    <ScrollView
                        style={{ flex: 1 }}
                        showsVerticalScrollIndicator={false}
                        nestedScrollEnabled={true}
                        refreshControl={
                            <RefreshControl
                                refreshing={refreshing}
                                onRefresh={onRefresh}
                            />
                        }
                    >

                        <View style={styles.circleProgressView}>
                            <View style={styles.textProgressContainer}>
                                <Text style={styles.textProgressTitle}>Tarefas de Hoje</Text>
                                <Text style={styles.textProgress}>{dados.totalTarefasConcluidasHoje} de {dados.totalTarefasHoje} concluídas</Text>
                            </View>

                            <AnimatedCircularProgress
                                size={80}
                                width={8}
                                fill={dados.porcentagemTarefas}
                                tintColor="#00e0ff"
                                backgroundColor="#e0e0e0"
                                lineCap={"round"}
                            >
                                {
                                    (fill) => (
                                        <Text style={styles.numberInside}>{dados.porcentagemTarefas}%</Text>
                                    )
                                }
                            </AnimatedCircularProgress>
                        </View>


                        <View style={styles.containerBox}>

                        <TouchableOpacity onPress={() => navigation.navigate("Tarefas")}>
                                <View>
                                    <View style={styles.box}>
                                        <MaterialIcons style={styles.iconRegistered} name="lock-clock" size={70} color="#b8240d" />
                                        <View style={styles.textos}>
                                            <Text style={styles.rText}>Tarefas Pendentes</Text>
                                            <Text style={styles.lenghtText}>{dados.tarefasPendentes}</Text>
                                        </View>
                                    </View>
                                    <Text style={styles.textFooter}>Minhas Tarefas Pendentes</Text>
                                </View>
                            </TouchableOpacity>


                            <TouchableOpacity onPress={() => navigation.navigate("Tarefas")}>
                                <View>
                                    <View style={styles.box}>
                                        <MaterialIcons style={styles.iconRegistered} name="date-range" size={70} color="#e0841b" />
                                        <View style={styles.textos}>
                                            <Text style={styles.rText}>Tarefas Escritório</Text>
                                            <Text style={styles.lenghtText}>{dados.tarefasPendentesEscritorio}</Text>
                                        </View>
                                    </View>
                                    <Text style={styles.textFooter}>Tarefas Pendentes Escritório</Text>
                                </View>
                            </TouchableOpacity>

                            <TouchableOpacity onPress={() => navigation.navigate("Pessoas")}>
                                <View>
                                    <View style={styles.box}>
                                        <MaterialIcons style={styles.iconRegistered} name="people-alt" size={70} color="#17a2b8" />
                                        <View style={styles.textos}>
                                            <Text style={styles.rText}>Clientes</Text>
                                            <Text style={styles.lenghtText}>{dados.quantidade_clientes}</Text>
                                        </View>
                                    </View>
                                    <Text style={styles.textFooter}>Clientes cadastrados</Text>
                                </View>
                            </TouchableOpacity>


                            
                            <TouchableOpacity onPress={() => navigation.navigate("Funcionarios")}>
                                <View>
                                    <View style={styles.box}>
                                        <MaterialIcons style={styles.iconRegistered} name="people-alt" size={70} color="#087799" />
                                        <View style={styles.textos}>
                                            <Text style={styles.rText}>Funcionários</Text>
                                            <Text style={styles.lenghtText}>{dados.total_func}</Text>
                                        </View>
                                    </View>
                                    <Text style={styles.textFooter}>Funcionários cadastrados</Text>
                                </View>
                            </TouchableOpacity>

                            <TouchableOpacity onPress={() => navigation.navigate("Fornecedores")}>
                                <View>
                                    <View style={styles.box}>
                                        <MaterialIcons style={styles.iconRegistered} name="people-alt" size={70} color="#123f87" />
                                        <View style={styles.textos}>
                                            <Text style={styles.rText}>Fornecedores</Text>
                                            <Text style={styles.lenghtText}>{dados.quantidade_forn}</Text>
                                        </View>
                                    </View>
                                    <Text style={styles.textFooter}>Fornecedores cadastrados</Text>
                                </View>
                            </TouchableOpacity>
                            
                        </View>


                    </ScrollView>
                }
            </View>
        </View>


    )
}