import { CODE_MONKEY_UPGRADE, DEV_MONKEY_UPGRADE, APE_UPGRADE, HUMAN_UPGRADE } from "game/upgrades/monkey"

const getUpgradeTier = ( game, upgradeType ) => {
  const pressUpgrades = game.get( "upgrades" ).filter( upgrade => upgrade.get( "type" ) === upgradeType )
  let upgradeTier = 0
  if ( pressUpgrades.size > 0 ) {
    upgradeTier = pressUpgrades
    .sortBy( upgrade => upgrade.get( "tier" ) )
    .reverse()
    .first().get( "tier" )
  }
  return upgradeTier
}

export default [
  {
    name: "Code Monkey",
    description:
      "The lowliest type of monkey there is. Code monkey get up get coffee! Clicks one key every two seconds.",
    condition: game => game.get( "unlocked" ).size > 1,
    amountPerTick: ( index, { props: { pointsPerMonkeyPress, game } } ) =>
      ( pointsPerMonkeyPress *
        ( game.getIn( [ "monkeys", index ] ) || 0 )
      ) * ( 2 ** getUpgradeTier( game, CODE_MONKEY_UPGRADE ) ),
    interval: 2000,
    price: ( monkeyAmount ) => Math.round( 15 * ( 1.145 ** ( monkeyAmount ) ) ),
    preview: () => "You can almost unlock your first Code Monkey! You need at least 2 keys.",
    notifications: [],
  },
  {
    name: "Dev Monkey",
    description: "This monkey can take care of writing a component by himself! Clicks three keys every two seconds!",
    condition: game => game.get( "unlocked" ).size > 3,
    amountPerTick: ( index, { props: { game, pointsPerMonkeyPress } } ) =>
      pointsPerMonkeyPress * 3 * ( game.getIn( [ "monkeys", index ] ) || 0 ) * (
        ( 2 ** getUpgradeTier( game, DEV_MONKEY_UPGRADE ) )
      ),
    interval: 2000,
    price: ( monkeyAmount ) => Math.round( 150 * ( 1.15 ** ( monkeyAmount ) ) ),
    preview: () => "You need at least 4 keys to get one of these.",
    notifications: [],
  },
  {
    name: "Code Ape",
    description: "This monkey will smash some keys in hope of making quality code! Clicks five keys every second!",
    condition: game => game.get( "unlocked" ).size > 5,
    amountPerTick: ( index, { props: { game, pointsPerMonkeyPress } } ) =>
      pointsPerMonkeyPress * 5 * ( game.getIn( [ "monkeys", index ] ) || 0 ) * (
        ( 2 ** getUpgradeTier( game, APE_UPGRADE ) )
      ),
    interval: 1000,
    price: ( monkeyAmount ) => Math.round( 300 * ( 1.16 ** ( monkeyAmount ) ) ),
    preview: () => "6 keys to make this crazy monkey happy.",
    notifications: [],
  },
  {
    name: "Human",
    description: "This monkey believes it has free will and creative power! It will click 15 keys every second!",
    condition: game => game.get( "unlocked" ).size > 8,
    amountPerTick: ( index, { props: { game, pointsPerMonkeyPress } } ) =>
      pointsPerMonkeyPress * 15 * ( game.getIn( [ "monkeys", index ] ) || 0 ) * (
        ( 2 ** getUpgradeTier( game, HUMAN_UPGRADE ) )
      ),
    interval: 1000,
    price: ( monkeyAmount ) => Math.round( 1000 * ( 1.17 ** ( monkeyAmount ) ) ),
    preview: () => "9 keys should be enough to keep the human at the keyboard all day.",
    notifications: [],
  },
  {
    name: "Super Nerd",
    description: "The bully's boss, enjoying himself by making games! Clicks 50 keys twice a second",
    condition: game => game.get( "unlocked" ).size > 11,
    amountPerTick: ( index, { props: { game, pointsPerMonkeyPress } } ) =>
      pointsPerMonkeyPress * 50 * ( game.getIn( [ "monkeys", index ] ) || 0 ),
    interval: 500,
    price: ( monkeyAmount ) => Math.round( 50000 * ( 1.19 ** ( monkeyAmount ) ) ),
    preview: () => "Reach 12 keys to see who's boss",
    notifications: [],
  },
]

@@ -117,20 +117,16 @@
 tUpgrade
-Tier
  = ( gam
@@ -135,19 +135,19 @@
  upgrade
-Typ
+Cod
 e ) =%3E %7B
@@ -155,22 +155,17 @@
   const 
-pressU
+u
 pgrades 
@@ -245,11 +245,11 @@
 rade
-Typ
+Cod
 e )%0A
@@ -258,31 +258,37 @@
 let 
-upgradeTier = 0
+highestUpgrade = null
 %0A  if ( 
 pres
@@ -283,22 +283,17 @@
 %0A  if ( 
-pressU
+u
 pgrades.
@@ -313,28 +313,26 @@
     
-upgradeTier = pressU
+highestUpgrade = u
 pgra
@@ -414,47 +414,305 @@
 st()
-.get( %22tier%22 )%0A  %7D%0A  return upgradeT
+%0A  %7D%0A  return highestUpgrade %7C%7C new Map()%0A%7D%0A%0Aconst amountPerTick = ( index, pointsPerMonkeyPress, game, upgradeCode ) =%3E %7B%0A  const base = pointsPerMonkeyPress * ( game.getIn( %5B %22monkeys%22, index %5D ) %7C%7C 0 )%0A  const upgrade = getUpgrade( game, upgradeCode )%0A  const modifier = upgrade.get(%22modif
 ier
+%22)
 %0A%7D%0A%0A
@@ -1119,36 +1119,32 @@
  2 ** getUpgrade
-Tier
 ( game, CODE_MON
@@ -1157,16 +1157,30 @@
 GRADE ) 
+.get( %22tier%22 )
 ),%0A    i
@@ -1772,36 +1772,32 @@
  2 ** getUpgrade
-Tier
 ( game, DEV_MONK
@@ -1798,32 +1798,46 @@
 V_MONKEY_UPGRADE
+ ).get( %22tier%22
  ) )%0A      ),%0A  
@@ -2402,36 +2402,32 @@
  2 ** getUpgrade
-Tier
 ( game, APE_UPGR
@@ -2421,32 +2421,46 @@
 ame, APE_UPGRADE
+ ).get( %22tier%22
  ) )%0A      ),%0A  
@@ -3027,12 +3027,8 @@
 rade
-Tier
 ( ga
@@ -3044,16 +3044,30 @@
 _UPGRADE
+ ).get( %22tier%22
  ) )%0A   

Update monkeys amountPerTick calculation