local this = class("moduleCanteenFree", G_UIModuleBase)
local _productsTpl = L_GameTpl:getProductsCanteenTpl()
local _homeBuildingCanteenTpl = L_GameTpl:getHomeBuildingCanteenTpl()
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()

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

function this.bind()
  return {
    module_top = {
      moduleName = "pages/home/moduleHomeBuildingTop"
    },
    module_foodStorage = {
      moduleName = "pages/canteen/moduleFoodStorage"
    },
    scrollList_plan = {
      moduleName = "pages/canteen/cellCanteenPlan"
    },
    module_master = {
      moduleName = "pages/product/moduleSetMaster"
    },
    txt_resident = "",
    txt_time = "",
    txt_food = "",
    module_num = {
      moduleName = "pages/canteen/moduleCanteenNum"
    }
  }
end

function this.methods()
  return {
    onClick_start = function(self)
      local num = self.modules.module_num.data.productNum
      if L_CanteenManager:checkProduct(self.data.selectPlanId, num, self.data.buildingGuid, false) then
        local data = {
          productId = self.data.selectPlanId,
          guid = self.data.buildingGuid,
          count = num
        }
        L_CanteenManager:reqStartCanteen(data)
      end
    end,
    scrollList_plan = {
      onClick_select = function(self, bind)
        if bind.go_lock then
          return
        end
        self.data.selectPlanId = bind.id
        self:setPlanListActive()
        self:initModuleNum()
        self:initMasterInfo()
      end
    }
  }
end

function this:open()
  this.super.open(self)
  L_HomeResidentStore:listenCallFunc(L_HomeResidentStore.event.residentDataUpdate, self.onEvent_refreshResident, self)
end

function this:close()
  this.super.close(self)
  L_HomeResidentStore:unListenCallFunc(L_HomeResidentStore.event.residentDataUpdate, self.onEvent_refreshResident)
end

function this:initModule(buildingGuid)
  self.data.buildingGuid = buildingGuid
  local serverData = L_HomeStore:getHomeBuildingByGuid(self.data.buildingGuid)
  self.data.buildingId = serverData.build_id
  self.modules.module_top:setGuid(self.data.buildingGuid)
  self:initPlanList()
  self:initModuleNum()
  self:initMasterInfo()
end

function this:initPlanList()
  local tpl_building = _homeBuildingTpl:getTplById(self.data.buildingId)
  local groupId = _homeBuildingTpl:getGroupId(tpl_building)
  local level = _homeBuildingTpl:getLevel(tpl_building)
  local data = _homeBuildingCanteenTpl:getUnlockMap(groupId)
  local tmp = {}
  for i, v in pairs(data) do
    local tpl_pro = _productsTpl:getTplById(i)
    table.insert(tmp, {
      id = i,
      img_icon = _productsTpl:getIcon(tpl_pro),
      txt_name = _productsTpl:getName(tpl_pro),
      txt_food = string.format("+%s", _productsTpl:getGetFood(tpl_pro) * 60),
      go_lock = v > level,
      go_unlock = v <= level,
      txt_unlock = L_WordsTpl:getValue("ui_moduleCanteenFree_03", {
        [0] = v
      }),
      go_needMaster = _productsTpl:getIsNeedMaster(tpl_pro),
      alpha_content = v > level and 0.5 or 1
    })
  end
  self.data.selectPlanId = self.data.selectPlanId or tmp[1].id
  self.bind.scrollList_plan:clear()
  self.bind.scrollList_plan:insert_array(tmp)
  self:setPlanListActive()
end

function this:initMasterInfo()
  local func = function(masterId)
    self.data.selectMasterId = masterId
  end
  self.modules.module_master:initModule(func)
end

function this:initModuleNum()
  local numChange = function()
    self:initCostInfo()
  end
  self.modules.module_num:initModule(self.data.buildingGuid, self.data.selectPlanId, numChange)
end

function this:initCostInfo()
  local num = self.modules.module_num.data.productNum
  self.bind.txt_time = L_WordsTpl:getValue("ui_timeUtil_09", {
    [0] = num
  })
  local tpl = _productsTpl:getTplById(self.data.selectPlanId)
  local getFood = _productsTpl:getGetFoodAnHour(tpl)
  self.bind.txt_food = string.format("+%s", getFood * num)
  self:onEvent_refreshResident()
end

function this:setPlanListActive()
  for i = 1, #self.bind.scrollList_plan do
    local active = false
    if self.bind.scrollList_plan:getValue(i, "id") == self.data.selectPlanId then
      active = true
    end
    self.bind.scrollList_plan:change(i, {go_active = active})
  end
end

function this:checkProduct()
  local num = self.modules.module_num.data.productNum
  local num_tem = num == 0 and 1 or num
  local tpl_building = _homeBuildingTpl:getTplById(self.data.buildingId)
  local groupId = _homeBuildingTpl:getGroupId(tpl_building)
  local level = _homeBuildingTpl:getLevel(tpl_building)
  local data = _homeBuildingCanteenTpl:getUnlockMap(groupId)
  if level < data[self.data.selectPlanId] then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_moduleCanteenFree_02"))
    return false
  end
  local tpl = _productsTpl:getTplById(self.data.selectPlanId)
  local material = _productsTpl:getMaterial(tpl)
  for i, v in pairs(material) do
    local hasNum = L_BagStore:getItemNum(v[2])
    local needNum = num_tem * v[3]
    if hasNum < needNum then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_shortageMaterial"))
      return false
    end
  end
  return true
end

function this:onEvent_refreshResident()
  local tpl = _productsTpl:getTplById(self.data.selectPlanId)
  local resident_need = _productsTpl:getNeedLabor(tpl)
  local resident_have = L_HomeResidentStore:getRestResidentNum()
  if resident_need > resident_have then
    resident_have = L_GameUtil.fillColor(resident_have, L_Const.colorHtml.red001)
  end
  self.bind.txt_resident = L_WordsTpl:getValue("ui_moduleCanteenFree_01", {
    [0] = resident_have,
    [1] = resident_need
  })
end

return this
