import DesktopView from '@/core/components/views/DesktopView'
import useLogin from '../hooks/useLogin'
import Link from '@/core/components/elements/Link/Link'
import PageContent from '@/lib/content/components/PageContent'
import Alert from '@/core/components/elements/Alert/Alert'
import { useSession, signIn, SignOut } from 'next-auth/react'
import Image from 'next/image'
import { useRouter } from 'next/router'
import { useContext, useEffect, useState } from 'react'
import { getAuth, setAuth } from '@/core/utils/auth'
import { setCookie } from 'cookies-next'

const LoginDesktop = () => {
  const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } =
    useLogin()

  const router = useRouter()
  const [query, setQuery] = useState(router?.query?.next || '/')
  const { data: session } = useSession()
  const auth = getAuth()

  const handleGoogle = async () => {
    const url = query != '/' ? '/login?next=' + query : '/login'
    await signIn('google', { callbackUrl: url })
  }

  useEffect(() => {
    if(session){
      setCookie('auth', JSON.stringify(session?.odooUser))
      router.push(query)
    }
  },[session])

  return (
    <DesktopView>
      <div className='container mx-auto'>
        <div className='grid grid-cols-2 gap-x-10 pt-16'>
          <div>
            <h1 className='text-2xl font-semibold'>Selamat Datang di Indoteknik</h1>
            <h2 className='text-gray_r-11 font-normal mt-1 mb-4'>
              Masuk ke akun kamu untuk mulai transaksi!
            </h2>

            {alert && (
              <Alert className='text-center' type={alert.type}>
                {alert.children}
              </Alert>
            )}

            <form className='w-full mt-6 flex flex-col gap-y-4' onSubmit={handleSubmit}>
              <div>
                <label htmlFor='email'>Alamat Email</label>
                <input
                  type='email'
                  name='email'
                  id='email'
                  className='form-input w-full mt-3'
                  ref={emailRef}
                  onChange={handleChangeInput}
                  placeholder='contoh@email.com'
                />
              </div>
              <div>
                <div className='flex justify-between'>
                  <label htmlFor='password'>Kata Sandi</label>
                  <Link href='/forgot-password'>Lupa Kata Sandi</Link>
                </div>
                <input
                  type='password'
                  name='password'
                  id='password'
                  className='form-input w-full mt-3'
                  ref={passwordRef}
                  onChange={handleChangeInput}
                  placeholder='••••••••••••'
                />
              </div>
              <button
                type='submit'
                className='btn-yellow w-full mt-2'
                disabled={!isValid || isLoading}
              >
                {!isLoading ? 'Masuk' : 'Loading...'}
              </button>
            </form>
            <div className='flex  items-center mt-3 mb-3'>
              <hr className='flex-1' />
              <p className='text-gray-400'>ATAU</p>
              <hr className='flex-1' />
            </div>

            <button
              type='submit'
              className='border border-gray-500 p-2 rounded-md hover:bg-gray-100  w-full mt-2 flex items-center justify-center gap-x-2'
              onClick={() => handleGoogle()}
            >
              <Image
                src='/images/icons8-google.svg'
                alt='google image'
                className='h-7 w-7'
                width={10}
                height={10}
              />
              <p>Masuk dengan Google</p>
            </button>

            <div className='text-gray_r-11 mt-10'>
              Belum punya akun Indoteknik?{' '}
              <Link href='/register' className='inline'>
                Daftar akun baru
              </Link>
            </div>
          </div>
          <div>
            <PageContent path='/login' />
          </div>
        </div>
      </div>
    </DesktopView>
  )
}

export default LoginDesktop
