local this = class("modulePetInfoCollectInfo", G_UIModuleBase)
local _homeBuildingProductionTpl = L_GameTpl:getHomeBuildingProductionTpl()
local _homeBuildingCollectionTpl = L_GameTpl:getHomeBuildingCollectionTpl()
local _petTpl = L_GameTpl:getPetTpl()
local CoolieEnvType = {
  BUILDING = 1,
  COLLECTION = 2,
  CROP = 3,
  RANCH_GATHER = 4,
  RANCH_PRODUCT = 5,
  SATIETY_RESTORE = 6,
  COLLECTION_MANAGE = 7
}

function this.bind()
  return {
    petIconActive = false,
    addPetActive = false,
    satietyActive = false,
    satietyCurScore = "",
    satietyTotalScore = "",
    petIcon = "",
    buildIcon = ""
  }
end

function this.methods()
  return {
    onClick_addPetBtn = function(self)
      self:dealChangePet()
    end,
    onClick_switchPetBtn = function(self)
      self:dealChangePet()
    end
  }
end

function this:dealChangePet()
  local func = function(petGuid)
    if self._envType == CoolieEnvType.RANCH_GATHER then
      L_HomeRanchStore:req_setGatherPet(self._blockId, petGuid, handler(self, self.refreshPet))
    elseif self._envType == CoolieEnvType.COLLECTION or self._envType == CoolieEnvType.BUILDING or self._envType == CoolieEnvType.COLLECTION_MANAGE then
      self._souceBuildGuid = self._souceBuildGuid or 0
      L_PetStore:req_CSProtoPetStationed(petGuid, self._buildType, self._buildGuid, self._souceBuildGuid, handler(self, self.refreshPet))
    end
  end
  L_UI:open("pageStation", {
    capacityId = self._capacityId,
    needLaborType = self._needLaborType,
    selectGuid = self._selectGuid,
    petSize = self._petSize,
    callback = func
  })
end

function this:open()
  L_SatietyStore:listenCallFunc(L_SatietyStore.event.satiety_value_refresh, self.refreshSatiety, self)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPet, self.refreshView, self)
end

function this:close()
  L_SatietyStore:unListenCallFunc(L_SatietyStore.event.satiety_value_refresh, self.refreshSatiety, self)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPet, self.refreshView, self)
end

function this:refreshPet()
  if self._envType == CoolieEnvType.RANCH_GATHER then
    self:initRanch(self._blockId)
  elseif self._envType == CoolieEnvType.BUILDING then
    self:initModule(self._buildGuid)
  elseif self._envType == CoolieEnvType.COLLECTION then
    self:setCollectionModule(self._buildGuid, self._souceBuildGuid or 0)
  elseif self._envType == CoolieEnvType.COLLECTION_MANAGE then
    self:setCollectionManageModule(self._buildGuid)
  end
end

function this:initRanch(blockId)
  self._envType = CoolieEnvType.RANCH_GATHER
  self._capacityId = L_HomeConst.homeLaborType.RANCH_GATHER
  self._needLaborType = L_HomeConst.HomeLaborId.ZHAO_LIAO
  self._petSize = {
    1,
    2,
    3,
    4
  }
  local blockRanchData = L_HomeRanchStore:getBlockRanchData(blockId)
  self._blockId = blockId
  self._selectGuid = blockRanchData and blockRanchData.harvest_pet or 0
  self:refreshView()
end

function this:initModule(buildingGuid)
  self._envType = CoolieEnvType.BUILDING
  local serverData = L_HomeStore:getHomeBuildingByGuid(buildingGuid)
  printf("\232\174\190\231\189\174\229\187\186\231\173\145\229\133\165\228\189\143\229\174\160\231\137\169", buildingGuid, serverData)
  local tpl = _homeBuildingProductionTpl:getTplById(serverData.build_id)
  self._capacityId = _homeBuildingProductionTpl:getPetHomeAttribute(tpl)
  self._needLaborType = _homeBuildingProductionTpl:getLaborType(tpl)
  self._petSize = _homeBuildingProductionTpl:getPetSize(tpl)
  self._buildType = L_Const.PetStationedWorkStatusType.PSWST_HOME_REST
  self._buildGuid = buildingGuid
  self._selectGuid = serverData.station_pet_guid
  local homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
  self.bind.buildIcon = homeBuildingTpl:getBuildingPixelIcon(homeBuildingTpl:getTplById(serverData.build_id))
  self.bindComponents.buildIcon:SetNativeSize()
  self:refreshView()
end

function this:setCollectionModule(collectionId, souceBuildId)
  self._envType = CoolieEnvType.COLLECTION
  local serverData = L_CollectionStore:getCollection(collectionId)
  local tpl_collection = _homeBuildingCollectionTpl:getTplById(serverData.build_id)
  self._capacityId = _homeBuildingCollectionTpl:getPetCapacity(tpl_collection)
  self._petSize = _homeBuildingCollectionTpl:getPetSize(tpl_collection)
  self._needLaborType = _homeBuildingCollectionTpl:getLaborType(tpl_collection)
  self._buildType = L_Const.PetStationedWorkStatusType.PSWST_WILD_REST
  self._buildGuid = collectionId
  self._selectGuid = serverData.pet_guid
  self._souceBuildGuid = souceBuildId or 0
  local homeCollectionWorldTpl = L_GameTpl:getHomeCollectionWorldTpl()
  self.bind.buildIcon = homeCollectionWorldTpl:getPixelIcon(homeCollectionWorldTpl:getTplById(collectionId))
  self.bindComponents.buildIcon:SetNativeSize()
  self:refreshView()
end

function this:setCollectionManageModule(buildingId)
  self._envType = CoolieEnvType.COLLECTION_MANAGE
  local serverData = L_HomeStore:getHomeBuildingByGuid(buildingId)
  self._capacityId = L_HomeConst.homeLaborType.COLLECTION_MANAGE
  self._petSize = {
    1,
    2,
    3,
    4
  }
  self._buildType = L_Const.PetStationedWorkStatusType.PSWST_HOME_WORK
  self._buildGuid = buildingId
  self._selectGuid = serverData.station_pet_guid
  local homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
  self.bind.buildIcon = homeBuildingTpl:getBuildingPixelIcon(homeBuildingTpl:getTplById(serverData.build_id))
  self.bindComponents.buildIcon:SetNativeSize()
  self:refreshView()
end

function this:refreshView()
  self.bind.petIconActive = not math.isEmpty(self._selectGuid)
  self.bind.satietyActive = self.bind.petIconActive
  self.bind.addPetActive = math.isEmpty(self._selectGuid)
  if math.isEmpty(self._selectGuid) then
  else
    local pet = L_PetStore:getPetItem(self._selectGuid)
    local configData = L_ItemTplManager:getPetItem(pet.id, pet:isSpecialPet())
    self.bind.petIcon = _petTpl:getPetPixelIcon(_petTpl:getTplById(pet.id), pet:isSpecialPet())
    self:refreshSatiety()
  end
end

function this:refreshSatiety()
  local currentSatiety = L_SatietyStore:getPetSatiety(self._selectGuid)
  local maxSatiety = L_SatietyStore:getPetMaxSatiety(self._selectGuid)
  local color = L_Const.colorHtml.yellow016
  if currentSatiety == 0 then
    color = L_Const.colorHtml.red002
  end
  self.bind.satietyCurScore = L_GameUtil.fillColor(string.format("%d", currentSatiety), color)
  self.bind.satietyTotalScore = string.format("/%d", maxSatiety)
end

return this
