import { Scene } from 'phaser'
import { emitter } from '@/plugins'
import map from '@/assets/game/assets/map3.json'
import street from '@/assets/game/assets/street.png'
import BuildingsCombineV3 from '@/assets/game/assets/BuildingsCombineV3.png'
// import datGuiSetup from '@/game/dat.gui.setup.js'
// import animatedTiles from '@/game/plugin/animatedTiles.js'

enum Direction {
    LEFT = 'l',
    RIGHT = 'r',
    UP = 'u',
    DOWN = 'd'
}
interface Path {
    postion: {
        x: number
        y: number
    }
    directions: Array<Direction>
}
interface Person {
    wait: boolean
    sprite: null | any
    paths: Array<Path>
}

function getRandom(num = 1) {
    return Math.floor(Math.random() * num)
}

export default class PlayScene3 extends Scene {
    animatedTiles: any
    constructor() {
        super({ key: 'PlayScene3' })
    }
    private map: any
    private enterBtn: any
    private personInfo = <any>{
        Isabella_Rodriguez: <Person>{
            wait: false,
            sprite: null,
            paths: []
        },
        Arthur_Burton: <Person>{
            wait: false,
            sprite: null,
            paths: []
        },
        Tamara_Taylor: <Person>{
            wait: false,
            sprite: null,
            paths: []
        },
        Abigail_Chen: <Person>{
            wait: false,
            sprite: null,
            paths: []
        },
        Klaus_Mueller: <Person>{
            wait: false,
            sprite: null,
            paths: []
        },
        Wolfgang_Schulz: <Person>{
            wait: false,
            sprite: null,
            paths: []
        },
        Carlos_Gomez: <Person>{
            wait: false,
            sprite: null,
            paths: []
        }
    }

    preload() {
        this.load.image('street', street)
        this.load.image('BuildingsCombineV3', BuildingsCombineV3)
        this.load.tilemapTiledJSON('map3', map)
        // this.load.scenePlugin({
        //     key: 'AnimatedTiles',
        //     url: animatedTiles,
        //     sceneKey: 'animatedTiles'
        // })
    }

    create() {
        // this.add.tileSprite(0, 0, 1024, 1024, 'grass').setOrigin(0, 0)
        this.map = this.make.tilemap({
            key: 'map3',
            tileWidth: 16,
            tileHeight: 16
        })
        const street = this.map.addTilesetImage('street', 'street')
        const BuildingsCombineV3 = this.map.addTilesetImage('BuildingsCombineV3', 'BuildingsCombineV3')
        this.map.createLayer('bg', [street], 0, 0)
        this.map.createLayer('building', [street, BuildingsCombineV3], 0, 0)
        this.map.createLayer('tree', [street, BuildingsCombineV3], 0, 0)
        this.animatedTiles.init(this.map)
    }

    update() {
        this.animatedTiles.updateAnimatedTiles()
        // Object.keys(this.personInfo).forEach((key: string) => {
        //     const info = this.personInfo[key]
        //     switch (info.direction) {
        //         case 'l':
        //             info.sprite.x -= 1
        //             break
        //         case 'r':
        //             info.sprite.x += 1
        //             break
        //         case 'u':
        //             info.sprite.y -= 1
        //             break
        //         case 'd':
        //             info.sprite.y += 1
        //             break
        //     }
        //     const walkName = {
        //         [Direction.LEFT]: key + '-left-walk',
        //         [Direction.RIGHT]: key + '-right-walk',
        //         [Direction.UP]: key + '-up-walk',
        //         [Direction.DOWN]: key + '-down-walk'
        //     }
        //     const x = info.sprite.x
        //     const y = info.sprite.y
        //     info.paths.forEach((path: Path) => {
        //         if (x === path.postion.x && y === path.postion.y) {
        //             const direction = path.directions[getRandom(path.directions.length)]
        //             info.sprite.play(walkName[direction], true)
        //             info.direction = direction
        //         }
        //     })
        // })
    }
}
