import { Badge, Tag } from 'antd'

import { EntityRemediationTicketPhaseApproval } from 'apiClient/services/devops/interface'

const regionColors: { [key: string]: string } = {
  jp: 'orange',
  apac: 'magenta',
  na: 'geekblue',
  emea: 'green'
}

const tierColors: { [key: string]: string } = {
  t0: '#79155B',
  t1: '#C23373',
  t2: '#F6635C',
  t3: '#FFBA86'
}

const statusColors: { [key: string]: string } = {
  ongoing: 'orange',
  'post-mortem': 'purple',
  'under-improvement': 'blue',
  archived: 'green'
}

const statusText: { [key: string]: string } = {
  ongoing: 'Ongoing',
  'post-mortem': 'Post Mortem',
  'under-improvement': 'Under Improvement',
  archived: 'Archived'
}

const renderRegionString = (regionString: string) => {
  var regions = regionString?.split(' ')
  if (regions) {
    return regions
      .filter((e) => !!e)
      .map((region) => (
        <Tag color={regionColors[region] ? regionColors[region] : 'purple'}>
          {region === 'jp' ? 'Japan' : region.toUpperCase()}
        </Tag>
      ))
  } else {
    return <></>
  }
}

const renderStatusString = (statusString: string | undefined) => {
  const status = statusString
  if (status) {
    return (
      <Badge
        color={statusColors[status] ? statusColors[status] : 'grey'}
        text={statusText[status] ? statusText[status] : 'Unknown'}
      />
    )
  } else {
    return <></>
  }
}

const renderComponentString = (componentsString: string | undefined) => {
  const components = componentsString?.split(' ')
  if (components) {
    return components.map((component) =>
      component ? <Tag color="blue">{component}</Tag> : <></>
    )
  } else {
    return <></>
  }
}

// Used in approval diag
const defaultApprovalInfo =
  'Please check all the information carefully and decide whether to approve or reject'

const findTicketApprovalByID = (
  ticketApprovals: EntityRemediationTicketPhaseApproval[],
  apprId: number
) => {
  for (var i = 0; i < ticketApprovals.length; i++) {
    if (ticketApprovals[i].approval_id === apprId) return ticketApprovals[i]
  }

  return undefined
}

export {
  regionColors,
  tierColors,
  statusColors,
  renderRegionString,
  renderStatusString,
  renderComponentString,
  defaultApprovalInfo,
  findTicketApprovalByID
}
