export const utils = (orientation: string, size: number) => {
  const sizeAttr = orientation === 'v' ? 'width' : 'height'
  let percentage: string
  if (size) {
    percentage = `${size}%`
  } else {
    percentage = orientation === 'v' ? '40%' : '80%'
  }
  return { [sizeAttr]: percentage }
}

export const parseBorderMargin = (marginString: any) => {
  const numberPattern = '[+-]?[0-9]+\\.?[0-9]*'
  const unitPattern = '(px|em|ex|%|in|cm|mm|pt|pc)'
  const spacePattern = '( )?'
  const fullPattern = `(${spacePattern}${numberPattern}${unitPattern})?`
  const pattern = new RegExp(fullPattern, 'g')
  const match = marginString ? marginString.match(pattern) : ['']

  switch (match.length) {
    case 1:
      return { r: '4%', l: '4%' }
    case 2:
      return { r: match[0], l: match[0] }
    default:
      return { r: match[0], l: match[1] }
  }
}

const parseElementStyles = (margin: string, position: number, defaultPosition: number, parameters: VixonicParameters) => {
  const { orientation, separator } = parameters
  const marginAttr = orientation === 'v' ? 'marginBottom' : 'marginRight'
  const sizeAttr = orientation === 'v' ? 'width' : 'height'
  const pos =  position ?? defaultPosition
  return {
    order: pos,
    [sizeAttr]: '100%',
    [marginAttr]: getMarginValue(margin, pos, separator)
  }
}

export const parseParameters = (parameters: any) => {
  const imageAlignment = parameters.imageAlignment || 'center'
  const textAlignment = parameters.textAlignment || 'center'
  const dateAlignment = parameters.dateAlignment || 'center'

  return {
    imageEnabled: parameters.imageEnabled,
    imageStyle: {
      flexShrink: 0,
      display: 'flex',
      justifyContent: imageAlignment,
      alignItems: imageAlignment,
      ...parseElementStyles(parameters.imageMargin, parameters.imagePosition, 1, parameters)
    },
    descriptionEnabled: parameters.descriptionEnabled,
    optionalEnabled: parameters.optionalEnabled,
    textStyle: {
      display: 'flex', 
      flexDirection: 'column', 
      justifyContent: textAlignment, 
      flex: 1,
      ...parseElementStyles(parameters.textMargin, parameters.textPosition, 2, parameters)
    },
    dateEnabled: parameters.dateEnabled,
    dateStyle: {
      display: 'flex',
      justifyContent: dateAlignment,
      alignItems: dateAlignment,
      flexShrink: 0,
      ...parseElementStyles(parameters.dateMargin, parameters.datePosition, 3, parameters)
    },
  }
}

export const getPhotoUrl = (filename: string, parameters: VixonicParameters, downloadsPath: string) => {
  const mode = parameters.photoMode
  if (mode === 'zip' && parameters.photosZip?.filename !== undefined) {
    return downloadsPath + '/' + parameters.photosZip.filename.replace('.zip', '/') + filename
  } else {
    return filename
  }
}

const getMarginValue = (margin: string, pos: number, separator?: boolean): string | number => {
  if (margin !== undefined && margin !== '') {
    return `${margin}%`;
  }
  return pos === 3 || separator === true ? 0 : '4%';
};
