local this = class("moduleProductInfoNormal", G_UIModuleBase)
local _productsTpl = L_GameTpl:getProductsTpl()
local _foodGroupTpl = L_GameTpl:getFoodGroupTpl()
local _foodTpl = L_GameTpl:getFoodTpl()
local _foodMaterialTpl = L_GameTpl:getFoodMaterialGroupTpl()
local _petEggTpl = L_GameTpl:getPetEggTpl()

function this.bind()
  return {
    module_productFormulaNormalNew = {
      moduleName = "pages/product/moduleProductFormulaNormalNew"
    },
    active_moduleProductInfoDesc_normal = true,
    moduleProductInfoDesc_normal = {
      moduleName = "pages/product/moduleProductInfoDesc_normal"
    },
    active_moduleProductInfoDesc_accessory = true,
    moduleProductInfoDesc_accessory = {
      moduleName = "pages/product/moduleProductInfoDesc_accessory"
    },
    active_moduleProductInfoDesc_egg = true,
    moduleProductInfoDesc_egg = {
      moduleName = "pages/product/moduleProductInfoDesc_egg"
    },
    active_isEmpty = false,
    active_startBtn_accessory = true,
    active_startBtn_normal = true
  }
end

function this.methods()
  return {
    onClick_start = function(self)
      self:onClickStart()
    end,
    moduleProductInfoDesc_normal = {
      onShowFoodItemDesc = function(self, isShow)
        self.bindComponents.rect_descImg.anchoredPosition = C_Vector2(self.bindComponents.rect_descImg.anchoredPosition.x, isShow and -204 or -230)
      end
    }
  }
end

function this:open()
  self:setIsEmpty(true)
  self.moduleData = {}
end

function this:hide()
  self.modules.module_productFormulaNormalNew:hide()
end

function this:setMultiQualityProduct(buildingGuid, foodGroupId)
  self.multiQuaProductData = self.multiQuaProductData or {}
  self.multiQuaProductData.buildingGuid = buildingGuid
  self.multiQuaProductData.foodGroupId = foodGroupId
  self.multiQuaProductData.selectNum = self.multiQuaProductData.selectNum or 1
  self.moduleData.productInfoDescType = L_HomeConst.ProductInfoDescType.Normal
  self.moduleData.isMultiQualityProduct = true
  self:setUsingButton()
  self:setActiveInfoDescPart()
  self:setIsEmpty(false)
  local module_productFormulaNormalNew = self.modules.module_productFormulaNormalNew
  module_productFormulaNormalNew:setFoodGroup(foodGroupId)
end

function this:changeMultiQuaProductId(productId)
  self.multiQuaProductData.productId = productId
  local moduleProductInfoDesc_normal = self.modules.moduleProductInfoDesc_normal
  moduleProductInfoDesc_normal:setData(self.multiQuaProductData.productId, self.multiQuaProductData.buildingGuid, self)
  moduleProductInfoDesc_normal:refreshUI()
end

function this:checkIfCanStartMultiQualityProduct()
  local groupTpl = _foodGroupTpl:getTplById(self.multiQuaProductData.foodGroupId)
  local serverData = L_HomeStore:getHomeBuildingByGuid(self.multiQuaProductData.buildingGuid)
  if _foodGroupTpl:getIsNeedPet(groupTpl) and math.isEmpty(serverData.station_pet_guid) then
    L_FlyMsgManager:showNormalMsgByKey("notice_pageHomeCook_01")
    return false
  end
  if L_HomeUtil.checkBuildCanProduct(self.multiQuaProductData.buildingGuid, self.multiQuaProductData.productId) then
    local moduleProductFormulaNormalNew = self.modules.module_productFormulaNormalNew
    return moduleProductFormulaNormalNew:checkItemSatisfy()
  end
end

