import React, { ReactElement } from 'react'
import { useSiteMetadata } from '../../../hooks/use-site-metadata'
import * as styles from './Actions.module.css'
import Icon from '../../atoms/Icon'

interface ActionProps {
  title: string
  text: string
  url?: string
  onClick?(): void
}

const IconComp = ({ text }: { text: string }) =>
  text.includes('GitHub') ? (
    <Icon name="GitHub" />
  ) : text.includes('Bitcoin') ? (
    <Icon name="Bitcoin" />
  ) : (
    <Icon name="Twitter" />
  )

const Action = ({ title, text, url, onClick }: ActionProps) => {
  return (
    <a className={styles.action} href={url} onClick={onClick}>
      <IconComp text={text} />
      <h1 className={styles.actionTitle}>{title}</h1>
      <p className={styles.actionText}>{text}</p>
    </a>
  )
}

export default function PostActions({
  slug,
  githubLink
}: {
  slug: string
  githubLink: string
}): ReactElement {
  const { siteUrl } = useSiteMetadata()
  const urlTwitter = `https://twitter.com/intent/tweet?text=@kremalicious&url=${siteUrl}${slug}`

  return (
    <aside className={styles.actions}>
      <Action
        title="Have a comment?"
        text="Hit me up @kremalicious"
        url={urlTwitter}
      />
      <Action
        title="Found something useful?"
        text="Say thanks with BTC or ETH"
        url="/thanks"
      />
      <Action
        title="Edit on GitHub"
        text="Contribute to this post"
        url={githubLink}
      />
    </aside>
  )
}
