local this = class("cellProductAccessory", G_UIModuleBase)
local offsetLine = 25
local _productsTpl = L_GameTpl:getProductsTpl()
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local CellWidth = 351
local CellHeight = 146
local CellSpaceWidth = 80
local CellSpaceHeight = 37
local CellColOffset = 10
this.CellWidth = CellWidth
this.CellHeight = CellHeight
this.CellSpaceWidth = CellSpaceWidth
this.CellSpaceHeight = CellSpaceHeight
this.CellColOffset = CellColOffset

function this.bind()
  return {
    img_icon = "",
    txt_id = "",
    img_qualityBg = "",
    list_star = {},
    active_star = false,
    active_lock = false,
    active_bg = true,
    pos_selfRect = C_Vector2.zero,
    active_selectCurr = false,
    active_lineRowLock = false,
    active_lineRowUnLock = false,
    active_lineColLock = false,
    active_lineColUnLock = false,
    active_pointLockStar = false
  }
end

function this.methods()
  return {
    onClickCell = function(self)
      if self.bind.curProductId <= 0 then
        return
      end
      local serverData = L_HomeStore:getHomeBuildingByGuid(self.bind.guid)
      local tpl_building = _homeBuildingTpl:getTplById(serverData.build_id)
      local level = _homeBuildingTpl:getLevel(tpl_building)
      local productTpl = _productsTpl:getTplById(self.bind.curProductId)
      local isSatisfy, conditionRess = L_ConditionManager:isComplete(_productsTpl:getUnlockCondition(productTpl))
      local needLv = _productsTpl:unlockBuildingLevel(productTpl)
      if not isSatisfy then
        local tpl_pro = _productsTpl:getTplById(self.bind.curProductId)
        L_FlyMsgManager:showNormalMsg(L_ConditionManager:getTipDesc(_productsTpl:getUnlockCondition(tpl_pro)))
        return
      end
      if level < needLv then
        L_FlyMsgManager:showNormalMsgByKey("notice_cellProductAccessory", {
          [0] = needLv
        })
        return
      end
      local pos = L_Vector3.getPos(self.gameObject.transform)
      self.bind.callback(self.bind.curProductId, pos)
    end
  }
end

function this:open()
  local dataItem = self.bind
  self:setDynaPos(dataItem.curRow, dataItem.curCol)
  self:refreshInfo()
  self:refreshLine()
end

function this:setDynaPos(row, col)
  local posX = CellWidth / 2 + (col - 1) * (CellWidth + CellSpaceWidth) + 10
  local posY = -(row - 1) * (CellHeight + CellSpaceHeight)
  self.bind.pos_selfRect = C_Vector2(posX, posY)
end

