import * as PIXI from 'pixi.js'

export class Decor {
  uuid: string
  texture: PIXI.Texture
  decorations: Decoration[] = []

  constructor(o: any) {
    this.uuid = o.uuid
    this.texture = PIXI.Texture.from('./Assets/Decors/'+o.image)
    this.texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST
    // Load decorations.
    for (let d of o.decorations) {
      this.decorations.push(new Decoration(this.texture.baseTexture, d))
    }
  }
}

export class Decoration {
  uuid: string
  frames: DecorationFrame[] = []
  width: number = 0
  height: number = 0
  timeOffset: number = 0
  constructor(t: PIXI.BaseTexture, o: any) {
    this.width = o.width
    this.height = o.height
    this.uuid = o.uuid
    if (!isNaN(o.timeOffset)) {
      this.timeOffset = o.timeOffset
    }
    for (let frame of o.frames) {
      this.frames.push(new DecorationFrame(t, frame))
    }
  }
}

export class DecorationFrame {
  parts: DecorationPart[] = []
  time: number = 0
  constructor(t: PIXI.BaseTexture, o: any) {
    this.time = o.frame_time
    for (let p of o.parts) {
      this.parts.push(new DecorationPart(t, p))
    }
  }
}

export class DecorationPart {
  texture: PIXI.Texture
  clipX: number = 0
  clipY: number = 0
  clipWidth: number = 0
  clipHeight: number = 0
  x: number = 0
  y: number = 0
  alpha: number = 1
  rotation: number = 0
  mirror: boolean = false
  flip: boolean = false
  constructor(t: PIXI.BaseTexture, o: any) {
    this.clipX = o.clipX
    this.clipY = o.clipY
    this.clipWidth = o.clipWidth
    this.clipHeight = o.clipHeight
    this.x = o.x
    this.y = o.y
    this.alpha = o.alpha
    this.rotation = o.rotation
    this.mirror = o.mirror
    this.flip = o.flip
    // Hmm.
    this.texture = new PIXI.Texture(t, new PIXI.Rectangle(this.clipX, this.clipY, this.clipWidth, this.clipHeight))
  }
}

// This loads a file in an expected Vio Decor format and returns an array of Decor entries.
export function load(obj: any) {

}