local this = {}
local homeTechnologyTypeTpl = L_GameTpl:getHomeTechnologyTypeTpl()
local homeTechnologyLevelTpl = L_GameTpl:getHomeTechnologyLevelTpl()

function this:req_homeTechnologyTypeInfo(type)
  local data = {type = type}
  L_Net:sendMessage(MsgGenCode.CSProtoHomeTechnologyTypeInfo, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self:rsp_syncTechnologyTypeInfo(rspData)
    end
  end)
end

function this:req_homeTechnologyLevelUp(type, subType, callback)
  local data = {type = type, subType = subType}
  L_Net:sendMessage(MsgGenCode.CSProtoHomeTechnologyLevelUp, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      L_UI:open("pageScienceStudySuccess", {techTreeId = subType, callback = callback})
    end
  end)
end

function this:rsp_syncTechnologyTypeInfo(data)
  local info = data.info
  local syncType = data.sync_type
  local typeBase = info.base
  local tpl = homeTechnologyTypeTpl:getTplById(typeBase.type)
  if not self.data[typeBase.type] then
    return
  end
  local oldLevel = self.data[typeBase.type].level or 0
  local oldExp = self.data[typeBase.type].exp or 0
  local oldPoint = self.data[typeBase.type].point or 0
  local addPoint = (typeBase.point or 0) - oldPoint
  local technologyName = homeTechnologyTypeTpl:getTechnologyName(tpl)
  if syncType == 1 and typeBase.level and typeBase.level < homeTechnologyLevelTpl:getCategoryMaxLevelByCategoryType(typeBase.type) and typeBase.dailyExp and typeBase.dailyExp == homeTechnologyTypeTpl:getExpUpperLimit(tpl) then
    L_GameUtil.showCommonTip({
      txtContent = L_WordsTpl:getValue("home_technology_exp_reachlimit", {
        [0] = technologyName
      }),
      showNoMoreType = L_Const.ShowNoMoreType.today,
      showNoMoreKey = string.format(L_Const.ShowNoMoreKey.technologyExp, L_PlayerStore:getPlayerId(), typeBase.type),
      showNoMoreDefaultValue = true
    })
  end
  self.data[typeBase.type].type = typeBase.type
  self.data[typeBase.type].level = typeBase.level
  self.data[typeBase.type].exp = typeBase.exp or 0
  self.data[typeBase.type].totalExp = typeBase.totalExp or 0
  self.data[typeBase.type].point = typeBase.point or 0
  self.data[typeBase.type].totalPoint = typeBase.totalPoint or 0
  self.data[typeBase.type].dailyExp = typeBase.dailyExp or 0
  self.data[typeBase.type].dailyExpTime = typeBase.dailyExpTime
  local listTech = info.list
  if listTech then
    for ii, vv in pairs(listTech) do
      local tech = {}
      tech.subType = vv.subType
      tech.subLevel = vv.subLevel or 0
      tech.lastTime = vv.lastTime
      tech.maxSubLevel = vv.maxSubLevel or 0
      if not self.techData[tech.subType] then
        table.insert(self.data[typeBase.type].list, tech)
      else
        for i, v in pairs(self.data[typeBase.type].list) do
          if v.subType == vv.subType then
            v.subType = vv.subType
            v.subLevel = vv.subLevel or 0
            v.lastTime = vv.lastTime
            v.maxSubLevel = vv.maxSubLevel or 0
            break
          end
        end
      end
      self.techData[tech.subType] = tech
    end
  end
  if typeBase.level and oldLevel < typeBase.level or typeBase.exp and oldExp < typeBase.exp then
    L_FlyMsgManager:showHomeScienceExpTip({
      technologyType = typeBase.type,
      oldLevel = oldLevel,
      oldExp = oldExp,
      addPoint = addPoint
    })
  end
  if 0 < addPoint then
    self:showPointRewardTip(typeBase.type, addPoint)
  end
  self:call(self.event.refresh)
end

function this:rsp_syncTechnologyInfo(data)
  for i, v in pairs(data) do
    local typeBase = v.base
    self.data[typeBase.type] = {
      type = typeBase.type,
      level = typeBase.level,
      exp = typeBase.exp or 0,
      totalExp = typeBase.totalExp or 0,
      point = typeBase.point or 0,
      totalPoint = typeBase.totalPoint or 0,
      dailyExp = typeBase.dailyExp or 0,
      dailyExpTime = typeBase.dailyExpTime
    }
    self.data[typeBase.type].list = {}
    local listTech = v.list
    if listTech then
      for ii, vv in pairs(listTech) do
        local tech = {}
        tech.subType = vv.subType
        tech.subLevel = vv.subLevel or 0
        tech.lastTime = vv.lastTime
        tech.maxSubLevel = vv.maxSubLevel or 0
        table.insert(self.data[typeBase.type].list, tech)
        self.techData[tech.subType] = tech
      end
    end
  end
  self:call(self.event.refresh)
end

return this
