import bindMenu from '../bind-menu';
import { increaseChance } from '../../../game-data/chances';
import {
  chopper,
  collector,
  getWoodAxeRank,
  increaseWoodAxeRank,
  increaseSpeed,
} from '../../../game-data/worker-config';

import prices, { increment, count, config } from '../../../game-data/upgrade-prices';

export default function setup() {
  const inventory = this.game.state.states.Game.player.inventory;

  bindMenu('upgrades');

  const upgradeButtons = document.querySelectorAll('.upgrade');
  for (let i = 0; i < upgradeButtons.length; i++) {
    const itemName = upgradeButtons[i].id.replace('upgrade-', '');

    updatePrice(itemName, prices[itemName]);

    if (inventory.money >= upgradeButtons[i].dataset.price) {
      upgradeButtons[i].classList.remove('disabled');
    } else {
      upgradeButtons[i].classList.add('disabled');
    }
  }

  buy = buy.bind(inventory);

  upgrade('wood-axe', () => {
    if (!buy('wood-axe')) return;

    inventory.increment('wood-axe', 'rank');
  });
  upgrade('sell-price', () => {
    if (!buy('sell-price')) return;

    inventory.sellMultiplier += 0.1;
  });
  upgrade('chopper-wood-axe', () => {
    if (!buy('chopper-wood-axe')) return;

    const choppers = this.game.state.states.Game.groups.character.children.filter((o) => o.objectType === 'chopper');

    // update existing choppers' wood axes
    for (let i = 0; i < choppers.length; i++) {
      choppers[i].inventory.items['wood-axe'].rank++;
    }
    // update global chopper wood axe rank
    increaseWoodAxeRank();
  });
  upgrade('chopper-speed', () => {
    if (!buy('chopper-speed')) return;

    const choppers = this.game.state.states.Game.groups.character.children.filter((o) => o.objectType === 'chopper');

    for (let i = 0; i < choppers.length; i++) {
      choppers[i].speed -= 0.1;
      choppers[i].timer.events[0].delay -= 100;
    }
    increaseSpeed('chopper');
  });
  upgrade('collector-speed', () => {
    if (!buy('collector-speed')) return;

    const collectors = this.game.state.states.Game.groups.character.children.filter((o) => o.objectType === 'collector');

    for (let i = 0; i < collectors.length; i++) {
      collectors[i].speed -= 0.1;
      collectors[i].timer.events[0].delay -= 100;
    }
    increaseSpeed('collector');
  });

  upgrade('pine-cone', () => {
    if (!buy('pine-cone')) return;

    increaseChance('seedDrop');
  });

  upgrade('tree-grow', (event) => {
    if (!buy('tree-grow')) return;

    increaseChance('treeGrow');
  });
}

function buy(name) {
  const price = prices[name];

  if (this.money < price || count(name) >= config[name].max) return false;

  // NOTE update price before taking money so that the button will be properly
    // disabled if the player has no money for next price
  updatePrice(name, increment(name));
  this.money -= price;

  return true;
}

function upgrade(name, handler) {
  document.getElementById('upgrade-' + name).addEventListener('click', handler);
}

function updatePrice(name, newPrice) {
  if (Math.round(newPrice) !== newPrice) newPrice = newPrice.toFixed(2);

  const button = document.getElementById('upgrade-' + name);

  button.dataset.price = newPrice;

  button.textContent = button.textContent.replace(/\(\$.+\)/, '($' + newPrice + ')');
}

@@ -723,24 +723,63 @@
 ataset.price
+ %7C%7C count(itemName) %3E= config%5BitemName%5D
 ) %7B%0A      up
@@ -3050,16 +3050,199 @@
 rice) %7B%0A
+  const button = document.getElementById('upgrade-' + name);%0A%0A  if (count(name) %3E= config%5Bname%5D) %7B%0A    // item is maxed out; disable button%0A    button.classList.add('disabled');%0A  %7D%0A%0A
   if (Ma
@@ -3311,70 +3311,8 @@
 );%0A%0A
-  const button = document.getElementById('upgrade-' + name);%0A%0A
   bu

Disable maxed upgrade buttons