#==============================================================================
#  ExCommand_PrizeShop Qå Щ` 
#------------------------------------------------------------------------------
# [åפ΄I] äơꥹȥåפ줿Ʒȡʤ
# äƥƥȤνQǤ褦ˤ륹ץزĤǤ
#==============================================================================

# QåיCܤЄ״Bʾå ID
# ηŤΥå ON ˤȡ[åפ΄I] gФHˡ
# ָƷKֽQå׻΄IФޤ ([ُΤ])
# Υåϡåפ΄IKˤrԄӵĤ OFF ˤʤޤ
EXCMD_PRZSHOP_SID = 7

# QåפΥޥZ
# 󤫤혤ˡ[ُ뤹] [ȴ] [] ˌꤷޤ
EXCMD_PRZSHOP_VOCAB = ["Q", "ȴ", ""]

# QƥO
# ָ΂򽻓QƥȤƒQޤ
#  IDgλZQ`Ȥ혤OƤ
# Ʒ΁  Q` ᤿ 1 Ϥ˜ʤνQ`Ȥˤʤޤ
# ) Q`Ȥ 100 Ȥ => 
# 1650G Ʒ 16 ǽQܡ100G ¤Ʒ 1 ǽQ
EXCMD_PRZSHOP_ITEM = [24, "ö", 5]

# ƷνQ`ָС
# Q`ȤָƤʤƷϡ񤫤Q㤷ԄӵĤOޤ
# [] ˡָ + [Q`] ޤƤСȤޤ
# ) Qƥ 20 ȽQܤƷ => *PRIZE_RATE[20]
EXCMD_PRZSHOP_SIGNATURE = "*PRIZE_RATE"

# åReŤָ ID
# ηŤΉ˥åReŤ뤷Ƥ [åפ΄I] gФ
# ƷνQ`ȤåפȤO줿ˉޤҊĿˤͬ
# åפǤ⡢ReŤʹ֤¤ǥ`ȤӤ뤳ȤܤǤ
EXCMD_PRZSHOP_VID = 7

# åפȤO
# ָ ( + [Q`] ) + [åRe:Q`] Ȥʽ
# :ǌˤåפȤνQ`ȤΥåȤ|Фäָޤ
#  1) å 1  20 å 2  15 å 3  25 
# QƥȽQܤƷ => *PRIZE_RATE[1:20|2:15|3:25]
#  2) å 2  10 å 3  15 ΥåפǤ
# 20 νQƥȽQܤƷ => *PRIZE_RATE[20][2:10|3:15]
# 
# λϡåפȤO  ƷȤO  񤫤ΓQ Ǥ

#------------------------------------------------------------------------------

