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

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

function this.bind()
  return {
    module_top = {
      moduleName = "pages/product/moduleRunningBuildingTop"
    },
    module_foodStorage = {
      moduleName = "pages/canteen/moduleFoodStorage"
    },
    img_icon = nil,
    txt_food = "",
    go_tips = false,
    txt_time = "",
    txt_tips = "",
    go_moduleNum = false,
    module_num = {
      moduleName = "pages/canteen/moduleCanteenNum"
    }
  }
end

function this.methods()
  return {
    onClick_stop = function(self)
      local func = function()
        L_CanteenManager:reqStopCanteen(self.data.buildingGuid)
      end
      local canteen = L_CanteenStore:getCanteen(self.data.buildingGuid)
      if canteen.remaining_time <= 0 then
        func()
      else
        local data_tip = {
          txtTitle = L_WordsTpl:getValue("ui_moduleCanteenRunning_05"),
          txtContent = L_WordsTpl:getValue("ui_moduleCanteenRunning_04"),
          confirmCallback = func
        }
        L_GameUtil.showCommonTip(data_tip)
      end
    end,
    onClick_add = function(self)
      if not self.bind.go_moduleNum then
        self:initModuleNum()
      end
      self.bind.go_moduleNum = true
    end
  }
end

function this:open()
  this.super.open(self)
  L_CanteenStore:listenCallFunc(L_CanteenStore.event.refreshFoodStorage, self.onEvent_refreshFoodStorage, self)
end

function this:close()
  this.super.close(self)
  L_CanteenStore:unListenCallFunc(L_CanteenStore.event.refreshFoodStorage, self.onEvent_refreshFoodStorage)
end

function this:initModule(buildingGuid)
  self.data.buildingGuid = buildingGuid
  local canteen = L_CanteenStore:getCanteen(self.data.buildingGuid)
  local tpl = _productsTpl:getTplById(canteen.product_id)
  self.bind.img_icon = _productsTpl:getIcon(tpl)
  self.bind.txt_food = string.format("+%s", _productsTpl:getGetFood(tpl) * 60)
  self.bind.go_moduleNum = false
  self.modules.module_top:setBuildingGuid(self.data.buildingGuid)
  self:refreshInfo()
end

function this:initModuleNum()
  local canteen = L_CanteenStore:getCanteen(self.data.buildingGuid)
  self.modules.module_num:initModule(self.data.buildingGuid, canteen.product_id)
end

function this:refreshInfo()
  local foodStorage = L_CanteenStore:getHomeFoodNum()
  self.bind.go_tips = false
  if foodStorage >= L_GameConstTpl:getHomeFoodStorage() then
    self.bind.go_tips = true
    self.bind.txt_tips = L_WordsTpl:getValue("ui_moduleCanteenRunning_03")
  end
  local canteen = L_CanteenStore:getCanteen(self.data.buildingGuid)
  if canteen.remaining_time <= 0 then
    self.bind.go_tips = true
    self.bind.txt_tips = L_WordsTpl:getValue("ui_moduleCanteenRunning_02")
  end
  local hour = math.floor(canteen.remaining_time / 60)
  local minute = canteen.remaining_time % 60
  self.bind.txt_time = L_WordsTpl:getValue("ui_moduleCanteenRunning_01", {
    [0] = hour,
    [1] = minute
  })
end

function this:onEvent_refreshFoodStorage()
  self:refreshInfo()
end

return this