function this:refreshLine()
  local curRow = self.bind.curRow
  local curCol = self.bind.curCol
  local lastRow = self.bind.lastRow
  local lastCol = self.bind.lastCol
  local curProductId = self.bind.curProductId
  local lastProductId = self.bind.lastProductId
  if curProductId <= 0 or lastProductId <= 0 then
    self.bind.active_star = false
    self.bind.active_pointLockStar = false
    self.bind.active_lineRowLock = false
    self.bind.active_lineRowUnLock = false
    self.bind.active_lineColLock = false
    self.bind.active_lineColUnLock = false
    self.bindComponents.rRect_star.anchoredPosition = C_Vector2(0, 0)
    return
  end
  local isUnLock = true
  local serverData = L_HomeStore:getHomeBuildingByGuid(self.bind.guid)
  local tpl_building = _homeBuildingTpl:getTplById(serverData.build_id)
  local level = _homeBuildingTpl:getLevel(tpl_building)
  local productTpl = _productsTpl:getTplById(self.bind.lastProductId)
  local needLv = _productsTpl:unlockBuildingLevel(productTpl)
  if level < needLv then
    isUnLock = false
  end
  if curRow == lastRow then
    self.bind.active_star = false
    self.bind.active_pointLockStar = false
    self.bind.active_lineRowLock = not isUnLock
    self.bind.active_lineRowUnLock = isUnLock
    self.bind.active_lineColLock = false
    self.bind.active_lineColUnLock = false
    local targetWidth = math.abs(curCol - lastCol) * (CellWidth + CellSpaceWidth) - CellWidth / 2
    if isUnLock then
      self.bindComponents.rRect_lineRowUnLock.sizeDelta = C_Vector2(targetWidth, 0)
    else
      self.bindComponents.rRect_lineRowLock.sizeDelta = C_Vector2(targetWidth, 0)
    end
  elseif curCol == lastCol then
    self.bind.active_star = false
    self.bind.active_pointLockStar = false
    self.bind.active_lineRowLock = false
    self.bind.active_lineRowUnLock = false
    self.bind.active_lineColLock = not isUnLock
    self.bind.active_lineColUnLock = isUnLock
    local targetHeight = math.abs(curRow - lastRow) * (CellHeight + CellSpaceHeight) - CellHeight / 2
    local targetPosY = 0 < curRow - lastRow and 0 or -targetHeight
    if isUnLock then
      self.bindComponents.rRect_lineColUnLock.sizeDelta = C_Vector2(0, targetHeight)
      self.bindComponents.rRect_lineColUnLock.anchoredPosition = C_Vector2(0, targetPosY)
    else
      self.bindComponents.rRect_lineColLock.sizeDelta = C_Vector2(0, targetHeight)
      self.bindComponents.rRect_lineColLock.anchoredPosition = C_Vector2(0, targetPosY)
    end
  else
    self.bind.active_star = isUnLock
    self.bind.active_pointLockStar = not isUnLock
    self.bind.active_lineRowLock = not isUnLock
    self.bind.active_lineRowUnLock = isUnLock
    self.bind.active_lineColLock = not isUnLock
    self.bind.active_lineColUnLock = isUnLock
    local targetWidth = math.abs(curCol - lastCol) * (CellWidth + CellSpaceWidth) - offsetLine
    local targetHeight = math.abs(curRow - lastRow) * (CellHeight + CellSpaceHeight) - CellHeight / 2 - offsetLine
    local targetPosY = 0 < curRow - lastRow and 0 or -targetHeight
    if isUnLock then
      self.bindComponents.rRect_lineRowUnLock.sizeDelta = C_Vector2(targetWidth, 0)
      self.bindComponents.rRect_lineColUnLock.sizeDelta = C_Vector2(0, targetHeight)
      self.bindComponents.rRect_lineColUnLock.anchoredPosition = C_Vector2(-(targetWidth + offsetLine), targetPosY + offsetLine)
      self.bindComponents.rRect_star.anchoredPosition = C_Vector2(-(targetWidth + offsetLine), 0)
    else
      self.bindComponents.rRect_lineRowLock.sizeDelta = C_Vector2(targetWidth, 0)
      self.bindComponents.rRect_lineColLock.sizeDelta = C_Vector2(0, targetHeight)
      self.bindComponents.rRect_lineColLock.anchoredPosition = C_Vector2(-(targetWidth + offsetLine), targetPosY + offsetLine)
      self.bindComponents.rRect_lockStar.anchoredPosition = C_Vector2(-(targetWidth + offsetLine), 0)
    end
  end
end

function this:refreshInfo()
  local curProductId = self.bind.curProductId
  if curProductId <= 0 then
    self.bind.active_star = false
    self.bind.active_pointLockStar = false
    self.bind.active_lock = false
    self.bind.active_bg = false
    self.bind.active_selectCurr = false
    return
  end
  local tpl_pro = _productsTpl:getTplById(curProductId)
  local reward = _productsTpl:getRewardId(tpl_pro)
  local itemData = L_DataUtil.parseRewardConfigItem(reward[1])
  local conf = L_ItemTplManager:getItemConfig(itemData.itemType, itemData.itemId)
  self.bind.img_icon = conf.icon
  self.bind.txt_id = conf.name
  self.bind.img_qualityBg = string.format("UI/Atlas/Home/tex_homecommontip_bg_quality_%s.png", conf.quality or 0)
  local starTmp = {}
  for i = 1, conf.quality + 1 do
    table.insert(starTmp, {})
  end
  self.bind.list_star:clear()
  self.bind.list_star:insert_array(starTmp)
  local serverData = L_HomeStore:getHomeBuildingByGuid(self.bind.guid)
  local tpl_building = _homeBuildingTpl:getTplById(serverData.build_id)
  local level = _homeBuildingTpl:getLevel(tpl_building)
  local productTpl = _productsTpl:getTplById(curProductId)
  local needLv = _productsTpl:unlockBuildingLevel(productTpl)
  local isSatisfy, conditionRes = L_ConditionManager:isComplete(_productsTpl:getUnlockCondition(tpl_pro))
  self.bind.active_lock = not isSatisfy or level < needLv
end

function this:refreshSelect(selectId)
  local id = self.bind.curProductId
  self.bind.active_selectCurr = selectId == id
end

return this
