local this = class("moduleCanteenNum", G_UIModuleBase)
local _productsTpl = L_GameTpl:getProductsCanteenTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {
    selectPlanId = nil,
    productNum = nil,
    buildingGuid = nil,
    numChangeFunc = nil
  }
end

function this.bind()
  return {
    go_num = false,
    go_num_empty = false,
    txt_productName = "",
    module_num = {
      moduleName = "modulePages/moduleSelectNum"
    },
    scrollList_material = {
      moduleName = "pages/canteen/cellCanteenMaterial"
    },
    size_content = C_Vector2(700, 534),
    go_addBtn = false
  }
end

function this.methods()
  return {
    onClick_add = function(self)
      if L_CanteenManager:checkProduct(self.data.selectPlanId, self.data.productNum, self.data.buildingGuid, true) then
        local data = {
          count = self.data.productNum,
          guid = self.data.buildingGuid
        }
        L_CanteenManager:reqAddCanteen(data, function()
          self.data.productNum = L_CanteenManager:getMaxCanProductNum(self.data.selectPlanId) == 0 and 0 or 1
          self:initModuleNum()
          self:initMaterialList()
          if self.data.numChangeFunc then
            self.data.numChangeFunc()
          end
        end)
      end
    end
  }
end

function this:open()
  this.super.open(self)
end

function this:close()
  this.super.close(self)
end

function this:initModule(buildingGuid, planId, numChange)
  self.data.buildingGuid = buildingGuid
  self.data.selectPlanId = planId
  self.data.numChangeFunc = numChange
  local serverData = L_HomeStore:getHomeBuildingByGuid(self.data.buildingGuid)
  self.bind.go_addBtn = serverData.status == L_Const.WorldMapBuildStatusType.WMBST_PRODUCT
  if self.bind.go_addBtn then
    self.bind.size_content = C_Vector2(700, 606)
  else
    self.bind.size_content = C_Vector2(700, 534)
  end
  local tpl = _productsTpl:getTplById(self.data.selectPlanId)
  self.bind.txt_productName = L_WordsTpl:getValue("ui_moduleCanteenNum", {
    [0] = _productsTpl:getName(tpl)
  })
  self.data.productNum = L_CanteenManager:getMaxCanProductNum(self.data.selectPlanId) == 0 and 0 or 1
  self:initModuleNum()
  self:initMaterialList()
  if self.data.numChangeFunc then
    self.data.numChangeFunc()
  end
end

function this:initModuleNum()
  local max = function()
    return L_CanteenManager:getMaxCanProductNum(self.data.selectPlanId)
  end
  local func = function(num)
    self.data.productNum = num
    self:initCostInfo()
    if self.data.numChangeFunc then
      self.data.numChangeFunc()
    end
  end
  self.modules.module_num:initModule(func, max)
  func(self.data.productNum)
end

function this:initCostInfo()
  self.bind.go_num = self.data.productNum > 0
  self.bind.go_num_empty = self.data.productNum <= 0
  self:initMaterialList()
end

function this:initMaterialList()
  local num_tmp = self.data.productNum == 0 and 1 or self.data.productNum
  local tpl = _productsTpl:getTplById(self.data.selectPlanId)
  local data = _productsTpl:getMaterial(tpl)
  local tmp = {}
  for i, v in pairs(data) do
    local reward = L_DataUtil.parseRewardConfigItem(v)
    local hadNum = L_BagStore:getItemNum(reward.itemId)
    local needNum = num_tmp * reward.itemNum
    if hadNum < needNum then
      needNum = L_GameUtil.fillColor(needNum, L_Const.colorHtml.red001)
    end
    local itemData = L_DataUtil.parseRewardConfigItem(v)
    itemData.itemNumTxt = tostring(hadNum)
    table.insert(tmp, {
      txt_hasNum = tostring(needNum),
      itemData = itemData
    })
  end
  self.bind.scrollList_material:clear()
  self.bind.scrollList_material:insert_array(tmp)
end

return this
