import { ContractComment } from 'common/comment'
import { Contract } from 'common/contract'
import React, { useEffect, useState } from 'react'
import { useUser } from 'web/hooks/use-user'
import { formatMoney } from 'common/util/format'
import { useRouter } from 'next/router'
import { Row } from 'web/components/layout/row'
import clsx from 'clsx'
import { Avatar } from 'web/components/avatar'
import { OutcomeLabel } from 'web/components/outcome-label'
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
import { firebaseLogin } from 'web/lib/firebase/users'
import { createCommentOnContract } from 'web/lib/firebase/comments'
import { Col } from 'web/components/layout/col'
import { track } from 'web/lib/service/analytics'
import { Tipper } from '../tipper'
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
import { Content } from '../editor'
import { Editor } from '@tiptap/react'
import { UserLink } from 'web/components/user-link'
import { CommentInput } from '../comment-input'
export function FeedCommentThread(props: {
  contract: Contract
  threadComments: ContractComment[]
  tips: CommentTipMap
  parentComment: ContractComment
}) {
  const { contract, threadComments, tips, parentComment } = props
  const [showReply, setShowReply] = useState(false)
  const [replyTo, setReplyTo] = useState<{ id: string; username: string }>()
  function scrollAndOpenReplyInput(comment: ContractComment) {
    setReplyTo({ id: comment.userId, username: comment.userUsername })
    setShowReply(true)
  }
  return (
    
      
      {[parentComment].concat(threadComments).map((comment, commentIdx) => (
        
      ))}
      {showReply && (
        
          
           {
              setShowReply(false)
            }}
          />
        
      )}
    
  )
}
export function FeedComment(props: {
  contract: Contract
  comment: ContractComment
  tips: CommentTips
  indent?: boolean
  onReplyClick?: (comment: ContractComment) => void
}) {
  const { contract, comment, tips, indent, onReplyClick } = props
  const {
    text,
    content,
    userUsername,
    userName,
    userAvatarUrl,
    commenterPositionProb,
    commenterPositionShares,
    commenterPositionOutcome,
    createdTime,
  } = comment
  const betOutcome = comment.betOutcome
  let bought: string | undefined
  let money: string | undefined
  if (comment.betAmount != null) {
    bought = comment.betAmount >= 0 ? 'bought' : 'sold'
    money = formatMoney(Math.abs(comment.betAmount))
  }
  const [highlighted, setHighlighted] = useState(false)
  const router = useRouter()
  useEffect(() => {
    if (router.asPath.endsWith(`#${comment.id}`)) {
      setHighlighted(true)
    }
  }, [comment.id, router.asPath])
  return (
    
  )
}
function CommentStatus(props: {
  contract: Contract
  outcome: string
  prob?: number
}) {
  const { contract, outcome, prob } = props
  return (
    <>
      {` predicting `}
      
      {prob && ' at ' + Math.round(prob * 100) + '%'}
    >
  )
}
export function ContractCommentInput(props: {
  contract: Contract
  className?: string
  parentAnswerOutcome?: string | undefined
  replyToUser?: { id: string; username: string }
  parentCommentId?: string
  onSubmitComment?: () => void
}) {
  const user = useUser()
  async function onSubmitComment(editor: Editor) {
    if (!user) {
      track('sign in to comment')
      return await firebaseLogin()
    }
    await createCommentOnContract(
      props.contract.id,
      editor.getJSON(),
      user,
      props.parentAnswerOutcome,
      props.parentCommentId
    )
    props.onSubmitComment?.()
  }
  return (
    
  )
}