import Menu from './menu'
import MenuItem from './menu-item'
import config from './config'
import App from '../app'

const resTitle = type => App.Config.resources[type] ? App.Config.resources[type].title : ''

const makeBuildMenuItem = (menu, title, requirements, level, x, y, btn_text, action, data) => {
    menu.addLabel(title, x, y, 14)

    menu.addResources(requirements, x, y + 20)

    // if (buildProduct) {
    //     menu.addLabel(buildProduct, x, y, 14)
    // }

    menu.add(new MenuItem({
        text: btn_text,
        x: x,
        y: y + 80,
        width: 125,
        height: 40,
        size: 18,
        border: true,
        action,
        disabled: !App.Helpers.isMetRequirements(App.Player, requirements, level),
        data
    }))
}

const generate = (type, area) => {
    const menu = new Menu()

    menu.addLabel(area.info().title, 990, 400)

    const building = area.getBuilding()
    if (building) {
        const buildingInfo = App.getBuildingInfo(building.type)

        let convert = ''
        if (building.config.convert && building.config.convert.length) {
            const titles = building.config.convert.map(item => resTitle(item.type))
            convert = ' из ' + titles.join(', ')
        }

        const buildProductType = resTitle(building.config.produce)
        let buildProduct = ''
        if (buildProductType) {
            buildProduct = 'производит ' + resTitle(building.config.produce) + convert
        }

        const infoText = [
            buildingInfo.title,
            area.buildingOwner === App.Player ? 'игрока' : 'бота',
            'уровень ' + (building.level + 1),
            buildProduct
        ].join(' ')
        menu.addLabel(infoText, 990, 440, 18)

        if (area.buildingOwner === App.Player) {
            if (building.type === 'MARKET') {
                const market = App.Market.get()
                market.forEach((item, i) => {
                    const x = 900
                    const y = 490 + i * 60
                    menu.addResources([ { type: item.from.type, amount: item.from.value } ], x, y)
                    menu.addLabel('>', x + 52, y + 10)
                    menu.addResources([ { type: item.to.type, amount: item.to.value } ], x + 90, y)
                    menu.add(new MenuItem({
                        text: 'Обменять',
                        x: x + 210,
                        y: y + 12,
                        width: 125,
                        height: 40,
                        size: 18,
                        border: true,
                        action: 'buy',
                        disabled: !App.Helpers.isMetRequirements(App.Player, [ { type: item.from.type, amount: item.from.value } ]),
                        data: { item }
                    }))
                })
            }
            else {
                let pos_y = 450

                const maxLevel = buildingInfo.requirements.length - 1
                if (building.level < maxLevel) {
                    const requirements = buildingInfo.requirements[building.level + 1]
                    const data = { area, buildingType: building.type, requirements }
                    const level = building.type === 'SCHOOL' ? 0 : building.level + 1
                    makeBuildMenuItem(menu, '',
                        requirements, level, 990, pos_y, 'Улучшить', 'upgrade', data)

                    pos_y += 130
                } else {
                    pos_y += 130
                }

                const requirements = buildingInfo.requirements[0]
                menu.add(new MenuItem({
                    text: 'Снести',
                    x: 990,
                    y: pos_y,
                    width: 125,
                    height: 40,
                    size: 18,
                    border: true,
                    action: 'destroy',
                    data: { area, requirements }
                }))
            }
        }
    }
    else {
        const availableBuildings = area.getAvalilableBuildings()
        const columnWidth = 200
        availableBuildings.forEach((buildingType, i) => {
            const offset_x = columnWidth * i - availableBuildings.length * columnWidth / 2 + columnWidth / 2
            const buildingInfo = App.getBuildingInfo(buildingType)

            // const buildProductType = resTitle(buildingInfo.produce)
            // let buildProduct = ''
            // if (buildProductType) {
            //     buildProduct = 'производит ' + resTitle(buildingInfo.produce)
            // }

            const requirements = buildingInfo.requirements[0]
            const data = { area, buildingType, requirements }
            makeBuildMenuItem(menu, buildingInfo.title.toUpperCase(),
                requirements, 0, 990 + offset_x, 450, 'Построить', 'build', data)

        })
    }

    return menu
}

export default {
    get: name => new Menu(config[name]),
    generate
}
