local LWRefundPunishManager = BaseClass("LWRefundPunishManager")
local LWRefundGoldBrickTemplate = require("DataCenter.LWRefundPunish.LWRefundGoldBrickTemplate")
local Localization = CS.GameEntry.Localization
local punishStrMap = {
  [RefundPunishType.PAY_BAN] = "refund_window_limit_buypack",
  [RefundPunishType.CHAT_BAN] = "refund_window_limit_chat",
  [RefundPunishType.MARCH_BAN] = "refund_window_limit_march",
  [RefundPunishType.LOGIN_BAN] = "refund_window_limit_ban"
}

function LWRefundPunishManager:__init()
  self.refund2BanType = RefundPunishType.None
  self.refund2PunishTime = 0
  self.goldBrickMap = {}
  self.goldBrickMapPC = {}
end

function LWRefundPunishManager:__delete()
  self.refund2BanType = nil
  self.refund2PunishTime = nil
  self.goldBrickMap = nil
  self.goldBrickMapPC = nil
end

function LWRefundPunishManager:InitData(t)
  self.refund2BanType = t.refund2BanType
  self.refund2PunishTime = t.refund2PunishTime
end

function LWRefundPunishManager:InitGoldBrickMap()
  self.goldBrickMap = {}
  LocalController:instance():visitTable(TableName.REFUND_GOLDBRICK, function(id, lineData)
    if lineData ~= nil then
      local template = LWRefundGoldBrickTemplate.New()
      template:InitData(lineData)
      self.goldBrickMap[template.id] = template
    end
  end)
end

function LWRefundPunishManager:GetPunishType()
  return self.refund2BanType
end

function LWRefundPunishManager:GetCanPay(packageInfo)
  if self:CheckSwitchOn() and self.refund2BanType and self.refund2BanType >= RefundPunishType.PAY_BAN then
    local isRefundBrickPack = packageInfo:getType() == GiftPackType.RefundBrickPackage
    if not isRefundBrickPack then
      UIManager:GetInstance():OpenWindow(UIWindowNames.LWRefundPunish)
      return false
    end
  end
  return true
end

function LWRefundPunishManager:GetCanChat()
  if self:CheckSwitchOn() and self.refund2BanType and self.refund2BanType >= RefundPunishType.CHAT_BAN then
    UIManager:GetInstance():OpenWindow(UIWindowNames.LWRefundPunish)
    return false
  end
  return true
end

function LWRefundPunishManager:GetCanMarch()
  if self:CheckSwitchOn() and self.refund2BanType and self.refund2BanType >= RefundPunishType.MARCH_BAN then
    UIManager:GetInstance():OpenWindow(UIWindowNames.LWRefundPunish)
    return false
  end
  return true
end

function LWRefundPunishManager:GetCanLogIn()
  if self:CheckSwitchOn() and self.refund2BanType and self.refund2BanType >= RefundPunishType.LOGIN_BAN then
    return false
  end
  return true
end

function LWRefundPunishManager:GetPunishTime()
  return self.refund2PunishTime
end

function LWRefundPunishManager:GetGoldBrickPackage()
  if not self.goldBrickMap or table.length(self.goldBrickMap) == 0 then
    self:InitGoldBrickMap()
  end
  return self.goldBrickMap
end

function LWRefundPunishManager:GetBrickNumById(id, isPc)
  local num = 0
  local goldBrickMap = self:GetGoldBrickPackage()
  if isPc then
    goldBrickMap = self:GetGoldBrickPackagePC()
  end
  local template = goldBrickMap[tonumber(id)]
  if template then
    num = template.brickNum
  else
    Logger.LogError("goldBrickMap not contain, id: " .. id)
  end
  return num
end

function LWRefundPunishManager:GetCurPunishList()
  if self.refund2BanType == RefundPunishType.None then
    return {}
  end
  local curPunishList = {}
  for _, value in pairs(RefundPunishType) do
    if value > RefundPunishType.None and value <= self.refund2BanType then
      table.insert(curPunishList, value)
    end
  end
  table.sort(curPunishList)
  return curPunishList
end

function LWRefundPunishManager:GetNextPunish()
  local nextPunish = self.refund2BanType + 1
  if nextPunish > RefundPunishType.LOGIN_BAN then
    nextPunish = RefundPunishType.LOGIN_BAN
  end
  return nextPunish
end

function LWRefundPunishManager:GetPunishLocalizationString(punish)
  local id = punishStrMap[punish]
  if not id then
    Logger.LogError("punishStrMap not contain,punish==" .. punish)
    return ""
  end
  return Localization:GetString(id)
end

function LWRefundPunishManager:CheckSwitchOn()
  return LuaEntry.DataConfig:CheckSwitch("refund_client2")
end

function LWRefundPunishManager:InitGoldBrickMapPC()
  self.goldBrickMapPC = {}
  LocalController:instance():visitTable(TableName.REFUND_GOLDBRICK_PC, function(id, lineData)
    if lineData ~= nil then
      local template = LWRefundGoldBrickTemplate.New()
      template:InitData(lineData)
      self.goldBrickMapPC[template.id] = template
    end
  end)
end

function LWRefundPunishManager:GetGoldBrickPackagePC()
  if not self.goldBrickMapPC or table.length(self.goldBrickMapPC) == 0 then
    self:InitGoldBrickMapPC()
  end
  return self.goldBrickMapPC
end

function LWRefundPunishManager:GetGoldBrickDataByGoods(goodsId)
  local brickMapPC = self:GetGoldBrickPackagePC()
  for k, v in pairs(brickMapPC) do
    local template = v
    if not template then
      Logger.LogError("goldBrickMap template is nil, index: " .. k)
      return nil
    end
    if template.goodsId == goodsId then
      return template
    end
  end
  return nil
end

function LWRefundPunishManager:GetIsRefundBrick(packageInfo)
  if not packageInfo then
    Logger.LogError("packageInfo is nil")
    return false
  end
  return packageInfo:getType() == GiftPackType.RefundBrickPackage
end

return LWRefundPunishManager
