import { useMemo } from 'react'

import {
  getRailsConnectionStr,
  GoConnectionStr,
  GoSampleCode,
  JDBCConntionStr,
  JDBCSampleCode,
  MyCLIConnectionStr,
  MySQLConnectionStr,
  NodeConntionStr,
  NodeSampleCode,
  PythonConnectStr,
  PythonSampleCode,
  SampleCodeConnection
} from '../ClientDriversExamples'
import ConnectionCode from '../ConnectionCode'

import { ClientConst, ClientType } from './types'

export function usePrivateConnectionCodes({
  disabled,
  endpoint,
  dnsName,
  port,
  onCopy
}: {
  disabled?: boolean
  endpoint: string
  dnsName: string
  port: number
  onCopy?: () => void
}) {
  return useMemo(() => {
    if (disabled) {
      return {} as Record<ClientType, JSX.Element>
    }

    const host = `${endpoint}.${dnsName}`
    const connectionInfo = new SampleCodeConnection('root', host, port, '')

    const codeTable = [
      {
        name: ClientConst.MySQL,
        code: (
          <ConnectionCode
            language="bash"
            menuName="MySQL"
            connection={MySQLConnectionStr(connectionInfo)}
            onCopy={onCopy}
            onSelectionCopy={onCopy}
          />
        )
      },
      {
        name: ClientConst.MyCLI,
        code: (
          <ConnectionCode
            language="bash"
            menuName="MyCLI"
            connection={MyCLIConnectionStr(connectionInfo)}
            onCopy={onCopy}
            onSelectionCopy={onCopy}
          />
        )
      },
      {
        name: ClientConst.Rails,
        code: (
          <ConnectionCode
            language="bash"
            menuName="Rails"
            connection={getRailsConnectionStr(connectionInfo)}
            comment={`# Please remember to update the <your_password> placeholder with your actual password.\n# Requires mysql2 gem ('gem install mysql2').`}
            onCopy={onCopy}
            onSelectionCopy={onCopy}
          />
        )
      },
      {
        name: ClientConst.JDBC,
        code: (
          <ConnectionCode
            language="clike"
            menuName="JDBC"
            showCheckBox
            connection={JDBCConntionStr(connectionInfo)}
            fullCode={JDBCSampleCode(connectionInfo)}
            comment="/* Be sure to replace the parameters in the following connection string. */"
            onCopy={onCopy}
            onSelectionCopy={onCopy}
          />
        )
      },
      {
        name: ClientConst.Python,
        code: (
          <ConnectionCode
            language="python"
            menuName="Python"
            showCheckBox
            connection={PythonConnectStr(connectionInfo)}
            fullCode={PythonSampleCode(connectionInfo)}
            comment={`# Please remember to update the <your_password> placeholder with your actual password.\n# Requires mysqlclient package ('pip3 install mysqlclient'). Please check https://pypi.org/project/mysqlclient/ for install guide.`}
            onCopy={onCopy}
            onSelectionCopy={onCopy}
          />
        )
      },
      {
        name: ClientConst.Go,
        code: (
          <ConnectionCode
            language="go"
            menuName="Go"
            showCheckBox
            connection={GoConnectionStr(connectionInfo)}
            fullCode={GoSampleCode(connectionInfo)}
            comment="/* Please remember to update the <your_password> placeholder with your actual password. */"
            onCopy={onCopy}
            onSelectionCopy={onCopy}
          />
        )
      },
      {
        name: ClientConst.Node,
        code: (
          <ConnectionCode
            language="javascript"
            menuName="Node.js"
            showCheckBox
            connection={NodeConntionStr(connectionInfo)}
            fullCode={NodeSampleCode(connectionInfo)}
            comment={`/*\n * Please remember to update the <your_password> placeholder with your actual password.\n * Requires mysql2 package('npm install --save mysql2'). Please check https://www.npmjs.com/package/mysql2 for install guide.\n */`}
            onCopy={onCopy}
            onSelectionCopy={onCopy}
          />
        )
      }
    ]
    const codeHashmap = codeTable.reduce((prev, { name, code }) => {
      prev[name] = code

      return prev
    }, {} as Record<ClientType, JSX.Element>)

    return codeHashmap
  }, [disabled, dnsName, endpoint, port])
}