class Game_Party
  #--------------------------------------------------------------------------
  #  Qƥȡ
  #--------------------------------------------------------------------------
  def prize_item_number
    return $game_variables[EXCMD_PRZSHOP_ITEM[0]]
  end
  #--------------------------------------------------------------------------
  #  Q`Ȥȡ
  #--------------------------------------------------------------------------
  def prize_rate(item)
    rate = 0
    shop_id = $game_variables[EXCMD_PRZSHOP_VID]
    sig = EXCMD_PRZSHOP_SIGNATURE
    reg = /#{Regexp.quote sig}(?:\[\d+\])?\[((\d+:\d+\|?)+)\]/
    note = item.note.clone
    note.gsub!(/\r\n/, "")
    if shop_id > 0 and note.scan(reg).to_a[0]
      for s in $~[1].split(/\s*\|\s*/)
        r = s.split(/\s*:\s*/)
        rate = r[1].to_i if r[0].to_i == shop_id
        break if rate > 0
      end
    end
    if rate == 0
      if note[/#{Regexp.quote sig}\[(\d+)\]/].to_a[0]
        rate = $1.to_i
      else
        rate = (item.price / EXCMD_PRZSHOP_ITEM[2]).round
      end
    end
    return rate > 0 ? rate : 1
  end
end

class Window_Base
  alias _excpshop_initialize initialize
  #--------------------------------------------------------------------------
  #  ֥ȳڻ (׷Ӷx)
  #     x      : ɥ X 
  #     y      : ɥ Y 
  #     width  : ɥη
  #     height : ɥθߤ
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    _excpshop_initialize(x, y, width, height)
    self.currency = Vocab::gold
  end
  #--------------------------------------------------------------------------
  #  gλO
  #--------------------------------------------------------------------------
  def currency=(currency)
    @currency = currency
  end
  #--------------------------------------------------------------------------
  #  gλ`ɤɤȡ
  #--------------------------------------------------------------------------
  def gold?
    return @currency == Vocab::gold
  end
  #--------------------------------------------------------------------------
  #  ͨ؛gλĤ軭 (ٶx)
  #     value :  (ֽʤ)
  #     x     : 軭 X 
  #     y     : 軭 Y 
  #     width : 
  #--------------------------------------------------------------------------
  def draw_currency_value(value, x, y, width)
    cx = contents.text_size(@currency).width
    self.contents.font.color = normal_color
    self.contents.draw_text(x, y, width - cx - 2, WLH, value, 2)
    self.contents.font.color = system_color
    self.contents.draw_text(x, y, width, WLH, @currency, 2)
  end
end

class Window_Gold
  alias _excpshop_refresh refresh
  #--------------------------------------------------------------------------
  #  եå (׷Ӷx)
  #--------------------------------------------------------------------------
  def refresh
    _excpshop_refresh
    if $game_switches[EXCMD_PRZSHOP_SID]
      self.contents.clear
      value = gold? ? $game_party.gold : $game_party.prize_item_number
      draw_currency_value(value, 4, 0, 120)
    end
  end
end

class Window_ShopBuy
  alias _excpshop_refresh refresh
  alias _excpshop_draw_item draw_item
  #--------------------------------------------------------------------------
  #  եå (׷Ӷx)
  #--------------------------------------------------------------------------
  def refresh
    unless $game_switches[EXCMD_PRZSHOP_SID]
      _excpshop_refresh
    else
      @data = []
      for goods_item in @shop_goods
        case goods_item[0]
        when 0
          item = $data_items[goods_item[1]]
        when 1
          item = $data_weapons[goods_item[1]]
        when 2
          item = $data_armors[goods_item[1]]
        end
        if item != nil
          @data.push(item)
        end
      end
      @item_max = @data.size
      create_contents
      for i in 0...@item_max
        draw_item(i)
      end
    end
  end
  #--------------------------------------------------------------------------
  #  Ŀ軭 (׷Ӷx)
  #     index : Ŀ
  #--------------------------------------------------------------------------
  def draw_item(index)
    unless $game_switches[EXCMD_PRZSHOP_SID]
      _excpshop_draw_item(index)
    else
      item = @data[index]
      number = $game_party.item_number(item)
      prize_number = $game_party.prize_item_number
      prize_rate = $game_party.prize_rate(item)
      enabled = (prize_rate <= prize_number and number < 99)
      rect = item_rect(index)
      self.contents.clear_rect(rect)
      draw_item_name(item, rect.x, rect.y, enabled)
      rect.width -= 4
      self.contents.draw_text(rect, prize_rate, 2)
    end
  end
end

class Scene_Shop
  alias _excpshop_start start
  alias _excpshop_terminate terminate
  alias _excpshop_create_command_window create_command_window
  alias _excpshop_update_command_selection update_command_selection
  alias _excpshop_update_buy_selection update_buy_selection
  alias _excpshop_update_sell_selection update_sell_selection
  alias _excpshop_decide_number_input decide_number_input
  #--------------------------------------------------------------------------
  #  Qåפɤж
  #--------------------------------------------------------------------------
  def prize?
    return $game_switches[EXCMD_PRZSHOP_SID]
  end
  #--------------------------------------------------------------------------
  #  _ʼI (׷Ӷx)
  #--------------------------------------------------------------------------
  def start
    _excpshop_start
    if prize?
      @gold_window.currency = EXCMD_PRZSHOP_ITEM[1]
      @number_window.currency = EXCMD_PRZSHOP_ITEM[1]
      @gold_window.refresh
    end
  end
  #--------------------------------------------------------------------------
  #  K˄I (׷Ӷx)
  #--------------------------------------------------------------------------
  def terminate
    _excpshop_terminate
    $game_switches[EXCMD_PRZSHOP_SID] = false
  end
  #--------------------------------------------------------------------------
  #  ޥɥɥ (׷Ӷx)
  #--------------------------------------------------------------------------
  def create_command_window
    unless prize?
      _excpshop_create_command_window
    else
      s1 = EXCMD_PRZSHOP_VOCAB[0]
      s2 = EXCMD_PRZSHOP_VOCAB[1]
      s3 = EXCMD_PRZSHOP_VOCAB[2]
      @command_window = Window_Command.new(384, [s1, s2, s3], 3)
      @command_window.y = 56
      if $game_temp.shop_purchase_only
        @command_window.draw_item(1, false)
      end
    end
  end
  #--------------------------------------------------------------------------
  #  ޥxkθ (׷Ӷx)
  #--------------------------------------------------------------------------
  def update_command_selection
    _excpshop_update_command_selection
    if Input.trigger?(Input::C) and prize?
      unless $game_temp.shop_purchase_only
        if @command_window.index == 1 # ȴ
          @gold_window.currency = Vocab::gold
          @number_window.currency = Vocab::gold
          @gold_window.refresh
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  #  ُ륢ƥxkθ (׷Ӷx)
  #--------------------------------------------------------------------------
  def update_buy_selection
    unless prize?
      _excpshop_update_buy_selection
    else
      @status_window.item = @buy_window.item
      if Input.trigger?(Input::B)
        Sound.play_cancel
        @command_window.active = true
        @dummy_window.visible = true
        @buy_window.active = false
        @buy_window.visible = false
        @status_window.visible = false
        @status_window.item = nil
        @help_window.set_text("")
        return
      end
      if Input.trigger?(Input::C)
        @item = @buy_window.item
        prize_number = $game_party.prize_item_number
        prize_rate = $game_party.prize_rate(@item)
        number = $game_party.item_number(@item)
        if @item == nil or prize_rate > prize_number or number == 99
          Sound.play_buzzer
        else
          Sound.play_decision
          max = prize_number / prize_rate
          max = [max, 99 - number].min
          @buy_window.active = false
          @buy_window.visible = false
          @number_window.set(@item, max, prize_rate)
          @number_window.active = true
          @number_window.visible = true
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  #  ȴƥxkθ (׷Ӷx)
  #--------------------------------------------------------------------------
  def update_sell_selection
    _excpshop_update_sell_selection
    if Input.trigger?(Input::B) and prize?
      @gold_window.currency = EXCMD_PRZSHOP_ITEM[1]
      @number_window.currency = EXCMD_PRZSHOP_ITEM[1]
      @gold_window.refresh
    end
  end
  #--------------------------------------------------------------------------
  #  ΛQ (׷Ӷx)
  #--------------------------------------------------------------------------
  def decide_number_input
    unless prize?
      _excpshop_decide_number_input
    else
      Sound.play_shop
      @number_window.active = false
      @number_window.visible = false
      prize_rate = $game_party.prize_rate(@item)
      number = @number_window.number
      case @command_window.index
      when 0  # ُ뤹 (Q)
        $game_variables[EXCMD_PRZSHOP_ITEM[0]] -= number * prize_rate
        $game_party.gain_item(@item, number)
        @gold_window.refresh
        @buy_window.refresh
        @status_window.refresh
        @buy_window.active = true
        @buy_window.visible = true
      when 1  # ȴ
        $game_party.gain_gold(number * (@item.price / 2))
        $game_party.lose_item(@item, number)
        @gold_window.refresh
        @sell_window.refresh
        @status_window.refresh
        @sell_window.active = true
        @sell_window.visible = true
        @status_window.visible = false
      end
    end
  end
end