function this:startMultiQualityProduct()
  local moduleProductFormulaNormalNew = self.modules.module_productFormulaNormalNew
  local targetTpl, selectRars = moduleProductFormulaNormalNew:getSelectFoodInfo()
  local guid = self.multiQuaProductData.buildingGuid
  local cookId = _foodTpl:getId(targetTpl)
  local count = self.multiQuaProductData.selectNum
  local materials = _foodTpl:getNeedMaterial(targetTpl)
  local items = {}
  for i = 1, 4 do
    local material = materials[i]
    if material then
      local itemId = _foodMaterialTpl:getItemId(material[1], selectRars[i])
      local oneNum = material[2]
      table.insert(items, {item_id = itemId, item_num = oneNum})
    end
  end
  L_CookStore:req_cook(guid, cookId, count, items)
end

function this:setSingleQualityProduct(buildingGuid, productId, buildType)
  if productId == nil or productId == 0 then
    return
  end
  self.singleQuaProductData = self.singleQuaProductData or {}
  self.singleQuaProductData.buildingGuid = buildingGuid
  self.singleQuaProductData.productId = productId
  self.singleQuaProductData.selectNum = 1
  self.moduleData.productInfoDescType = L_HomeConst.ProductInfoDescType.Normal
  self.moduleData.isMultiQualityProduct = false
  self:setUsingButton()
  self:setActiveInfoDescPart()
  self:setIsEmpty(false)
  local module_productFormulaNormalNew = self.modules.module_productFormulaNormalNew
  module_productFormulaNormalNew:setProduct(buildingGuid, productId, buildType)
  self:setProductInfoDesc(productId, self.singleQuaProductData.buildingGuid)
end

function this:setProductInfoDesc(productId, buildingGuid)
  local moduleProductInfoDesc_normal = self.modules.moduleProductInfoDesc_normal
  moduleProductInfoDesc_normal:setData(productId, buildingGuid, self)
  moduleProductInfoDesc_normal:refreshUI()
end

function this:checkIfCanStartSingleQualityProduct()
  if self.singleQuaProductData.buildingGuid == 0 then
    local isHadIdle = L_ProductManager:onIsHadIdleQueueWithFastProduct()
    if not isHadIdle then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_product_lack_produce_queue"))
      return false
    end
    local tpl = _productsTpl:getTplById(self.singleQuaProductData.productId)
    local reward = _productsTpl:getRewardId(tpl)
    if not table.isEmpty(reward) then
      local itemData = L_DataUtil.parseRewardConfigItem(reward[1])
      if itemData.itemType == L_Const.resType.homeBuilding then
        local hadNum = L_ProductManager:onGetAllFastProductCount(self.singleQuaProductData.productId, true)
        local maxNum = L_ProductManager:onGetMaxCountFastProduct(self.singleQuaProductData.productId)
        if hadNum >= maxNum then
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_home_building_reach_limit"))
          return false
        end
      end
    end
    return self.modules.module_productFormulaNormalNew:checkItemSatisfy()
  elseif L_HomeUtil.checkBuildCanProduct(self.singleQuaProductData.buildingGuid, self.singleQuaProductData.productId) then
    return self.modules.module_productFormulaNormalNew:checkItemSatisfy()
  end
end

function this:getNormalProductMaxCanDo()
  local moduleProductFormulaNormalNew = self.modules.module_productFormulaNormalNew
  return moduleProductFormulaNormalNew:getMaxCanDo()
end

function this:setNormalProductSelectNum(selectNum)
  local moduleProductFormulaNormalNew = self.modules.module_productFormulaNormalNew
  moduleProductFormulaNormalNew:setSelectNum(selectNum)
  if self.moduleData.isMultiQualityProduct then
    self.multiQuaProductData.selectNum = selectNum
  else
    self.singleQuaProductData.selectNum = selectNum
  end
end

function this:setEggInfo(eggGuid, buildingGuid)
  self.eggData = self.eggData or {}
  self.eggData.eggGuid = eggGuid
  self.eggData.buildingGuid = buildingGuid
  self.moduleData.productInfoDescType = L_HomeConst.ProductInfoDescType.Egg
  self.moduleData.isMultiQualityProduct = false
  self:setUsingButton()
  self:setActiveInfoDescPart()
  self:setIsEmpty(false)
  local module_productFormulaNormalNew = self.modules.module_productFormulaNormalNew
  module_productFormulaNormalNew:setEggGuid(self.eggData.eggGuid)
  self:setEggInfoDesc()
