local this = {}
local _homeTechnologyTreeTpl = L_GameTpl:getHomeTechnologyTreeTpl()
local _homeTechnologyTpl = L_GameTpl:getHomeTechnologyTpl()

function this:init()
  this.super.init(self)
  self.data = {}
  self.techData = {}
end

function this:getTechTypeInfo(techType)
  return self.data[techType]
end

function this:getTechTypeSubCnt(techType, tabId)
  local num = 0
  for i, v in pairs(self.data[techType].list) do
    local technologyTreeId = v.subType
    local tplTechTree = _homeTechnologyTreeTpl:getTplById(technologyTreeId)
    local type = _homeTechnologyTreeTpl:getType(tplTechTree)
    if type == tabId then
      num = num + v.subLevel
    end
  end
  return num
end

function this:getAllTechnology()
  return self.data
end

function this:isTechnologyUnlock(techTreeId)
  local tech = self.techData[techTreeId]
  return tech ~= nil
end

function this:getTechnologyInfo(techTreeId)
  local tech = self.techData[techTreeId]
  return tech
end

function this:getCurTechnologyLevelById(techTreeId)
  local tech = self.techData[techTreeId]
  if tech then
    return tech.subLevel
  else
    return 0
  end
end

function this:getCurTechnologyMaxLevelById(techTreeId)
  local tech = self.techData[techTreeId]
  if tech then
    return tech.maxSubLevel
  else
    return 0
  end
end

function this:getAllCurTechnologyList()
  local res = {}
  for _, v in pairs(self.techData) do
    if v.subLevel > 0 then
      local homeTechnologyTplId = _homeTechnologyTpl:getHomeTechnologyTplIdByTechtreeIdAndTechLevel(v.subType, v.subLevel)
      table.insert(res, homeTechnologyTplId)
    end
  end
  return res
end

function this:getAllCurTechnologyListByType(type)
  local res = {}
  if self.data[type] then
    for _, v in pairs(self.data[type].list) do
      if v.subLevel > 0 then
        local homeTechnologyTplId = _homeTechnologyTpl:getHomeTechnologyTplIdByTechtreeIdAndTechLevel(v.subType, v.subLevel)
        table.insert(res, homeTechnologyTplId)
      end
    end
  end
  return res
end

function this:getTechnologyTypeRedList()
  local homeTechnologyTreeTpl = L_GameTpl:getHomeTechnologyTreeTpl()
  local res = {}
  for i, v in pairs(homeTechnologyTreeTpl.data) do
    local techType = homeTechnologyTreeTpl:getType(v)
    if not res[techType] and self:GetTechnologyRedByTreeId(i) then
      res[techType] = 1
    end
  end
  return res
end

function this:GetTechnologyRedByTreeId(treeId)
  local curTechLevel = L_TechnologyStore:getCurTechnologyLevelById(treeId)
  local homeTechnologyTreeTpl = _homeTechnologyTreeTpl:getTplById(treeId)
  local homeTechnologyTplId = _homeTechnologyTpl:getHomeTechnologyTplIdByTechtreeIdAndTechLevel(treeId, curTechLevel)
  local homeTechnologyTpl = _homeTechnologyTpl:getTplById(homeTechnologyTplId)
  local upgradeCondition = _homeTechnologyTpl:getUnlockCondi1(homeTechnologyTpl)
  local maxLevel = curTechLevel == 0 and 1 or L_TechnologyStore:getCurTechnologyMaxLevelById(treeId)
  local isSatisfy, _ = L_ConditionManager:isComplete(upgradeCondition)
  if isSatisfy and curTechLevel < maxLevel then
    local needPoint = _homeTechnologyTpl:getPoint(homeTechnologyTpl)
    local techType = _homeTechnologyTreeTpl:getType(homeTechnologyTreeTpl)
    local techTypeInfo = L_TechnologyStore:getTechTypeInfo(techType)
    local point = techTypeInfo and techTypeInfo.point or 0
    if needPoint <= point then
      return true
    end
  end
  return false
end

function this:showPointRewardTip(tplTechnologyType, addNum)
  local _homeTechnologyTypeTpl = L_GameTpl:getHomeTechnologyTypeTpl()
  local tpl = _homeTechnologyTypeTpl:getTplById(tplTechnologyType)
  local currencyID = _homeTechnologyTypeTpl:getTechnologyCurrencyId(tpl)
  L_FlyMsgManager:showItemServerRewards({
    {
      itemtype = L_Const.resType.currency,
      itemid = currencyID,
      itemnum = addNum
    }
  })
end

return this
