export type BaseMeterMode = 'round_inner' | 'round_outer'

export type BaseMeterNeedleType = 'image' | 'line'

export type BaseMeterNeedleImage = {
  image: string
  alpha: number
  recolor: string
  size: {
    width: number
    height: number
  }
  center: {
    x: number
    y: number
  }
}

export type BaseMeterNeedleLine = {
  color: string
  round: boolean
  size: {
    width: number
    height: number
  }
}

export type BaseMeterNeedle = {
  name: string
  type: BaseMeterNeedleType
  value: number
  image: BaseMeterNeedleImage
  line: BaseMeterNeedleLine
}

export type BaseMeterSection = {
  name: string
  start: number
  end: number
  text: string
  line: {
    color: string
    width: number
  }
  curve: {
    color: string
    width: number
  }
}

const getNextIndex = (items: { name: string }[], prefix: string) => {
  const indexes = items
    .map((item) => Number(item.name.replace(`${prefix}_`, '')))
    .filter((value) => !Number.isNaN(value))

  return indexes.length ? Math.max(...indexes) + 1 : 0
}

export const getNextNeedleIndex = (items: BaseMeterNeedle[]) => getNextIndex(items, 'needles')

export const getNextSectionIndex = (items: BaseMeterSection[]) => getNextIndex(items, 'section')

export const createNeedle = (name: string, value = 0): BaseMeterNeedle => {
  return {
    name,
    type: 'line',
    value,
    image: {
      image: '',
      alpha: 255,
      recolor: '#00000000',
      size: {
        width: 60,
        height: 4
      },
      center: {
        x: 0,
        y: 2
      }
    },
    line: {
      color: '#212121FF',
      round: false,
      size: {
        width: 4,
        height: 60
      }
    }
  }
}

export const createSection = (name: string): BaseMeterSection => {
  return {
    name,
    start: 0,
    end: 20,
    text: '#FFFF00FF',
    line: {
      color: '#FF00FFFF',
      width: 3
    },
    curve: {
      color: '#FF00FFFF',
      width: 2
    }
  }
}
