
-- Meta class
VinayDebugInfo = {resTable = {}, resTableCustom = {}, barns = {}, warehouses = {}, taverns = {}, barnCnt = 0, warehouseCnt = 0, tavernCnt = 0}

local RES_TYPES = {
  "logs",
  "stone",
  "ore",
  "clay",
  "hay",
  "firewood",
  "coal",
  "fish",
  "meat",
  "milk",
  "eggs",
  "fruits",
  "vegetables",
  "bread",
  "fish_pie",
  "meat_pie",
  "fruit_pie",
  "vegetable_pie",
  "grain",
  "flour",
  "alcohol",
  "oat",
  "tool",
  "improve_tool",
  "dress",
  "improve_dress",
  "herbs",
  "skin",
  "flax",
  "wool",
  "pig_iron",
  "honey",
  "honey_pie"
}

local WAREHOUSE_RES_IDS = {
  "logs",
  "stone",
  "ore",
  "clay",
  "hay",
  "coal",
  "pig_iron"
}

local BARN_RES_IDS = {
  "firewood",
  "fish",
  "meat",
  "milk",
  "eggs",
  "fruits",
  "vegetables",
  "bread",
  "fish_pie",
  "meat_pie",
  "fruit_pie",
  "vegetable_pie",
  "grain",
  "flour",
  "oat",
  "tool",
  "improve_tool",
  "dress",
  "improve_dress",
  "herbs",
  "skin",
  "flax",
  "wool",
  "honey",
  "honey_pie"
}

local TAVERN_RES_IDS = {
  "alcohol"
}

local FILENAMES = {
  "barnRes.txt",
  "warehouseRes.txt",
  "tavernRes.txt",
  "gameRes.txt"
}

local BUILDINGS = {
  "Barn",
  "WareHouse",
  "Tavern"
}

local BARN = 1
local WAREHOUSE = 2
local TAVERN = 3
local GAME_RES = 4

-- Local functions


-- Derived class method new

