export class Segment {
  title: string
  width: number
  height: number
  bg: string | string[]
  zones: SegmentZone[] = []
  layers: SegmentLayer[] = []

  constructor(o: any) {
    this.title = o.title
    this.height = o.rows * o.cellHeight
    this.width = o.columns * o.cellWidth
    this.bg = o.backgroundColor

    for (let z of o.zones) {
      this.zones.push(new SegmentZone(z))
    }

    for (let l of o.layers) {
      this.layers.push(new SegmentLayer(l))
    }
  }
}

export class SegmentLayer {
  title: string
  darkness: number = 0
  contrast: number = 100
  decorations: SegmentDecoration[] = []

  constructor(o: any) {
    this.title = o.title
    this.darkness = o.darkness
    this.contrast = o.contrast

    for (let d of o.decorations) {
      this.decorations.push(new SegmentDecoration(d))
    }
  }
}

export class SegmentDecoration {
  decor: string
  decoration: string
  x: number
  y: number
  mirror: boolean
  flip: boolean
  timeOffset: number = 0
  speed: number = 1.0
  rotation: number = 0
  constructor(o: any) {
    this.decor = o.decor
    this.decoration = o.decoration
    this.x = o.x
    this.y = o.y
    this.mirror = o.mirror
    this.flip = o.flip
    if (!isNaN(o.timeOffset)) {
      this.timeOffset = o.timeOffset
    }
    if (!isNaN(o.speed)) {
      this.speed = o.speed
    }
    if (!isNaN(o.rotation)) {
      this.rotation = o.rotation
    }
  }
}

export class SegmentZone {
  uuid: string
  points: [number, number][] = []
  type: 'solid'|'fluid'
  constructor(o: any) {
    this.uuid = o.uuid
    this.points = o.points.map((v: any) => {
      return [v.x, v.y]
    })
    if (o.data.type === 'fluid') {
      this.type = o.data.type
    } else {
      this.type = 'solid'
    }
  }
}