import { Alignment, pad } from '../../../string'
import { RUMObject } from '../../../types'
import {
  DEFAULT_022_RSS_FORMAT,
  DEFAULT_ADM,
  DEFAULT_AGE_GESTATION,
  DEFAULT_AUTHORISATION,
  DEFAULT_BIRTH_DATE,
  DEFAULT_CAT,
  DEFAULT_CONF,
  DEFAULT_CONV,
  DEFAULT_CP_RES,
  DEFAULT_DESTINATION,
  DEFAULT_DP,
  DEFAULT_DR,
  DEFAULT_ENTRY_DATE,
  DEFAULT_ENTRY_MODE,
  DEFAULT_ER,
  DEFAULT_EXIT_DATE,
  DEFAULT_EXIT_MODE,
  DEFAULT_FILL,
  DEFAULT_FINESS,
  DEFAULT_IGS,
  DEFAULT_INNOV,
  DEFAULT_LAST_MENS_DATE,
  DEFAULT_NP,
  DEFAULT_PROVENANCE,
  DEFAULT_RAAC,
  DEFAULT_RES,
  DEFAULT_RH,
  DEFAULT_RSS,
  DEFAULT_RT_D,
  DEFAULT_RT_M,
  DEFAULT_RUM,
  DEFAULT_SESSION,
  DEFAULT_SEX,
  DEFAULT_SPEC,
  DEFAULT_TARIF,
  DEFAULT_U_MED,
  DEFAULT_WEIGHT
} from '../../shared'
import { formatZARecord } from './022-format-za-record'

// Generate one RUM RSS 022 record line
export function parseRUMObjectTo022FormatString(params: RUMObject): string {
  const {
    FINESS = DEFAULT_FINESS,
    RSS = DEFAULT_RSS,
    ADM = DEFAULT_ADM,
    RUM = DEFAULT_RUM,
    birthDate = DEFAULT_BIRTH_DATE,
    sex = DEFAULT_SEX,
    uMed = DEFAULT_U_MED,
    authorisation = DEFAULT_AUTHORISATION,
    entryDate = DEFAULT_ENTRY_DATE,
    entryMode = DEFAULT_ENTRY_MODE,
    provenance = DEFAULT_PROVENANCE,
    exitDate = DEFAULT_EXIT_DATE,
    exitMode = DEFAULT_EXIT_MODE,
    destination = DEFAULT_DESTINATION,
    cpRes = DEFAULT_CP_RES,
    weight = DEFAULT_WEIGHT,
    ageGestation = DEFAULT_AGE_GESTATION,
    lastMensDate = DEFAULT_LAST_MENS_DATE,
    session = DEFAULT_SESSION,
    DP = DEFAULT_DP,
    DR = DEFAULT_DR,
    IGS = DEFAULT_IGS,
    conf = DEFAULT_CONF,
    rtM = DEFAULT_RT_M,
    rtD = DEFAULT_RT_D,
    innov = DEFAULT_INNOV,
    conv = DEFAULT_CONV,
    raac = DEFAULT_RAAC,
    spec = DEFAULT_SPEC,
    RH = DEFAULT_RH,
    tarif = DEFAULT_TARIF,
    cat = DEFAULT_CAT,
    NP = DEFAULT_NP,
    ER = DEFAULT_ER,
    FILL = DEFAULT_FILL,
    RES = DEFAULT_RES,
    DAs = [],
    DADs = [],
    ZAs = []
  } = params

  const { nDA = DAs.length, nDAD = DADs.length, nZA = ZAs.length } = params

  const fixed = [
    pad(FINESS, 9, Alignment.LEFT),
    DEFAULT_022_RSS_FORMAT, // Format version
    pad(RSS, 20, Alignment.LEFT),
    pad(ADM, 20, Alignment.LEFT),
    pad(RUM, 10, Alignment.LEFT),
    birthDate,
    sex,
    pad(uMed, 4, Alignment.LEFT),
    pad(authorisation ?? '', 2, Alignment.LEFT),
    entryDate,
    entryMode,
    pad(provenance, 1, Alignment.LEFT),
    exitDate,
    exitMode,
    pad(destination, 1, Alignment.LEFT),
    cpRes,
    pad(weight, 4, Alignment.RIGHT, weight && weight.length > 0 ? '0' : ' '),
    pad(ageGestation, 2, Alignment.RIGHT, ageGestation && ageGestation.length > 0 ? '0' : ' '),
    pad(lastMensDate, 8, Alignment.LEFT, ' '),
    pad(session.toString(), 2, Alignment.RIGHT, '0'),
    pad(nDA.toString(), 2, Alignment.RIGHT, '0'),
    pad(nDAD.toString(), 2, Alignment.RIGHT, '0'),
    pad(nZA.toString(), 3, Alignment.RIGHT, '0'),
    pad(DP, 8, Alignment.LEFT),
    pad(DR, 8, Alignment.LEFT),
    pad(IGS, 3, Alignment.RIGHT, '0'),
    pad(conf ?? '', 1, Alignment.LEFT),
    pad(rtM, 1, Alignment.LEFT),
    pad(rtD, 1, Alignment.LEFT),
    pad(innov, 15, Alignment.LEFT),
    pad(conv, 1, Alignment.LEFT),
    pad(raac, 1, Alignment.LEFT),
    pad(spec, 1, Alignment.LEFT),
    pad(RH, 1, Alignment.LEFT),
    pad(tarif, 1, Alignment.LEFT),
    pad(cat, 1, Alignment.LEFT),
    pad(NP, 1, Alignment.LEFT),
    pad(ER, 1, Alignment.LEFT),
    pad(FILL, 4, Alignment.LEFT),
    pad(RES, 3, Alignment.LEFT)
  ]

  const das = DAs.map((code) => pad(code, 8, Alignment.LEFT)).join('')
  const dads = DADs.map((code) => pad(code, 8, Alignment.LEFT)).join('')
  const zas = ZAs.map(formatZARecord).join('')

  const result = [...fixed, das, dads, zas].join('')

  return result
}