end

function this:setEggInfoDesc()
  local moduleProductInfoDesc_egg = self.modules.moduleProductInfoDesc_egg
  moduleProductInfoDesc_egg:setData(self.eggData.eggGuid, self.eggData.buildingGuid, self)
  moduleProductInfoDesc_egg:refreshUI()
end

function this:tryStartHatchEgg()
  local petEggData = L_PetStore:getPetEggItem(self.eggData.eggGuid)
  local petEggTpl = _petEggTpl:getTplById(petEggData.configId)
  local productId = _petEggTpl:getProduct(petEggTpl)
  if not L_HomeUtil.checkBuildCanProduct(self.eggData.buildingGuid, productId) then
    return
  end
  L_PetStore:req_hatchPetEgg(self.eggData.buildingGuid, self.eggData.eggGuid)
end

function this:setUsingButton()
  self.bind.active_startBtn_accessory = self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Accessory
  self.bind.active_startBtn_normal = self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Normal or self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Egg
end

function this:setActiveInfoDescPart()
  self.bind.active_moduleProductInfoDesc_normal = self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Normal
  self.bind.active_moduleProductInfoDesc_accessory = self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Accessory
  self.bind.active_moduleProductInfoDesc_egg = self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Egg
end

function this:setIsEmpty(isEmpty)
  self.bind.active_isEmpty = isEmpty
end

function this:onClickStart()
  local timeNowServer = C_TimeUtility.NowServer
  if self.moduleData.lastClickStartTime ~= nil and timeNowServer - self.moduleData.lastClickStartTime < 0.5 then
    return
  end
  self.moduleData.lastClickStartTime = timeNowServer
  if self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Normal then
    self:tryStartNormalProduct()
  elseif self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Egg then
    self:tryStartHatchEgg()
  end
end

function this:tryStartNormalProduct()
  if self.singleQuaProductData and self.singleQuaProductData.buildingGuid == 0 then
    if not self:checkIfCanStartSingleQualityProduct() then
      return
    end
    local selectMaterial = self.modules.module_productFormulaNormalNew:getSelectMaterialTableIdList()
    L_ProductStore:req_CSProtoSimpleProductStart(self.singleQuaProductData.productId, self.singleQuaProductData.selectNum, selectMaterial)
  elseif self.moduleData.isMultiQualityProduct then
    if not self:checkIfCanStartMultiQualityProduct() then
      return
    end
    self:startMultiQualityProduct()
  else
    if not self:checkIfCanStartSingleQualityProduct() then
      return
    end
    local productId = self.singleQuaProductData.productId
    local buildingGuid = self.singleQuaProductData.buildingGuid
    local productTpl = _productsTpl:getTplById(productId)
    if productTpl == nil then
      return
    end
    local groupType = _productsTpl:getGroupId(productTpl)
    local buildFunctionType = L_ProductManager:onGetHomeBuildFunctionType(groupType)
    if buildFunctionType == L_HomeConst.HomeBuildFunctionType.EQUIP then
      local selectGuid = self.modules.module_productFormulaNormalNew:getSelectedGuid()
      L_ProductStore:req_CSProtoAccessoryProductStart(buildingGuid, productId, selectGuid)
    else
      local selectMaterial = self.modules.module_productFormulaNormalNew:getSelectMaterialTableIdList()
      L_ProductStore:req_CSProtoNormalProductStart(buildingGuid, productId, self.singleQuaProductData.selectNum, selectMaterial)
    end
  end
end

function this:initSelectNum()
  if self.moduleData.productInfoDescType == L_HomeConst.ProductInfoDescType.Normal then
    local moduleProductInfoDesc_normal = self.modules.moduleProductInfoDesc_normal
    moduleProductInfoDesc_normal:initSelectNum()
  end
end

return this
