#==============================================================================
# ■ Scene_Shop
#------------------------------------------------------------------------------
# 　ショップ画面の処理を行うクラスです。
#==============================================================================

class Scene_Shop < Scene_MenuBase
  #--------------------------------------------------------------------------
  # ● 準備
  #--------------------------------------------------------------------------
  def prepare(goods, purchase_only)
    @goods = goods
    @purchase_only = purchase_only
    @phase = :normal
  end
  #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  def start
    super
    create_help_window
    create_sales_window
    create_gold_window
    create_command_window
    @wy = @command_window.y + @command_window.height
    @wh = Graphics.height - @wy
    @wy2 = @wy + @wh
    @help_window.y = @wy2
    create_number_window
    create_status_window
    create_buy_window
    create_category_window
    create_sell_window
  end
  
  
  def item_height
    (height - standard_padding * 2) / 4
  end  
  
  def current_shop_person
    $shop_people[$game_variables[$var_shop_people]]
  end  
  
  #--------------------------------------------------------------------------
  # ● 背景の作成
  #--------------------------------------------------------------------------
  def create_background
    @background_sprite = Sprite.new
    @background_sprite.bitmap = SceneManager.background_bitmap
  end
  #--------------------------------------------------------------------------
  # ● ゴールドウィンドウの作成
  #--------------------------------------------------------------------------
  def create_gold_window
    @gold_window = Window_Gold.new
    @gold_window.viewport = @viewport
    @gold_window.x = Graphics.width - @gold_window.width
    @gold_window.y = @sales_window.height
  end
  #--------------------------------------------------------------------------
  # ● 店員ウィンドウの作成
  #--------------------------------------------------------------------------
  def create_sales_window
    @sales_window = Window_Sales.new
    @sales_window.viewport = @viewport
    @sales_window.x = 0
    @sales_window.y = 0
    @sales_window.set_handler(:ok,     method(:return_scene))
    @sales_window.set_chara(current_shop_person)
    @sales_window.refresh
    @sales_window.deactivate    
    @help_window.hide
  end  
  
  
  #--------------------------------------------------------------------------
  # ● コマンドウィンドウの作成
  #--------------------------------------------------------------------------
  def create_command_window
    @command_window = Window_ShopCommand.new(@gold_window.x, @purchase_only)
    @command_window.viewport = @viewport
    @command_window.y = @sales_window.height
    @command_window.set_handler(:buy,    method(:command_buy))
    @command_window.set_handler(:sell,   method(:command_sell))
    @command_window.set_handler(:cancel, method(:cancel_shop ))
  end
   

  def cancel_shop 
    @sales_window.phase = :cancel_shop
    @sales_window.message_type = :cancel_shop
    @sales_window.refresh    
    @sales_window.activate
  end  
  #--------------------------------------------------------------------------
  # ● 個数入力ウィンドウの作成
  #--------------------------------------------------------------------------
  def create_number_window
    wy = @wy
    wh = @wh
    @number_window = Window_ShopNumber.new(0, wy, wh)
    @number_window.viewport = @viewport
    @number_window.hide
    @number_window.set_handler(:ok,     method(:on_number_ok))
    @number_window.set_handler(:cancel, method(:on_number_cancel))
  end
  #--------------------------------------------------------------------------
  # ● ステータスウィンドウの作成
  #--------------------------------------------------------------------------
  def create_status_window
    wx = @number_window.width
    wy = @wy
    ww = Graphics.width - wx
    wh = @wh
    @status_window = Window_ShopStatus.new(wx, wy, ww, wh)
    @status_window.viewport = @viewport
    @status_window.hide
  end
  #--------------------------------------------------------------------------
  # ● 購入ウィンドウの作成
  #--------------------------------------------------------------------------
  def create_buy_window
    wy = @wy
    wh = @wh
    @buy_window = Window_ShopBuy.new(0, wy, wh, @goods)
    @buy_window.viewport = @viewport
    @buy_window.help_window = @help_window
    @buy_window.sales_window = @sales_window
    @buy_window.status_window = @status_window
    @buy_window.hide
    @buy_window.set_handler(:ok,     method(:on_buy_ok))
    @buy_window.set_handler(:cancel, method(:on_buy_cancel))
  end
  #--------------------------------------------------------------------------
  # ● カテゴリウィンドウの作成
  #--------------------------------------------------------------------------
  def create_category_window
    @category_window = Window_ItemCategory.new
    @category_window.viewport = @viewport
    @category_window.help_window = @help_window
    @category_window.y = @wy
    @category_window.hide.deactivate
    @category_window.set_handler(:ok,     method(:on_category_ok))
    @category_window.set_handler(:cancel, method(:on_category_cancel))
  end
  #--------------------------------------------------------------------------
  # ● 売却ウィンドウの作成
  #--------------------------------------------------------------------------
  def create_sell_window
    wy = @category_window.y + @category_window.height
    wh = Graphics.height - wy
    @sell_window = Window_ShopSell.new(0, wy, Graphics.width, wh)
    @sell_window.viewport = @viewport
    @sell_window.help_window = @help_window
    @sell_window.hide
    @sell_window.set_handler(:ok,     method(:on_sell_ok))
    @sell_window.set_handler(:cancel, method(:on_sell_cancel))
    @category_window.item_window = @sell_window
  end

  #--------------------------------------------------------------------------
  # ● コマンド［購入する］
  #--------------------------------------------------------------------------
  def command_buy
    activate_buy_window
    @sales_window.phase = :select
    @sales_window.message_type = :select_buy
    @sales_window.refresh
    

  end
  #--------------------------------------------------------------------------
  # ● コマンド［売却する］
  #--------------------------------------------------------------------------
  def command_sell
    @category_window.show.activate
    @sell_window.show
    @sell_window.unselect
    @sell_window.refresh
    @sales_window.phase = :normal
    @sales_window.message_type = :select_sell
    @sales_window.refresh
    
  end

  #--------------------------------------------------------------------------
  # ● 購入［キャンセル］
  #--------------------------------------------------------------------------
  def on_buy_cancel
    @command_window.activate
    @buy_window.hide
    @status_window.hide
    @status_window.item = nil
    @help_window.clear
    
  end


  #--------------------------------------------------------------------------
  # ● カテゴリ［キャンセル］
  #--------------------------------------------------------------------------
  def on_category_cancel
    @command_window.activate
    @category_window.hide
    @sell_window.hide
  end

  #--------------------------------------------------------------------------
  # ● 売却［キャンセル］
  #--------------------------------------------------------------------------
  def on_sell_cancel
    @sell_window.unselect
    @category_window.activate
    @status_window.item = nil
    @help_window.clear
  end
  #--------------------------------------------------------------------------
  # ● 個数入力［決定］
  #--------------------------------------------------------------------------
  def on_number_ok
    Sound.play_shop
    case @command_window.current_symbol
    when :buy
      do_buy(@number_window.number)
    when :sell
      do_sell(@number_window.number)
    end
    end_number_input
    @gold_window.refresh
    @status_window.refresh
  end
