import { api as entityModule } from '../entity-module/GraphicEntityModule.js'
import { EntityFactory } from '../entity-module/EntityFactory.js'
import { MyTooltipModule } from './MyTooltipModule.js'

import { playerColors } from '../config.js'


export const COL_PASSABLE_BG = 0xffffff;
export const COL_WALL = 0x000000
export const COL_START = 0x00dd00
export const COL_GOAL = 0xee4400
export const COL_OPEN = 0x98fb98
export const COL_CLOSED = 0xafeeee
export const COL_PATH = 0xd7f777
export const COL_LANDMARK  = 0xff0000

export const Z_TILE = 0
export const Z_LANDMARK = 1


export const graphicsHelper = {
    runtimeId: 1000000, // don't interfere with counter from SDK, as this would update the wrong entities from Java code
    playerColors: [0xff4040, 0x4040ff],
    defaults: {
        sprite: { alpha: 1, anchorX: 0, anchorY: 0, baseHeight: null, baseWidth: null, blendMode: 0, curve: {}, image: null, mask: -1, rotation: 0, scaleMode: "LINEAR", scaleX: 1, scaleY: 1, skewX: 0, skewY: 0, t: 0, tint: 0xFFFFFF, visible: true, x: 0, y: 0, zIndex: 0, },
        circle: { alpha: 1, blendMode: 0, curve: {}, fillAlpha: 1, fillColor: 0xFFFFFF, lineAlpha: 1, lineColor: 0, lineWidth: 1, mask: -1, radius: 25, rotation: 0, scaleX: 1, scaleY: 1, skewX: 0, skewY: 0, t: 0, visible: true, x: 0, y: 0, zIndex: 0, },
        group: { alpha: 1, children: [], mask: -1, rotation: 0, scaleX: 1, scaleY: 1, skewX: 0, skewY: 0, visible: true, x: 0, y: 0, zIndex: 0, },
        text: { alpha: 1, anchorX: 0, anchorY: 0, blendMode: 0, fillColor: 0, fontFamily: "Lato", fontSize: 26, fontWeight: "normal", mask: -1, maxWidth: 0, rotation: 0, scaleX: 1, scaleY: 1, skewX: 0, skewY: 0, strokeColor: 0, strokeThickness: 0, text: "", textAlign: "left", tint: 16777215, visible: true, x: 0, y: 0, zIndex: 0, },
        line: { alpha: 1, blendMode: 0, curve: {}, fillAlpha: 1, fillColor: 0xFFFFFF, lineAlpha: 1, lineColor: 0, lineWidth: 1, mask: -1, rotation: 0, scaleX: 1, scaleY: 1, skewX: 0, skewY: 0, t: 0, visible: true, x: 0, y: 0, zIndex: 0, },
        rect: { alpha: 1, blendMode: 0, curve: {}, fillAlpha: 1, fillColor: 0xFFFFFF, lineAlpha: 1, lineColor: 0, lineWidth: 1, mask: -1, width: 25, height: 25, rotation: 0, scaleX: 1, scaleY: 1, skewX: 0, skewY: 0, t: 0, visible: true, x: 0, y: 0, zIndex: 0, },
    }
}

export class MyViewModule {
    /**
     * Corresponds to the moduleName variable used in the Java module.
     */
    static get moduleName  () { return 'MyViewModule' }

    /**
     * Called when global data is received. Should only be called on init.
     * @param players information about players, such as avatar, name, color..
     * @param globalData data that has been sent from the Java module.
     */
    handleGlobalData (players, globalData) {
        this.globalData = { players: players, playerCount: players.length }

        // const g = EntityFactory.create("G")
        //
        // g.id = ++graphicsHelper.runtimeId
        // g.data = { ...graphicsHelper.defaults.group }
        // //g.addState(0, { values: { ...g.data, t:0}, }, frameInfo.number, frameInfo)
        // g.addState(0, { values: { ...g.data, t:0}, }, 0, 0)
        // this.envGroup = g


        if (globalData === undefined) {return } //console.log(globalData)

    }


    /**
     * Called when data is received.
     * Handles data for the given frame. Returns data that will be sent as parameter to updateScene.
     * @param frameInfo information about the current frame.
     * @param frameData data that has been sent from the Java module.
     */
    handleFrameData (frameInfo, frameData) {
        // Handle your data here
        if (frameData === undefined) {return { frameInfo, frameData }}  //console.log(frameData);

        const splitted = frameData.split(" ")

        // for (let sid of splitted)
        // {
        //     const id = parseInt(sid)
        //     if (isNaN(id)) continue
        //
        //     const e = entityModule.entities.get(id)
        //     console.log(id, e);
        //     this.envGroup.data.children.push(e)
        //
        //     //if (e.currentState) e.currentState.x += 100;
        //
        //
        //     //e.addState(0, { values: { ...e.currentState, t:0}, }, frameInfo.number, frameInfo)
        // }
        //
        // //console.log(this.envGroup.children);
        // this.envGroup.data.y = this.envGroup.data.y+50
        // //this.envGroup.data.
        //
        // this.envGroup.addState(1, { values: { ...this.envGroup.data, t:1}, }, frameInfo.number, frameInfo)

        // Return what is necessary to your module
        return { frameInfo, frameData }
    }


    /**
     * Called when the scene needs an update.
     * @param previousData data from the previous frame.
     * @param currentData data of the current frame.
     * @param progress progress of the frame. 0 <= progress <= 1
     * @param speed the speed of the viewer, setted up by the user.
     */
    updateScene (previousData, currentData, progress, speed) { }

    /**
     * Called when the viewer needs to be rerendered (init phase, resized viewer).
     * @param container a PIXI Container. Add your elements to this object.
     * @param canvasData canvas data containing width and height.
     */
    reinitScene (container, canvasData) { }

    /**
     * Called every delta milliseconds.
     * @param delta time between current and last call. Aproximately 16ms by default.
     */
    animateScene (delta) { }
}