#　データ

module DarkHall
	
	
	
	
	class ItemModel < Model
		include GetText
		
	
		attr_accessor :name, :black_name, :price, :effect
		attr_accessor :product_type, :treasure_type, :treasure_rank
		attr_accessor :target_type, :description, :extra_captions
		attr_accessor :message_type
		bool_attr_accessor :town_usable, :camp_usable, :battle_usable
		bool_attr_accessor :instant, :selable, :legendary
		
		
		def init_params
			super
			@name = "[#{@id}]"
			@description = "[#{@id}]"
			@price = 0
			@selable = true
			@effect = nil
			@instant = true
			@camp_usable = false
			@battle_usable = false
			@town_usable = false
			@target_type = TG_NONE
			@product_type = PD_GOODS
			@treasure_type = TRT::ITEM
			@treasure_rank = nil
			@message_type = nil
			@extra_captions = []
			@legendary = false
		end
		
		def sell_price_rate
			0.5
		end
		
		def sell_price
			if @legendary then
				(@price * 0.9).to_i
			else
				(@price * sell_price_rate).to_i
			end
		end
	
		def usable_by?(member)
			return true
		end
		
		def item_type_caption
			(instant? ? _('消費アイテム') : _('特殊アイテム'))
		end
		
		def spec_caption_texts
			[]
		end
		
	end
	
	class PotionModel < ItemModel
		def init_params
			super
			@black_name = "？小瓶"
			@instant = true
			@town_usable = true
			@camp_usable = true
			@battle_usable = true
			@target_type = TG_MEMBER
			@extra_captions = []
			@description = "????"
			@message_type = :Bottle
			@treasure_type = TRT::POTION
		end
		
		def sell_price_rate
			0.2
		end
	end
	
	class BundleItemModel < ItemModel
		attr_accessor :name_id, :number_max, :unit
		def init_params
			super
			@number_max = 99
			@unit = "個"
	
		end

		def name
			n_(@name_id, 1)
		end

	end
	
	class ArrowModel < BundleItemModel
		attr_accessor :damage_rate
		def init_params
			super
			@unit = "本"
			@damage_rate = 1.0
		end
		
		def usable_by?(member)
			member.kind_of?(Archer)
		end

		def item_type_caption
			_('矢')
		end
	end
	
	class BoltModel < BundleItemModel
		def init_params
			super
			@unit = "本"
		end
		
		def usable_by?(member)
			!(member.kind_of?(Prayer))
		end

		def item_type_caption
			_('矢')
		end

	end
	

	class MagicStoneModel < ItemModel
		def sell_price_rate
			0.1
		end
		
		
		def init_params
			super
			@instant = true
			@town_usable = true
			@camp_usable = true
			@battle_usable = true
			@target_type = :none
			@extra_captions = []
			@description = "????"
		end
	end
	
	class SpellBookModel < ItemModel
		attr_accessor :spell_id
	
		def init_params
			super
			@price = 20
			@instant = false
			@spell_id = nil
		end

		def item_type_caption
			_('呪文書')
		end
	end
	
	class KeyModel < ItemModel
		def init_params
			super
			@price = 10
			@instant = false
		end

		def item_type_caption
			_('扉の鍵')
		end
	end
	
	
	
	
	

	class Item < StorableObject
		include InstanceFromModel
		bool_attr_reader :unidentified

		def initialize(data_id)
			super(data_id)
			@unidentified = false
		end
		
		def identify
			@unidentified = false
		end
		
		def unidentify
			@unidentified = true if self.black_name
		end
		
		def identified?
			!(unidentified?)
		end
		
		def price
			return data.price.to_i
		end
		
		def sell_price
			return (unidentified? ? 0 : data.sell_price.to_i)
		end

		
		def description
			return (unidentified? ? "(未識別)" : data.description)
		end

		
		def name
			(unidentified ? self.black_name : self.data.name)
		end
		
		def usable_by?(member)
			(unidentified? ? false : super)
		end

		def data
			DB.find_item(@data_id)
		end
		

		def caption_texts
			if identified? then
				[item_type_caption].concat(spec_caption_texts).concat(extra_captions)
			else
				[item_type_caption]
			end
		end


		
		def load_mpac_model(model)
			@data_id = model['data_id']
			@unidentified = model['unidentified']
		end
		
		def dump
			hash = super
			hash['data_id'] = @data_id
			hash['unidentified'] = @unidentified
			return hash
		end
		
		def self.create_from_mpac_model(hash)
			obj = new(hash['data_id'])
			obj.unidentify if hash['unidentified']
			return obj
		end
	end
	
	class BundleItem < Item
		attr_accessor :number
		
		def initialize(data_id, number = 1)
			super(data_id)
			@number = number
		end	
		
		def price
			return (data.price * @number).to_i
		end
		
		def sell_price
			return (data.sell_price * @number.to_f).to_i
		end

		
		def name
			name = n_(self.data.name_id, @number)
			caption = n_('%{number}%{unit}の%{name}', '%{number}%{unit}の%{name}', @number)
			return caption.evaluate(:unit => self.unit, :number => @number, :name => name)
			#return "#{@number}#{self.unit}の#{super}"
		end
		
		def dump
			hash = super
			hash['number'] = @number
			return hash
		end
		
		def self.create_from_mpac_model(hash)
			return new(hash['data_id'], hash['number'])
		end

	end
	

	

	
end


