local this = class("cellInteractive", G_UIModuleBase)
local _interactiveTpl = L_GameTpl:getWorldInteractivetypeTpl()
local normalBgIcon = "Interactive/tex_common_bg_jiaohu4"
local noItemBgIcon = "Interactive/tex_common_bg_jiaohu6"
local lockBgIcon = "Interactive/tex_common_bg_jiaohu5"
local lockBgColor = "#8B8B8B"
local normalTxtColor = "#4C5E87"
local noItemTxtColor = "#D9435F"
local lockTxtColor = "#2A2B31"

function this.bind()
  return {
    txt_key = L_WordsTpl:getValue("ui_cellMapInteractive"),
    img_icon = "",
    txt_content = "",
    active_light = false,
    ani_state = "",
    float_pressFill = 0,
    toggle_isPress = false,
    norm_icon = true,
    lock_icon = false,
    canvas_group = 1,
    go_reddot = false,
    go_select = false,
    bg_color = C_Color(1, 1, 1, 1),
    img_bg_icon = normalBgIcon,
    color_txt = C_Color(0.2980392, 0.3686275, 0.5294118, 1),
    colorSelect = C_Color(1, 1, 1, 1),
    go_mouse = false
  }
end

function this.methods()
  return {
    onUp = function(self)
      self:invokeClick()
    end,
    onDown = function(self)
      self:startPress()
    end
  }
end

function this:open(options)
  local txt = "" .. L_SettingStore:getSetData(L_Const.settingType.interaction) .. ""
  self.bind.txt_key = txt
  local interData = self.bind.interData
  self.bind.toggle_isPress = interData.press and true or false
  self.bind.norm_icon = true
  if interData.isNew then
    self.bind.txt_content = interData.desc
    self.bind.img_icon = interData.icon
    if interData.icon == "UI/Atlas/TaskType/tex_icon_mapnode_zhuxian.png" or interData.icon == "UI/Atlas/TaskType/tex_icon_mapnode_yindao.png" or interData.icon == "UI/Atlas/TaskType/tex_icon_mapnode_zhixian.png" then
      self.bindComponents.iconTrans.sizeDelta = C_Vector2(60, 60)
    end
    local isLocked = interData.isLocked or false
    self.bind.norm_icon = not isLocked
    self.bind.lock_icon = isLocked
    if isLocked then
      self.bind.canvas_group = 0.8
      local _, c = C_ColorUtility.TryParseHtmlString(lockBgColor)
      self.bind.bg_color = c
      self.bind.colorSelect = c
    else
      self.bind.canvas_group = 1
      self.bind.bg_color = C_Color(1, 1, 1, 1)
      self.bind.colorSelect = C_Color(1, 1, 1, 1)
    end
    local isLack = interData.isLack or false
    if isLack then
      local _, c = C_ColorUtility.TryParseHtmlString(noItemTxtColor)
      self.bind.color_txt = c
      self.bind.img_bg_icon = noItemBgIcon
    elseif isLocked then
      local _, c = C_ColorUtility.TryParseHtmlString(lockTxtColor)
      self.bind.color_txt = c
      self.bind.img_bg_icon = lockBgIcon
    else
      local _, c = C_ColorUtility.TryParseHtmlString(normalTxtColor)
      self.bind.color_txt = c
      self.bind.img_bg_icon = normalBgIcon
    end
    
    function self._callback()
      if interData.btnClickCb ~= nil then
        interData.btnClickCb(interData.interactData)
      end
    end
  else
    local interType = interData.interType
    if interType ~= nil then
      local tpl = _interactiveTpl:getTplById(interType)
      local icon = _interactiveTpl:getIcon(tpl)
      self.bind.img_icon = icon
    end
    if interData.buildType ~= nil then
      self.bind.img_icon = interData.icon
    end
    self.bind.txt_content = interData.name
    self._callback = interData.onClick
  end
  self.bind.go_reddot = interData.go_reddot
end

function this:startPress()
  self:killTween()
  local interData = self.bind.interData
  if interData.press then
    self.parent:setPressState(true)
    self.bind.float_pressFill = 0
    self._longPressTween = self.bindComponents.pressFill:DOFillAmount(1, 0.5)
    
    function self._longPressTween.onComplete()
      if interData.onClick then
        interData.onClick()
      end
      self.parent:endPress()
    end
  end
end

function this:doEndPress()
  self:killTween()
  L_TimerManager:newOrResetTimer(self, "endPress", function()
    self.bind.float_pressFill = 0
  end, 0.1)
end

function this:killTween()
  if self._longPressTween then
    self._longPressTween:Kill()
    self._longPressTween = nil
  end
  L_TimerManager:clearTimer(self)
end

function this:close()
  self:killTween()
end

function this:invokeClick()
  self.parent:endPress()
  local data = self.bind.interData
  if data.press then
    return false
  end
  local soundKey = data.soundKey
  if soundKey ~= nil and L_PlayerManager:getMainControlEntity() then
    local go = L_PlayerManager:getMainControlEntity():getGameObject()
    if go ~= nil then
      L_AudioUtil.playSound(soundKey, go)
    end
  end
  local cond = data.cond
  if cond ~= nil then
    for k, v in pairs(cond) do
      local executor = data.executers[v[1]]
      if executor ~= nil then
        local fit = executor:isCondFit(v[2])
        if not fit then
          return
        end
      elseif not L_ConditionManager:singleIsComplete(v) then
        local desc = L_ConditionManager:getTipDesc({v})
        L_FlyMsgManager:showNormalMsg(desc)
        return
      end
    end
  end
  if self._callback then
    self._callback()
  end
  return true
end

function this:refreshState()
  local isSelect = self.parent.selectedIndex == self.bind.index
  self.bind.ani_state = (isSelect or L_DeviceTpl:getIsMobile()) and "anim_interactive_select_init" or "anim_interactive_deselect_init"
  if L_DeviceTpl:getIsPc() then
    self.bind.go_select = isSelect
    self.bind.active_light = isSelect
  else
    self.bind.go_select = true
    self.bind.active_light = false
  end
  self.bind.go_mouse = isSelect and L_DeviceTpl:getIsPc() and #self.parent.modules.list_button > 1
  local interData = self.bind.interData
  if isSelect and interData ~= nil and interData.selectCb ~= nil then
    interData.selectCb()
  end
end

return this
