import { RefreshIcon } from '@heroicons/react/outline'
import { PrivateUser, User } from 'common/user'
import { cleanDisplayName, cleanUsername } from 'common/util/clean-username'
import { formatMoney } from 'common/util/format'
import Link from 'next/link'
import React, { useState } from 'react'
import Textarea from 'react-expanding-textarea'
import { AddFundsButton } from 'web/components/add-funds-button'
import { ConfirmationButton } from 'web/components/confirmation-button'
import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row'
import { Page } from 'web/components/page'
import { SEO } from 'web/components/SEO'
import { SiteLink } from 'web/components/site-link'
import { Title } from 'web/components/title'
import { defaultBannerUrl } from 'web/components/user-page'
import { generateNewApiKey } from 'web/lib/api/api-key'
import { changeUserInfo } from 'web/lib/firebase/api'
import { redirectIfLoggedOut } from 'web/lib/firebase/server-auth'
import { uploadImage } from 'web/lib/firebase/storage'
import {
  getUserAndPrivateUser,
  updatePrivateUser,
  updateUser,
} from 'web/lib/firebase/users'
export const getServerSideProps = redirectIfLoggedOut('/', async (_, creds) => {
  return { props: { auth: await getUserAndPrivateUser(creds.uid) } }
})
function EditUserField(props: {
  user: User
  field: 'bio' | 'website' | 'bannerUrl' | 'twitterHandle' | 'discordHandle'
  label: string
}) {
  const { user, field, label } = props
  const [value, setValue] = useState(user[field] ?? '')
  async function updateField() {
    // Note: We trim whitespace before uploading to Firestore
    await updateUser(user.id, { [field]: value.trim() })
  }
  return (
    
      {label} 
      {field === 'bio' ? (
        
  )
}
export default function ProfilePage(props: {
  auth: { user: User; privateUser: PrivateUser }
}) {
  const { user, privateUser } = props.auth
  const [avatarUrl, setAvatarUrl] = useState(user.avatarUrl || '')
  const [avatarLoading, setAvatarLoading] = useState(false)
  const [name, setName] = useState(user.name)
  const [username, setUsername] = useState(user.username)
  const [apiKey, setApiKey] = useState(privateUser.apiKey || '')
  const updateDisplayName = async () => {
    const newName = cleanDisplayName(name)
    if (newName) {
      setName(newName)
      await changeUserInfo({ name: newName }).catch((_) => setName(user.name))
    } else {
      setName(user.name)
    }
  }
  const updateUsername = async () => {
    const newUsername = cleanUsername(username)
    if (newUsername) {
      setUsername(newUsername)
      await changeUserInfo({ username: newUsername }).catch((_) =>
        setUsername(user.username)
      )
    } else {
      setUsername(user.username)
    }
  }
  const updateApiKey = async (e?: React.MouseEvent) => {
    const newApiKey = await generateNewApiKey(user.id)
    setApiKey(newApiKey ?? '')
    e?.preventDefault()
    if (!privateUser.twitchInfo) return
    await updatePrivateUser(privateUser.id, {
      twitchInfo: { ...privateUser.twitchInfo, needsRelinking: true },
    })
  }
  const fileHandler = async (event: any) => {
    const file = event.target.files[0]
    setAvatarLoading(true)
    await uploadImage(user.username, file)
      .then(async (url) => {
        await changeUserInfo({ avatarUrl: url })
        setAvatarUrl(url)
        setAvatarLoading(false)
      })
      .catch(() => {
        setAvatarLoading(false)
        setAvatarUrl(user.avatarUrl || '')
      })
  }
  return (
    
       
       
        
           
          
            Done
           
        
         
          
            {avatarLoading ? (
               
            ) : (
              <>
                 
                 
              >
            )}
          
          
            Display name 
              setName(e.target.value || '')}
              onBlur={updateDisplayName}
            />
          
          
            Username 
              setUsername(e.target.value || '')}
              onBlur={updateUsername}
            />
          
          {/* TODO: Allow users with M$ 2000 of assets to set custom banners */}
          {/*   */}
          
            Banner image{' '}
            Not editable for now 
           
          
          {(
            [
              ['bio', 'Bio'],
              ['website', 'Website URL'],
              ['twitterHandle', 'Twitter'],
              ['discordHandle', 'Discord'],
            ] as const
          ).map(([field, label]) => (
             
          ))}
          
            Email 
            
              {privateUser.email ?? '\u00a0'}
            
           
          
            Balance 
            
              {formatMoney(user.balance)}
               
            
           
          
            API key 
            
               
              ,
                }}
                submitBtn={{
                  label: 'Update key',
                  className: 'btn-primary',
                }}
                onSubmitWithSuccess={async () => {
                  updateApiKey()
                  return true
                }}
              >
                
 
                   
                  
                    Updating your API key will break any existing applications
                    connected to your account, 
including the Twitch bot .
                    You will need to go to the{' '}
                    
 
                      
                        Twitch page
                       
                    {' '}
                    to relink your account.
                  
 
                
              
             
           
        
      
     
  )
}