local this = class("interactSystem", L_EcsConst.system)
local _spawnerTpl = L_GameTpl:getWorldSpawnerTpl()
local _worldMapTpl = L_GameTpl:getWorldMapTpl()
local _interactTpl = L_GameTpl:getWorldInteractTpl()
local sConcat = string.concat
local subPath = sConcat(L_R.world.coreMgr, "interactive.commonCondition.")
local executers = {
  [12042] = require(sConcat(subPath, "cond_systemUnlock"))
}

function this:ctor(...)
  this.super.ctor(self, ...)
end

function this:setInteractUI(data)
  local interData = {}
  interData.sId = data.sId
  interData.interactId = data.interactId
  interData.entity = data.entity
  interData.isIn = data.isIn
  interData.desc = self:getBtnName(data)
  interData.icon = data.btnIcon
  interData.soundKey = data.soundKey
  interData.hide = data.hide
  interData.priority = data.priority
  interData.isNew = true
  local canShow = self:refreshExternalParam(interData)
  if not canShow then
    return
  end
  interData.isLack = self:getIsLack(data)
  
  function interData.btnClickCb()
    if data.clickCb then
      data.clickCb()
    end
  end
  
  interData.selectCb = data.selectCb
  L_InteractiveManager:setInteract(interData)
end

function this:refreshExternalParam(data)
  local tpl = _interactTpl:getTplById(data.interactId)
  if tpl ~= nil then
    data.cond = _interactTpl:getInteractCond(tpl)
    data.executers = executers
    for k, v in pairs(data.cond) do
      local executor = executers[v[1]]
      if executor ~= nil then
        local isLocked = executor:isLocked(v[2])
        data.isLocked = isLocked
        if isLocked then
          return executor:canButtonShow(v[2])
        end
        return true
      end
    end
  end
  return true
end

function this:getBtnName(data)
  local interactType = tonumber(data.interactType)
  local entity = data.entity
  local executer = L_InteractiveMediator:getExecuter(interactType)
  if executer ~= nil then
    local btnName = executer:getDisplayName(entity, data.overrideParam)
    if btnName ~= nil then
      return btnName
    end
  end
  if L_WordsTpl:getValue(data.btnName) ~= nil and L_WordsTpl:getValue(data.btnName) ~= string.empty then
    return L_WordsTpl:getValue(data.btnName)
  else
    return data.btnName
  end
end

function this:getIsLack(data)
  local interactType = tonumber(data.interactType)
  local entity = data.entity
  local executer = L_InteractiveMediator:getExecuter(interactType)
  if executer ~= nil and executer.getIsLack then
    local isLock = executer:getIsLack(entity, data.overrideParam)
    return isLock or false
  end
  return false
end

function this:executeInteractAction(cData)
  local entity = cData.entity
  local param = cData.overrideParam
  local executer = L_InteractiveMediator:getExecuter(cData.interactType)
  if executer ~= nil and executer:checkCondition(entity, param, cData) and not L_TaskManager:checkIsDisableInteractTransfer(cData.entity) then
    executer:executeInteract(entity, param, cData)
  end
end

function this:onDestroy()
end

function this:onUpdate()
end

return this