#~   #--------------------------------------------------------------------------
#~   # ● カテゴリ［決定］
#~   #--------------------------------------------------------------------------
#~   def on_category_ok
#~     activate_sell_window
#~     @sell_window.select(0)
#~   end  
  #--------------------------------------------------------------------------
  # ● 個数入力［キャンセル］
  #--------------------------------------------------------------------------
  def on_number_cancel
    Sound.play_cancel
    end_number_input
  end
  #--------------------------------------------------------------------------
  # ● 購入の実行
  #--------------------------------------------------------------------------
  alias takoffdo_buy do_buy
  def do_buy(number)
    takoffdo_buy(number)
    @sales_window.message_type = :thanks_buy
    @sales_window.refresh    

  end
  #--------------------------------------------------------------------------
  # ● 売却の実行
  #--------------------------------------------------------------------------
  alias takoffdo_sell do_sell
  def do_sell(number)
    takoffdo_sell(number)
    @sales_window.message_type = :thanks_sell
    @sales_window.refresh    

  end
  #--------------------------------------------------------------------------
  # ● 個数入力の終了
  #--------------------------------------------------------------------------
  def end_number_input
    @number_window.hide
    case @command_window.current_symbol
    when :buy
      @sales_window.phase = :buyed
      activate_buy_window
    when :sell
      @sales_window.phase = :selled
      activate_sell_window
    end
  end
  #--------------------------------------------------------------------------
  # ● 最大購入可能個数の取得
  #--------------------------------------------------------------------------
  def max_buy
    max = $game_party.max_item_number(@item) - $game_party.item_number(@item)
    buying_price == 0 ? max : [max, money / buying_price].min
  end



end
