import { useTrainingQuery } from '../src/gql'
import { TrainingArchive } from '../src/training'
import { useState, useEffect } from 'react'
import { TTraining } from '../src/training/types'
import TrainingProgram from '../src/training/components/TrainingProgram'
import TrainingMeta from '../src/training/components/TrainingMeta'
import theme from '../src/styles/theme'

const Home = () => {
  const { data, error, loading } = useTrainingQuery()
  const [training, setTraining] = useState<undefined | TTraining>()
  console.log(data)

  useEffect(() => {
    if (data?.training) {
      setTraining(data.training)
    }
  }, [data])

  return (
    <section className='next-training'>
      {training ? (
        <>
          <h1 className='training-title'>{training.title}</h1>
          <div className='training-meta-wrap'>
            <TrainingMeta training={training} />
          </div>
          <TrainingProgram training={training} />
        </>
      ) : null}

      <TrainingArchive />

      <style jsx>
        {`
          .next-training {
            padding: 1.2em 0.3em;
          }
          .training-title {
            margin: 0.3em 0;
            padding: 0;
            font-size: 3.4rem;
            font-weight: 300;
            text-transform: uppercase;
            color: ${theme.colors.presentationBackground};
            margin: 0.3em 0;
            position: relative;
          }
          .training-title::before {
            content: 'your next training';
            text-transform: none;
            font-weight: 300;
            font-size: 1.1rem;
            display: block;
            position: absolute;
            top: -1rem;
            left: 1rem;
            color: ${theme.colors.highlight};
          }
          @media (min-width: ${theme.midsize}) {
            .next-training {
              display: grid;
              grid-template-areas:
                'title    info   '
                'program  program'
                'archive  archive';
              grid-template-columns: 1fr auto;
              column-gap: 1em;
            }
            .training-title {
            grid-area: title;
            }
            :global(.training-meta-wrap) {
              grid-area: info;
            }
            :global(.training-program) {
              grid-area: program;
            }
            :global(.training-archive) {
              grid-area: archive;
            }
          }
          @media (min-width: ${theme.fullsize}) {
            .next-training {
              padding: 3em 3em;
              grid-template-areas:
                'free1  title    program  free2'
                'free1  info     program  free2'
                'free1  archive  archive  free2';
              grid-template-columns: minmax(0, 1fr) minmax(550px, 4fr) minmax(300px, 2fr) minmax(0, 1fr);
              column-gap: 2em;
              background-image: linear-gradient(
                to bottom,
                ${theme.colors.presentationBackground},
                ${theme.colors.presentationBackground} 400px,
                ${theme.colors.black}44 400px,
                ${theme.colors.background} 406px
              );
            }
            .training-title {
              font-size: 5rem;
              margin: 0;
              color: ${theme.colors.presentation}
            }
            .training-title::before {
              font-size: 1.4rem;
              display: block;
              position: absolute;
              top: -0.7rem;
              left: 0.8rem;
              color: ${theme.colors.presentation};
            }
            .training-meta-wrap {
              filter: drop-shadow(0 0 10px rgba(0, 0, 0, 0.5));
            }
            .next-training :global(.training-meta) {
              padding-top: 6em;
              color: ${theme.colors.white};
              height: 300px;
              background-image: url('/media/man_working_out.jpg');
              background-size: auto 100%;
              background-repeat: no-repeat;
              background-position: right center;
              clip-path: polygon(0% 0%, 95% 0%, 100% 50%, 95% 100%, 0% 100%, 0% 0%);
            }
            .next-training :global(.training-program) {
              padding: 2em;
              background-color: #ebebeb;
              /* box-shadow: ${theme.bs}; */
              filter: drop-shadow(0 0 10px rgba(0,0,0,0.18));
            }
          }
        `}
      </style>
    </section>
  )
}

export default Home
