local this = class("moduleSelectNum", G_UIModuleBase)

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {
    max = nil,
    min = nil,
    func = nil,
    num = 1
  }
end

function this.bind()
  return {
    txt_num = "",
    interactable_btn_add = false,
    interactable_btn_decrease = false,
    interactable_btn_max = false,
    interactable_btn_min = false
  }
end

function this.methods()
  return {
    onEditEnd_num = function(self, text)
      self:changeNum(tonumber(text))
    end,
    onClick_add = function(self)
      self:changeNum(self.data.num + 1)
    end,
    onClick_decrease = function(self)
      self:changeNum(self.data.num - 1)
    end,
    onClick_max = function(self)
      self:changeNum(math.maxinteger)
    end,
    onClick_min = function(self)
      self:changeNum(math.mininteger)
    end
  }
end

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

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

function this:initModule(func, max, min, defaultSelectNum)
  min = min or 1
  self.data.max = max
  self.data.min = min
  self.data.func = func
  self.data.num = defaultSelectNum or 1
  self:showNum()
end

function this:changeNum(num)
  local max = self.data.max
  if type(self.data.max) ~= "number" then
    max = self.data.max()
  end
  local min = self.data.min
  if type(self.data.min) ~= "number" then
    min = self.data.min()
  end
  num = num or min
  self.data.num = math.clamp(num, min, max)
  self:showNum()
  if self.data.func then
    self.data.func(self.data.num)
  end
end

function this:getNum()
  return self.data.num
end

function this:showNum()
  self.bind.txt_num = tostring(self.data.num)
  self:refreshBtnStatus()
end

function this:refreshBtnStatus()
  local max = self.data.max
  if type(self.data.max) ~= "number" then
    max = self.data.max()
  end
  local min = self.data.min
  if type(self.data.min) ~= "number" then
    min = self.data.min()
  end
  self.bind.interactable_btn_add = max > self.data.num
  self.bind.interactable_btn_decrease = min < self.data.num
  self.bind.interactable_btn_max = max > self.data.num
  self.bind.interactable_btn_min = min < self.data.num
end

return this
