#===============================================================================
#  Sets the ItemManiac prices without a need for another PBS file
#===============================================================================
class ItemManiacAdapter < PokemonMartAdapter 
  def getPrice(item, multiplier, selling = false)
    if $game_temp.mart_prices && $game_temp.mart_prices[item]
      if selling
        return $game_temp.mart_prices[item][1] if $game_temp.mart_prices[item][1] >= 0
      else
        return $game_temp.mart_prices[item][0] if $game_temp.mart_prices[item][0] > 0
      end
    end
    return (pbGetPrice(item)*0.6).round # Set the Item Maniac price increase multiplier in the place of the 0.6. Standard selling price is 0.5.
  end
end
#===============================================================================
#  Removes the automatic /2 price shown through the Scene
#===============================================================================
class ItemManiac_Scene < PokemonMart_Scene
  def pbChooseNumber(helptext,item,maximum,multiplier)
    curnumber = 1
    ret = 0
    helpwindow = @sprites["helpwindow"]
    itemprice = @adapter.getPrice(item,multiplier,!@buying)
    pbDisplay(helptext, true)
    using(numwindow = Window_AdvancedTextPokemon.new("")) {   # Showing number of items
      qty = @adapter.getQuantity(item)
      using(inbagwindow = Window_AdvancedTextPokemon.new("")) {   # Showing quantity in bag
        pbPrepareWindow(numwindow)
        pbPrepareWindow(inbagwindow)
        numwindow.viewport = @viewport
        numwindow.width = 224
        numwindow.height = 64
        numwindow.baseColor = Color.new(88, 88, 80)
        numwindow.shadowColor = Color.new(168, 184, 184)
        inbagwindow.visible = @buying
        inbagwindow.viewport = @viewport
        inbagwindow.width = 190
        inbagwindow.height = 64
        inbagwindow.baseColor = Color.new(88, 88, 80)
        inbagwindow.shadowColor = Color.new(168, 184, 184)
        inbagwindow.text = _INTL("In Bag:<r>{1}  ", qty)
        numwindow.text = _INTL("x{1}<r>$ {2}", curnumber, (curnumber * itemprice).to_s_formatted)
        pbBottomRight(numwindow)
        numwindow.y -= helpwindow.height
        pbBottomLeft(inbagwindow)
        inbagwindow.y -= helpwindow.height
        loop do
          Graphics.update
          Input.update
          numwindow.update
          inbagwindow.update
          self.update
          if Input.repeat?(Input::LEFT)
            pbPlayCursorSE
            curnumber -= 10
            curnumber = 1 if curnumber < 1
            numwindow.text = _INTL("x{1}<r>$ {2}", curnumber, (curnumber * itemprice).to_s_formatted)
          elsif Input.repeat?(Input::RIGHT)
            pbPlayCursorSE
            curnumber += 10
            curnumber = maximum if curnumber > maximum
            numwindow.text = _INTL("x{1}<r>$ {2}", curnumber, (curnumber * itemprice).to_s_formatted)
          elsif Input.repeat?(Input::UP)
            pbPlayCursorSE
            curnumber += 1
            curnumber = 1 if curnumber > maximum
            numwindow.text = _INTL("x{1}<r>$ {2}", curnumber, (curnumber * itemprice).to_s_formatted)
          elsif Input.repeat?(Input::DOWN)
            pbPlayCursorSE
            curnumber -= 1
            curnumber = maximum if curnumber < 1
            numwindow.text = _INTL("x{1}<r>$ {2}", curnumber, (curnumber * itemprice).to_s_formatted)
          elsif Input.trigger?(Input::C)
            pbPlayDecisionSE
            ret = curnumber
            break
          elsif Input.trigger?(Input::B)
            pbPlayCancelSE
            ret = 0
            break
          end
        end
      }
    }
    helpwindow.visible = false
    return ret
  end
end
#===============================================================================
#  Screen specific for the ItemManiac
#===============================================================================
class ItemManiacScreen < PokemonMartScreen
  def initialize(scene,stock)
    @scene = scene
      @stock = stock
      @adapter = ItemManiacAdapter.new
  end
  def pbSellScreenManiac(multiplier)
    item=@scene.pbStartBuyOrSellScene(false,@stock,@adapter)
    loop do
      @scene.pbShowMoney
      item=@scene.pbChooseBuyItem
      break if item==0
      itemname=@adapter.getDisplayName(item)
      price=@adapter.getPrice(item,true)
      qty=@adapter.getQuantity(item)
      if qty==0
        pbMessage(_INTL("It looks like you don't have any {1}...",PBItems.getNamePlural(item)))
        next
      end
      @scene.pbShowMoney
      if qty>1
        qty=@scene.pbChooseNumber(
           _INTL("Alright. So how many {1} would you like to sell?",PBItems.getNamePlural(item)),item,qty,multiplier)
      end
      if qty==0
        @scene.pbHideMoney
        next
      end
      price*=qty
      if pbConfirm(_INTL("I can pay you ${1} for that. Sounds good?",price.to_s_formatted))
        @adapter.setMoney(@adapter.getMoney+price)
        qty.times do
          @adapter.removeItem(item)
        end
        pbDisplayPaused(_INTL("You handed over the {1} and received ${2}.",itemname,price.to_s_formatted)) { pbSEPlay("Mart buy item") }
        @scene.pbRefresh
      end
      @scene.pbHideMoney
    end
    @scene.pbEndSellScene
  end
end
#===============================================================================
#  Initializes the ItemManiac conversation
#===============================================================================
def pbItemManiac(stock,multiplier=5,initiate_speech="Fantastic! Don't worry, I will pay extra for any treasures you sell me!",end_speech="No problem! You know where to find me if you want to sell more treasures.")
  # This is the text when you initiate a conversation with the item maniac. Change it however you see fit.
  pbMessage(_INTL(initiate_speech))
  for i in 0...stock.length
    stock[i] = getID(PBItems,stock[i])
    if !stock[i] || stock[i]==0 ||
       (pbIsImportantItem?(stock[i]) && $PokemonBag.pbHasItem?(stock[i]))
      stock[i] = nil
    end
  end
  stock.compact!
  scene = ItemManiac_Scene.new
  screen = ItemManiacScreen.new(scene,stock)
  screen.pbSellScreenManiac(multiplier)
  # This is the text when you stop talking to the item maniac. Change it however you see fit.
  pbMessage(_INTL(end_speech))
  $game_temp.clear_mart_prices
end