function VinayDebugInfo:new (o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  self.resTable = {}
  self.resTableCustom = {}

  local num = table.getn(RES_TYPES)
  --spam("num_res = ", num)
  for i = 1, num, 1 do
    self.resTableCustom[i] = {name = string.format("%s",RES_TYPES[i]), value = 0x0}
  end

  self.resTableCustom[num+1] = {name = string.format("%s","VINAY_RES_ARRAY"), value = 0xCAFECAFE}
  --spam("Custom table created with entries = ", table.getn(self.resTableCustom))

  self.barns = {}
  self.warehouses = {}
  self.taverns = {}
  self.barnCnt = 0
  self.warehouseCnt = 0
  self.tavernCnt = 0
  return o
end

-- Derived class method other
function VinayDebugInfo:getValueForRes(resType)
  local val = 0
  for key,value in pairs(self.resTable) do
    if(key == resType) then
      val = value
      break
    end
  end
  return val
end

function VinayDebugInfo:fillCustomTable ()
  local num = table.getn(RES_TYPES)
  for i = 1, num, 1 do
    self.resTableCustom[i].value = self:getValueForRes(RES_TYPES[i])
  end
end

function VinayDebugInfo:fillBarnInfo(bld)
  self.barnCnt = self.barnCnt + 1
  --spam("Filling Barn[",self.barnCnt,"]")
  
  local num = table.getn(BARN_RES_IDS)
  local data = nil
  data = {}
	for i = 1, num, 1 do
    local count = bld:getResourceStored(BARN_RES_IDS[i])
    data[i] = {res = BARN_RES_IDS[i], val = count}
  end
  self.barns[self.barnCnt] = nil
  self.barns[self.barnCnt] = data
end

function VinayDebugInfo:fillWarehouseInfo(bld)
  self.warehouseCnt = self.warehouseCnt + 1
  --spam("Filling Warehouse[",self.warehouseCnt,"]")

  local num = table.getn(WAREHOUSE_RES_IDS)
  local data = nil
  data = {}
	for i = 1, num, 1 do
    local count = bld:getResourceStored(WAREHOUSE_RES_IDS[i])
    data[i] = {res = WAREHOUSE_RES_IDS[i], val = count} 
  end
  self.warehouses[self.warehouseCnt] = nil
  self.warehouses[self.warehouseCnt] = data
end

function VinayDebugInfo:fillTavernInfo(bld)
  self.tavernCnt = self.tavernCnt + 1
  --spam("Filling Tavern[",self.tavernCnt,"]")

  local num = table.getn(TAVERN_RES_IDS)
  local data = nil
  data = {}
	for i = 1, num, 1 do
    local count = bld:getResourceStored(TAVERN_RES_IDS[i])
    data[i] = {res = TAVERN_RES_IDS[i], val = count}
  end
  self.taverns[self.tavernCnt] = nil
  self.taverns[self.tavernCnt] = data
end

function VinayDebugInfo:dumpBuildingInfoToFile(bldType, filename)
  local arr = nil
  local index = nil
  if(bldType == BARN) then
    arr = self.barns
  elseif (bldType == WAREHOUSE) then
    arr = self.warehouses
  elseif (bldType == TAVERN) then
    arr = self.taverns
  else
    spam("Wrong Building type to dump in file")
  end

  local oFile = io.open(filename, "w")
  local num = table.getn(arr)
  --spam("Building type: ",BUILDINGS[bldType] ," Count = ", num, " File = ", filename)
  for i = 1, num, 1 do
    --spam("Writing building info in ", filename, " for index: ", i)
    local data = arr[i]
    local entries = table.getn(data)
    --spam("This building has ", entries, " resources")
    for j = 1, entries, 1 do
      oFile:write(string.format("%s,%d\n",data[j].res, data[j].val))
    end
  end
  
  oFile:close()
end

function VinayDebugInfo:updateValueInResTable (key , value)
  if(self.resTable[key] == nil) then
    self.resTable[key] = value
  else
    self.resTable[key] = self.resTable[key] + value
  end
end

function VinayDebugInfo:updateVillageResurces ()
  self.warehouseCnt = 0
  self.barnCnt = 0
  self.tavernCnt = 0
  self.resTable = {}

	local buildings = getBuildings()
	local buildingsCount = table.getn(buildings)
	for i = 1, buildingsCount, 1 do
		local bld = buildings[i]
		local bldState = bld.state
		if bldState == WORKING or bldState == DESTROY then
			local bldName = bld.name
      
			if bldName == "Barn" or bldName == "Warehouse" then
        --if bldState == WORKING then 
          --spam(bldName.." is ".."WORKING("..bldState..")")
        --else
          --spam(bldName.." is ".."DESTROYED("..bldState..")")
        --end

        if bldName == "Barn" then
          self:fillBarnInfo(bld)
        else
          self:fillWarehouseInfo(bld)
        end

				local resources = getBuildingResources(bld)			
				for key,value in pairs(resources) do
          self:updateValueInResTable(key, value)
				end
        
			elseif bldName == "Tavern" then
				local count = bld:getResourceStored("alcohol")
        self:updateValueInResTable("alcohol", count)
        self:fillTavernInfo(bld)
			end
      
		end
	end
  
  self:fillCustomTable()
end

function VinayDebugInfo:printResurces ()
  spam("Entries in resTable:")
  for key,value in pairs(self.resTable) do
    spam(key, " = ", value)
  end
  
  local num = table.getn(self.resTableCustom)
  spam(self.resTable, "Entries in resTableCustom = ", table.getn(self.resTableCustom))
  for i = 1, num-1, 1 do
    spam(self.resTableCustom[i].name," = ", self.resTableCustom[i].value) 
  end
end

function VinayDebugInfo:dumpToFile()
  local oFile = io.open(FILENAMES[GAME_RES], "w")
  local num = table.getn(self.resTableCustom)
  for i = 1, num-1, 1 do
    oFile:write(string.format("%s,%d\n",self.resTableCustom[i].name, self.resTableCustom[i].value))
  end
  
  oFile:close()
end

function VinayDebugInfo:all()
  --spam("")
  spam("")
  --spam("----- NEW CALL-----")
  self:updateVillageResurces()
  --self:printResurces()
  self:dumpToFile()
  self:dumpBuildingInfoToFile(BARN, FILENAMES[BARN])
  self:dumpBuildingInfoToFile(WAREHOUSE, FILENAMES[WAREHOUSE])
  self:dumpBuildingInfoToFile(TAVERN, FILENAMES[TAVERN])
  return "Resource info dumped to files"
end