local this = class("moduleHomeCenterProduction", require("ui.pages.home.homeCenter.moduleHomeCenterToggleModule"))

function this.bind()
  return {
    list_cellFreeWorkProduction = {
      moduleName = "pages/home/homeCenter/moduleHomeCenterProduction/cellFreeWorkProduction"
    },
    enable_scrollView = true,
    active_noBuildingPart = false
  }
end

function this.methods()
  return {
    list_cellFreeWorkProduction = {
      onClickImgBuilding = function(self, params)
        self:openProductionPart(params)
      end
    }
  }
end

function this:open()
  self.onHomeHubSyncHandler = handler(self, self.onHomeHubSync)
  L_HomeStore:getCsHomeStore():RegisterEvent(C_EHomeStoreEvent.HomeHubSync, self.onHomeHubSyncHandler)
end

function this:close()
  L_HomeStore:getCsHomeStore():UnregisterEvent(C_EHomeStoreEvent.HomeHubSync, self.onHomeHubSyncHandler)
end

function this:refreshData()
  self.UIData = {}
  self.UIData.homeHubWorkingBuildingInfoList = L_HomeStore:getHomeHubWorkingBuildingInfoListByTabType(L_Const.HomeHubTabType.HHTT_PRODUCT)
  self.UIData.dragCellViewIndex = nil
  self.UIData.dragEndCellViewIndex = nil
  self.UIData.dragCell = nil
  self.UIData.dragDeltaPos = nil
  self.UIData.dragStart = false
end

function this:refreshUI()
  local insertArray = {}
  for i = 1, #self.UIData.homeHubWorkingBuildingInfoList do
    table.insert(insertArray, {})
  end
  self.bind.list_cellFreeWorkProduction:clear()
  self.bind.list_cellFreeWorkProduction:insert_array(insertArray)
  for i = 1, #self.UIData.homeHubWorkingBuildingInfoList do
    local cellFreeWorkProduction = self.bind.list_cellFreeWorkProduction:getItemCls(i)
    local homeHubWorkingBuildingInfo = self.UIData.homeHubWorkingBuildingInfoList[i]
    cellFreeWorkProduction:refreshData(homeHubWorkingBuildingInfo)
    cellFreeWorkProduction:refreshUI()
  end
  self.bind.active_noBuildingPart = #self.UIData.homeHubWorkingBuildingInfoList == 0
end

function this:show()
  this.super.show(self)
  self:configDrag("+")
end

function this:hide()
  this.super.hide(self)
  self:configDrag("-")
end

function this:configDrag(operator)
  self.onUITouchHandle = self.onUITouchHandle or L_CommonUtil.handle(self.onUITouch, self)
  self.onUIDragHandle = self.onUIDragHandle or L_CommonUtil.handle(self.onUIDrag, self)
  self.onUIDragEndHandle = self.onUIDragEndHandle or L_CommonUtil.handle(self.onUIDragEnd, self)
  self.onUITouchEndHandle = self.onUITouchEndHandle or L_CommonUtil.handle(self.onUIDragEnd, self)
  C_ScreenTouch.OnUI_Touch(operator, self.onUITouchHandle)
  C_ScreenTouch.OnUI_TouchEnd(operator, self.onUITouchEndHandle)
  C_ScreenTouch.OnUI_Drag(operator, self.onUIDragHandle)
  C_ScreenTouch.OnUI_DragEnd(operator, self.onUIDragEndHandle)
end

function this:onUITouch(gesture)
  if self.UIData.dragStart == true then
    return
  end
  local go = gesture:GetCurrentFirstPickedUIElement()
  self.UIData.dragCellViewIndex, self.UIData.dragCell = self:getDragCellViewIndexAndCls(go)
  if self.UIData.dragCellViewIndex == nil then
    return
  end
  L_FlyMsgManager:startLongPress(gesture, 0.5, function()
    if not self.isBind or self.UIData.dragCellViewIndex == nil then
      return
    end
    local touchLocPos = L_Vector3.screenToLocalInRectangle(self.bindComponents.rect_list_cellFreeWorkProduction, gesture.position)
    local oriLocPos = {
      x = self.UIData.dragCell:getRect().transform.localPosition.x,
      y = self.UIData.dragCell:getRect().transform.localPosition.y
    }
    self.UIData.dragDeltaPos = {
      x = touchLocPos.x - oriLocPos.x,
      y = touchLocPos.y - oriLocPos.y
    }
    L_Vector3.setAnchored(self.UIData.dragCell:getRect(), touchLocPos)
    self.UIData.dragCell:inactiveDragBtn()
    self.bind.enable_scrollView = false
    self.bindComponents.enable_dragLayout.enabled = false
    self.UIData.dragStart = true
  end)
end

function this:onUIDragStart(gesture)
end

function this:onUIDrag(gesture)
  if self.UIData.dragStart ~= true then
    return
  end
  local touchLocPos = L_Vector3.screenToLocalInRectangle(self.bindComponents.rect_list_cellFreeWorkProduction, gesture.position)
  L_Vector3.setAnchored(self.UIData.dragCell:getRect(), touchLocPos)
end

function this:onUIDragEnd(gesture)
  if self.UIData.dragStart ~= true then
    return
  end
  local go = gesture:GetCurrentFirstPickedUIElement()
  self.UIData.dragEndCellViewIndex = self:getDragCellViewIndexAndCls(go)
  if self.UIData.dragEndCellViewIndex ~= nil and self.UIData.dragCellViewIndex ~= nil then
    local dragCellHomeHubWorkingBuildingInfo = self.UIData.homeHubWorkingBuildingInfoList[self.UIData.dragCellViewIndex]
    local dragEndCellHomeHubWorkingBuildingInfo = self.UIData.homeHubWorkingBuildingInfoList[self.UIData.dragEndCellViewIndex]
    local sourcePos = dragCellHomeHubWorkingBuildingInfo.index
    local targetPos = dragEndCellHomeHubWorkingBuildingInfo.index
    L_HomeStore:reqChangeHomeHubBuildingPos(L_Const.HomeHubTabType.HHTT_PRODUCT, sourcePos, targetPos)
  end
  if self.UIData.dragCell then
    self.UIData.dragCell:activeDragBtn()
  end
  self.bindComponents.enable_dragLayout.enabled = true
  self.bind.enable_scrollView = true
  self.UIData.dragStart = false
  self.UIData.dragCellViewIndex = nil
  self.UIData.dragEndCellViewIndex = nil
  self.UIData.dragCell = nil
  self.UIData.dragDeltaPos = nil
end

function this:getDragCellViewIndexAndCls(go)
  for viewIndex, cls in pairs(self.modules.list_cellFreeWorkProduction) do
    local cellFreeWorkProduction = cls
    if cellFreeWorkProduction:checkIfOnDragCell(go) == true then
      return viewIndex, cellFreeWorkProduction
    end
  end
  return nil, nil
end

function this:onHomeHubSync()
  self:refreshData()
  self:refreshUI()
end

function this:openProductionPart(params)
  local buildingType = params.buildingType
  local buildingGuid = params.buildingGuid
  local pageName = L_HomeConst.BuildType2Page[buildingType]
  if pageName then
    L_UI:open(pageName, {
      buildingGuid = buildingGuid,
      buildType = buildingType,
      isFromHomeCenter = true
    })
  end
end

return this
