#==============================================================================
# ** Window_ShopBuy <--modified by Orochii, but you don't need to give credit.
# You can use/modify/share it freely (at least as freely as an original Enterbrain script ahaha).
#------------------------------------------------------------------------------
#  This window displays buyable goods on the shop screen.
#==============================================================================

class Window_ShopBuy < Window_Selectable
  #--------------------------------------------------------------------------
  # * Object Initialization
  #	x : window X coordinate
  #	y : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y)
	super(x, y, 240, 304)
	@shop_goods = $game_temp.shop_goods
	refresh
	self.index = 0
  end
  #--------------------------------------------------------------------------
  # * Draw Item
  #	index : item number
  #--------------------------------------------------------------------------
  def draw_item(index)
	item = @data[index]
	number = $game_party.item_number(item)
	enabled = (item.price <= $game_party.gold 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, item.price, 2)
  end
end

#==============================================================================
# ** Window_ShopNumber
#------------------------------------------------------------------------------
#  This window is for inputting quantity of items to buy or sell on the
# shop screen.
#==============================================================================

class Window_ShopNumber < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #	x : window X coordinate
  #	y : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y)
	super(x, y, 240, 304)
	@item = nil
	@max = 1
	@price = 0
	@number = 1
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
	y = 96
	self.contents.clear
	draw_item_name(@item, 0, y)
	self.contents.font.color = normal_color
	self.contents.draw_text(148, y, 20, WLH, "×")
	self.contents.draw_text(186, y, 20, WLH, @number, 2)
	self.cursor_rect.set(180, y, 28, WLH)
	draw_currency_value(@price * @number, 4, y + WLH * 2, 200)
  end
end

#==============================================================================
# ** Window_ShopStatus
#------------------------------------------------------------------------------
#  This window displays number of items in possession and the actor's equipment
# on the shop screen.
#==============================================================================

class Window_ShopStatus < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #	x : window X coordinate
  #	y : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y)
	super(x, y, 240, 304)
	@item = nil
	refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
	self.contents.clear
	if @item != nil
  	number = $game_party.item_number(@item)
  	self.contents.font.color = system_color
  	self.contents.draw_text(4, 0, 200, WLH, Vocab::Possession)
  	self.contents.font.color = normal_color
  	self.contents.draw_text(4, 0, 200, WLH, number, 2)
  	for actor in $game_party.members
    	x = 4
    	y = WLH * (2 + actor.index * 2)
    	draw_actor_parameter_change(actor, x, y)
  	end
	end
  end
  #--------------------------------------------------------------------------
  # * Draw Actor's Current Equipment and Parameters
  #	actor : actor
  #	x	: draw spot x-coordinate
  #	y	: draw spot y-coordinate
  #--------------------------------------------------------------------------
  def draw_actor_parameter_change(actor, x, y)
	return if @item.is_a?(RPG::Item)
	enabled = actor.equippable?(@item)
	self.contents.font.color = normal_color
	self.contents.font.color.alpha = enabled ? 255 : 128
	self.contents.draw_text(x, y, 200, WLH, actor.name)
	if @item.is_a?(RPG::Weapon)
  	item1 = weaker_weapon(actor)
	elsif actor.two_swords_style and @item.kind == 0
  	item1 = nil
	else
  	item1 = actor.equips[1 + @item.kind]
	end
	if enabled
  	#if @item.is_a?(RPG::Weapon)
    	val1 = item1 == nil ? 0 : item1.atk
    	val2 = @item == nil ? 0 : @item.atk
    	change_atk = val2 - val1
  	#else
    	val1 = item1 == nil ? 0 : item1.def
    	val2 = @item == nil ? 0 : @item.def
    	change_def = val2 - val1
  	#end
    	val1 = item1 == nil ? 0 : item1.spi
    	val2 = @item == nil ? 0 : @item.spi
    	change_int = val2 - val1
  	 
    	val1 = item1 == nil ? 0 : item1.agi
    	val2 = @item == nil ? 0 : @item.agi
    	change_agi = val2 - val1
  	text = change_atk.to_s+" / " + change_def.to_s+" / " + change_int.to_s+" / " + change_agi.to_s
  	self.contents.draw_text(x, y, 200, WLH, text, 2) #sprintf("%+d", change)
	end
	draw_item_name(item1, x, y + WLH, enabled)
  end
end


#==============================================================================
# ** Scene_Shop
#------------------------------------------------------------------------------
#  This class performs shop screen processing.
#==============================================================================

