import assets from '../../../Assets/Decors/**/*.yaml'
import { Decor, DecorationPart } from '../data/decor'
import * as PIXI from 'pixi.js'

export const decors: Record<string, Decor> = {}

for (let [key, value] of Object.entries(assets)) {
  decors[key] = new Decor(value)
}

export interface DecorationFrame {
  time: number
  container: PIXI.Container
  sprites: (PIXI.Sprite|PIXI.Text)[]
}

export class DecorationInstance {
  elapsed: number = 0
  container: PIXI.Container
  frames: DecorationFrame[] = []
  frameIndex: number = 0
  constructor(decorUUID: string, decorationUUID: string) {
    this.container = new PIXI.Container()
    let found = false
    for (let decor of Object.values(decors)) {
      if (decor.uuid === decorUUID) {
        for (let decoration of decor.decorations) {
          if (decoration.uuid === decorationUUID) {
            this.container.width = decoration.width
            this.container.height = decoration.height

            for (let frame of decoration.frames) {
              let frameInstance: DecorationFrame = {
                time: frame.time,
                container: new PIXI.Container(),
                sprites: frame.parts.map((v: DecorationPart) => {
                  let sprite = PIXI.Sprite.from(v.texture)
                  sprite.pivot.x += sprite.width/2
                  sprite.pivot.y += sprite.height/2
                  sprite.x = v.x
                  sprite.y = v.y
                  sprite.rotation = v.rotation
                  sprite.alpha = v.alpha
                  /*if (v.flip) {
                    sprite.pivot.y = -sprite.height
                    sprite.y += sprite.pivot.y
                  }
                  if (v.mirror) {
                    sprite.pivot.x = -sprite.width
                    sprite.x += sprite.pivot.x
                  }*/
                  // TODO: mirror and flip
                  return sprite
                })
              }
              frameInstance.container.width = decoration.width
              frameInstance.container.height = decoration.height
              for (let s of frameInstance.sprites){ 
                frameInstance.container.addChild(s)
              }
              this.frames.push(frameInstance)
            }

            found = true
            break
          }
        }
      }
      if (found) {
        break
      }
    }
    if (!found) {
      console.log('failed to find', decorUUID, decorationUUID)
      // bogus sprite
      let t = new PIXI.Text('missing')
      this.frames.push({
        time: 0,
        container: new PIXI.Container(),
        sprites: [t],
      })
      this.frames[this.frames.length-1].container.addChild(t)
    }
    // Add first frame to container.
    if (this.frames.length > 0) {
      this.container.addChild(this.frames[0].container)
    }
  }
  update(delta: number) {
    if (this.frames.length <= 1) return
    this.elapsed += delta
    let frameIndex = this.frameIndex
    while (this.elapsed >= this.frames[frameIndex].time) {
      this.elapsed -= this.frames[frameIndex].time
      frameIndex++
      if (frameIndex >= this.frames.length) {
        frameIndex = 0
      }
      this.container.removeChild(this.frames[this.frameIndex].container)
      this.frameIndex = frameIndex
      this.container.addChild(this.frames[this.frameIndex].container)
    }
  }
}
