import Game from '../cgt'
import Scene from '../cgt/scene'
import Menu from '../menu'
import App from '../app'
import Logger from '../cgt/utils/logger'

import CreateTact from '../cgt/utils/tact'
import CreateGrid from '../cgt/utils/grid'

export default class StartScene extends Scene {

    start() {

        this.world = App.getWorld()
        this.world.angle.setOptions(20, 100)

        this.menu = null
        this.hovered = null

        Logger.clear()

        this.world.drawAreasWithResources = true

        this.tactResources = CreateTact(10000)
        this.tactMarket = CreateTact(60000)
        this.tactBot = CreateTact(5000)

        App.Market.update()
    }

    update(dt) {
        this.world.update(dt)

        if (this.tactResources()) {
            App.Helpers.processTact(App.Player, this.world.areas)
            App.Helpers.processTact(App.Bot, this.world.areas)

            if (this.world.selectedSector !== null) {
                const area = this.world.areas[this.world.selectedSector]
                this.menu = Menu.generate('build', area)
            }
        }

        if (this.tactBot()) {
            const botDone = App.Helpers.scriptBot(App.Bot)
            if (botDone) {
                Game.scene('end')
            }

            Logger.set('bot ', JSON.stringify(App.Bot.getResources(), null, '\t'))
        }

        if (this.tactMarket()) {
            App.Market.update()
        }
    }

    draw(ctx) {
        this.world.draw(ctx)

        ctx.fillStyle = '#555'
        ctx.font = '18px Lato'
        ctx.textAlign = 'left'
        ctx.fillText('РЕСУРСЫ', 120, 430)

        ctx.fillStyle = '#000'
        ctx.font = '16px Lato'

        const grid = CreateGrid(2, 40, 450, 150, 35)
        ctx.fillStyle = '#000'
        ctx.font = '16px Lato'

        App.Player.getResources().forEach(item => {
            const { x, y } = grid()

            ctx.textAlign = 'right'
            ctx.fillText(item.value, x, y + 10)

            ctx.drawImage(item.icon, x + 10, y, 20, 20)

            ctx.textAlign = 'left'
            ctx.fillText(item.label, x + 40, y + 10)
        })

        // Logger.draw(ctx)

        if (this.menu) {
            this.menu.draw(ctx)
        }
    }

    onMouseZoom(delta) {
        this.world.angle.inc(-Math.sign(delta) * 10)
    }

    onMouseMove(x, y) {
        this.world.hoverObject(x, y)
        if (this.menu) {
            this.menu.mouse(x, y)
        }
    }

    onMouseClick() {
        if (this.world.hoveredSector !== null) {
            this.world.selectedSector = this.world.hoveredSector
            const area = this.world.areas[this.world.hoveredSector]
            this.menu = Menu.generate('build', area)
            return
        }

        if (this.menu) {
            const menuItem = this.menu.getHovered()
            if (menuItem) {
                if (menuItem.action === 'build') {
                    const { area, buildingType, requirements } = menuItem.data

                    area.build(buildingType, App.Player)

                    if (buildingType === 'WIN') {
                        Game.scene('end')
                        return
                    }

                    App.Player.increaseResources(requirements, -1)
                    this.menu = Menu.generate('build', area)

                    return
                }

                if (menuItem.action === 'destroy') {
                    const { area, requirements } = menuItem.data
                    area.removeBuilding()
                    App.Player.increaseResources(requirements)
                    this.menu = Menu.generate('build', area)
                    return
                }

                if (menuItem.action === 'upgrade') {
                    const { area, requirements } = menuItem.data
                    area.upgradeBuilding()
                    App.Player.increaseResources(requirements, -1)

                    if (area.getBuilding().type === 'SCHOOL') {
                        const newLevel = Math.max(
                            area.getBuilding().level,
                            App.Player.getResource('LEVEL').value
                    )
                        App.Player.setResourceAmount('LEVEL', newLevel)
                    }

                    this.menu = Menu.generate('build', area)
                    return
                }

                if (menuItem.action === 'buy') {
                    const { item } = menuItem.data
                    App.Player.increaseResources([ { type: item.from.type, amount: item.from.value } ], -1)
                    App.Player.increaseResources([ { type: item.to.type, amount: item.to.value } ])
                    App.Market.removeOffer(item.id)

                    if (this.world.selectedSector !== null) {
                        const area = this.world.areas[this.world.selectedSector]
                        this.menu = Menu.generate('build', area)
                    }
                }
            }
        }
    }
}
