import {useState} from 'react'
import PropTypes from 'prop-types'
import Image from 'next/image'
import {Box, Button, Container, Grid, Typography, useMediaQuery} from '@material-ui/core'
import {makeStyles} from '@material-ui/core/styles'
import HelpIcon from '@material-ui/icons/Help'
import GroupIcon from '@material-ui/icons/Group'
import MusicNoteIcon from '@material-ui/icons/MusicNote'
import {Pleroma, Peertube, Nextcloud} from '@icons-pack/react-simple-icons'

import KatKayLa from '../components/kat-kay-la'
import HeadLayout from '../components/head-layout'
import Carousel from '../components/carousel'
import Footer from '../components/footer'
import {jwennTeksKantite, jwennAwtisKantite} from '../lib/oki-api'
import RezoMenu from '../components/rezo-menu'
import JwennSesyon from '../components/jwenn-sesyon'
import JwennSignal from '../components/jwenn-signal'

import okiLogo from '../public/logo-192x110.png'

const useStyles = makeStyles(theme => ({
  root: {
    '& > *': {
      margin: theme.spacing(1.2)
    }
  }
}))

const REZO = [
  {
    id: 'mizik',
    tit: 'Mizik',
    icon: <MusicNoteIcon />
  },
  {
    id: 'pale',
    tit: 'Palé',
    icon: <Pleroma />
  },
  {
    id: 'gade',
    tit: 'Gadé',
    icon: <Peertube />
  },
  {
    id: 'mobilize',
    tit: 'Mobilizé',
    icon: <GroupIcon />
  },
  {
    id: 'niyaj',
    tit: 'Niyaj',
    icon: <Nextcloud />
  }
]

export default function Home({kantiteAwtis, kantiteTeks}) {
  const classes = useStyles()
  const [handleOpen, setHandleOpen] = useState(false)
  const handleClick = () => {
    setHandleOpen(true)
  }

  const kantite = [
    {id: 1, tit: 'Tèks', soutit: 'Texte', kantite: kantiteTeks, route: '/teks'},
    {id: 2, tit: 'Awtis', soutit: 'Artiste', kantite: kantiteAwtis, route: '/awtis?paj&paj=1'}
  ]

  const matches = useMediaQuery('(max-width:600px)')

  return (
    <HeadLayout tab={0}>
      <div style={{display: 'flex', flexDirection: 'column', minHeight: '100vh'}}>
        <Container align='center'>
          <Image
            alt='Logo #OKi'
            width={192}
            height={110}
            src={okiLogo}
            placeholder='blur'
          />
          <Typography variant='h6' component='h1'>
            Organisation KA Internationale
          </Typography>
        </Container>
        <Container style={{marginBlock: '1em'}}>
          <Grid container spacing={3}>
            {kantite.map(k => <KatKayLa key={k.id} tit={k.tit} soutit={k.soutit} kantite={k.kantite} route={k.route} />)}
          </Grid>
        </Container>
        <Container className={classes.root} style={{display: 'flex', justifyContent: 'center'}} align='center'>
          <Box style={{display: 'flex', flexDirection: 'column'}}>
            <RezoMenu data={REZO} />
            <JwennSesyon />
          </Box>
          <Box style={{display: 'flex', flexDirection: 'column'}}>
            <Button
              startIcon={<HelpIcon style={{fontSize: 25}} />}
              size='large'
              variant='contained'
              color='primary'
              onClick={handleClick}
            >
              Aide
            </Button>
            <JwennSignal />
          </Box>
        </Container>
        <Carousel
          isMobile={matches}
          handleOpen={handleOpen}
          setHandleOpen={setHandleOpen}
        />
        <Footer />
      </div>
    </HeadLayout>
  )
}

Home.propTypes = {
  kantiteAwtis: PropTypes.number.isRequired,
  kantiteTeks: PropTypes.number.isRequired
}

export async function getServerSideProps() {
  const awtisResponse = await jwennAwtisKantite()
  const teksResponse = await jwennTeksKantite()
  const kantiteAwtis = awtisResponse
  const kantiteTeks = teksResponse

  return {
    props: {
      kantiteAwtis,
      kantiteTeks
    }
  }
}