class Scene_Shop < Scene_Base
  #--------------------------------------------------------------------------
  # * Start processing
  #--------------------------------------------------------------------------
  def start
	super
	create_menu_background
	create_command_window
	@help_window = Window_Help.new
	@gold_window = Window_Gold.new(384, 56)
	#@dummy_window = Window_Base.new(0, 112, 544, 304)
	@buy_window = Window_ShopBuy.new(304, 112)
	@buy_window.active = false
	@buy_window.visible = false
	@buy_window.help_window = @help_window
	@sell_window = Window_ShopSell.new(0, 112, 544, 304)
	@sell_window.active = false
	@sell_window.visible = false
	@sell_window.help_window = @help_window
	@number_window = Window_ShopNumber.new(304, 112)
	@number_window.active = false
	@number_window.visible = false
	@status_window = Window_ShopStatus.new(0, 112)
	@status_window.visible = false
  end
  #--------------------------------------------------------------------------
  # * Termination Processing
  #--------------------------------------------------------------------------
  def terminate
	super
	dispose_menu_background
	dispose_command_window
	@help_window.dispose
	@gold_window.dispose
	#@dummy_window.dispose
	@buy_window.dispose
	@sell_window.dispose
	@number_window.dispose
	@status_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
	super
	update_menu_background
	@help_window.update
	@command_window.update
	@gold_window.update
	#@dummy_window.update
	@buy_window.update
	@sell_window.update
	@number_window.update
	@status_window.update
	if @command_window.active
  	update_command_selection
	elsif @buy_window.active
  	update_buy_selection
	elsif @sell_window.active
  	update_sell_selection
	elsif @number_window.active
  	update_number_input
	end
  end
  #--------------------------------------------------------------------------
  # * Create Command Window
  #--------------------------------------------------------------------------
  def create_command_window
	s1 = Vocab::ShopBuy
	s2 = Vocab::ShopSell
	s3 = Vocab::ShopCancel
	@command_window = Window_Command.new(304, [s1, s2, s3], 3)
	@command_window.y = 56
	if $game_temp.shop_purchase_only
  	@command_window.draw_item(1, false)
	end
  end
  #--------------------------------------------------------------------------
  # * Dispose of Command Window
  #--------------------------------------------------------------------------
  def dispose_command_window
	@command_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Update Command Selection
  #--------------------------------------------------------------------------
  def update_command_selection
	if Input.trigger?(Input::B)
  	Sound.play_cancel
  	$scene = Scene_Map.new
	elsif Input.trigger?(Input::C)
  	case @command_window.index
  	when 0  # buy
    	Sound.play_decision
    	@command_window.active = false
    	#@dummy_window.visible = false
    	@buy_window.active = true
    	@buy_window.visible = true
    	@buy_window.refresh
    	@status_window.visible = true
  	when 1  # sell
    	if $game_temp.shop_purchase_only
      	Sound.play_buzzer
    	else
      	Sound.play_decision
      	@command_window.active = false
      	#@dummy_window.visible = false
      	@sell_window.active = true
      	@sell_window.visible = true
      	@sell_window.refresh
    	end
  	when 2  # Quit
    	Sound.play_decision
    	$scene = Scene_Map.new
  	end
	end
  end
  #--------------------------------------------------------------------------
  # * Update Buy Item Selection
  #--------------------------------------------------------------------------
  def update_buy_selection
	@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
  	number = $game_party.item_number(@item)
  	if @item == nil or @item.price > $game_party.gold or number == 99
    	Sound.play_buzzer
  	else
    	Sound.play_decision
    	max = @item.price == 0 ? 99 : $game_party.gold / @item.price
    	max = [max, 99 - number].min
    	@buy_window.active = false
    	@buy_window.visible = false
    	@number_window.set(@item, max, @item.price)
    	@number_window.active = true
    	@number_window.visible = true
  	end
	end
  end
  #--------------------------------------------------------------------------
  # * Update Sell Item Selection
  #--------------------------------------------------------------------------
  def update_sell_selection
	if Input.trigger?(Input::B)
  	Sound.play_cancel
  	@command_window.active = true
  	#@dummy_window.visible = true
  	@sell_window.active = false
  	@sell_window.visible = false
  	@status_window.item = nil
  	@help_window.set_text("")
	elsif Input.trigger?(Input::C)
  	@item = @sell_window.item
  	@status_window.item = @item
  	if @item == nil or @item.price == 0
    	Sound.play_buzzer
  	else
    	Sound.play_decision
    	max = $game_party.item_number(@item)
    	@sell_window.active = false
    	@sell_window.visible = false
    	@number_window.set(@item, max, @item.price / 2)
    	@number_window.active = true
    	@number_window.visible = true
    	@status_window.visible = true
  	end
	end
  end
  #--------------------------------------------------------------------------
  # * Update Number Input
  #--------------------------------------------------------------------------
  def update_number_input
	if Input.trigger?(Input::B)
  	cancel_number_input
	elsif Input.trigger?(Input::C)
  	decide_number_input
	end
  end
  #--------------------------------------------------------------------------
  # * Cancel Number Input
  #--------------------------------------------------------------------------
  def cancel_number_input
	Sound.play_cancel
	@number_window.active = false
	@number_window.visible = false
	case @command_window.index
	when 0  # Buy
  	@buy_window.active = true
  	@buy_window.visible = true
	when 1  # Sell
  	@sell_window.active = true
  	@sell_window.visible = true
  	@status_window.visible = false
	end
  end
  #--------------------------------------------------------------------------
  # * Confirm Number Input
  #--------------------------------------------------------------------------
  def decide_number_input
	Sound.play_shop
	@number_window.active = false
	@number_window.visible = false
	case @command_window.index
	when 0  # Buy
  	$game_party.lose_gold(@number_window.number * @item.price)
  	$game_party.gain_item(@item, @number_window.number)
  	@gold_window.refresh
  	@buy_window.refresh
  	@status_window.refresh
  	@buy_window.active = true
  	@buy_window.visible = true
	when 1  # sell
  	$game_party.gain_gold(@number_window.number * (@item.price / 2))
  	$game_party.lose_item(@item, @number_window.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