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

import _auth from '@netuno/auth-client';
import _service from '@netuno/service-client';

import { Layout, Menu } from 'antd';

import {
  BrowserRouter as Router,
  Switch, Route, Link
} from "react-router-dom";

import Home from './pages/Home';
import Login from './pages/Login';
import Register from './pages/Register';
import Main from './pages/Main';

import Core from './Core';

import logo from './logo.svg';
import './App.less';

const { Header, Footer, Content } = Layout;

const { SubMenu } = Menu;

function App() {
  const [ logged, setLogged ] = useState(false);
  const [ data, setData ] = useState([]);
  useEffect(() => {
    _service({
        url: "pessoas",
        success: (response) => {
            if (response.json) {
              setData(response.json);
            }
        },
        fail: (e) => {
            console.log("Service Error", e);
        }
    });
  }, []);

  Core.events.login = () => {
    setLogged(true);
  }
  Core.events.logout = () => {
    setLogged(false);
  }

  let menu = null;
  if (_auth.isLogged()) {
    menu = <>
      <Menu.Item>
        <Link to="/main">Principal</Link>
      </Menu.Item>
    </>;
  } else {
    menu = <>
      <Menu.Item>
        <Link to="/">Home</Link>
      </Menu.Item>
      <Menu.Item>
        <Link to="/login">Login</Link>
      </Menu.Item>
      <Menu.Item>
        <Link to="/register">Registrar</Link>
      </Menu.Item>
    </>;
  }

  return (
    <Router>
      <Layout>
        <Header>
          <Menu mode="horizontal">
            {menu}
          </Menu>
        </Header>
        <Content>
          <Switch>
            <Route path="/login">
              <Login />
            </Route>
            <Route path="/register">
              <Register />
            </Route>
            <Route path="/main">
              <Main />
            </Route>
            <Route path="/">
              <Home />
            </Route>
          </Switch>
        </Content>
        <Footer>Footer</Footer>
      </Layout>
    </Router>
  );
}

export default App;
