import { Graphics, Container } from 'pixi.js'
import type { ContainerOptions, TileDto } from './interfaces'
import { defaultContainerOptions } from './constants'

export function getColorBackground(container: Container, colorName: string, alpha: number = 0.5) {
  const graphics = new Graphics()
  const color = 0xffffff
  graphics.rect(0, 0, container.width, container.height)
  graphics.fill(color)
  graphics.alpha = alpha
  graphics.x = 0
  graphics.y = 0
  return graphics
}

export function createContainer(options: ContainerOptions) {
  const opts = { ...defaultContainerOptions, ...options }
  const container = new Container()

  const rect = new Graphics().rect(opts.x, opts.y, opts.width, opts.height)

  if (opts.color) {
    rect.fill(opts.color)
  }
  rect.visible = opts.visible
  container.addChild(rect)
  if (opts.parent) {
    opts.parent.addChild(container)
  }
  return container
}

export async function wait(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms))
}

export const DIRECTIONS = ['north', 'east', 'south', 'west']

export function isTilePair(tile: TileDto): boolean {
  return !!(tile.pips && tile.pips[0] === tile.pips[1])
}

export function isTileVertical(tile: TileDto): boolean {
  return tile.orientation === 'north' || tile.orientation === 'south'
}
