local this = class("modulePhotoGroup_template", require("ui.pages.photo.group.modulePhotoGroup_base"))
local _photoTemplateTpl = L_GameTpl:getPhotoTemplateTpl()
local STATE_MENU = {CONFIG = 1, CUSTOM = 2}

function this.bind()
  return {
    txt_empty = "",
    activeAddBtn = false,
    mainList = {
      moduleName = "pages/photo/cell/cellPhotoTemplate"
    },
    moduleTitle = {
      moduleName = "pages/photo/modulePhotoListTitle"
    },
    txt_title1 = "",
    txt_title2 = ""
  }
end

function this.methods()
  return {
    moduleTitle = {
      onClickToggle = function(self, isOn)
        self:switchState()
      end
    },
    mainList = {
      onClickCell = function(self, cell)
        local refreshFunc = function()
          self:refreshMainList(cell.bind.id)
        end
        if cell.bind.isSelect then
          L_PhotoManager:setTempChooseData(nil, nil, refreshFunc)
        else
          L_PhotoManager:setTempChooseData(cell.bind.type, cell.bind.id, refreshFunc)
        end
      end,
      onClickAdd = function(self, bind)
        local sureCb = function(tempName)
          if L_PhotoStore:checkCustomTempIsMax() then
            L_FlyMsgManager:showNormalMsgByKey("notice_modulePhotoGroup_template_04")
            return
          end
          local id = L_PhotoStore:getAutoCustomTempId()
          L_PhotoManager:screenTemplateShot(id, true, nil, function(bool, data, tex, name)
            if bool then
              local data = L_PhotoManager:createPhotoWorldData(bind.type, id, name, tempName)
              printf("createPhotoWorldData fullStr", data:toString())
              L_PhotoStore:req_createTemplate(data:toProtoSendData(), function(isSuccess)
                if isSuccess then
                  self:refreshMain()
                else
                  L_PhotoStore:removeTemplateData(data.type, data.id)
                end
              end)
            else
              L_FlyMsgManager:showNormalMsgByKey("notice_modulePhotoGroup_template_03")
            end
          end)
        end
        local width, height
        local oriWidth, oriHeight = 768, 432
        local d = C_Screen.height * oriWidth / (C_Screen.width * oriHeight)
        if d < 1 then
          height = oriHeight
          width = math.ceil(C_Screen.width / C_Screen.height * height)
        else
          width = oriWidth
          height = math.ceil(C_Screen.height / C_Screen.width * width)
        end
        L_PhotoManager:capture(function(t2d)
          L_UI:open("pageTemplateTip", {ok_cb = sureCb, rt = t2d})
        end, width, height)
      end
    }
  }
end

function this:open()
  L_PhotoManager:enterPhotoTemplate()
  self.state = STATE_MENU.CONFIG
  self:refreshMain()
end

function this:close()
  L_PhotoManager:exitPhotoTemplate()
end

function this:getShowDataList()
  local list = {}
  local dataList
  if self.state == STATE_MENU.CONFIG then
    dataList = _photoTemplateTpl.data
    for _, data in ipairs(dataList or table.empty) do
      if _photoTemplateTpl:getShow(data) then
        table.insert(list, {
          id = _photoTemplateTpl:getId(data),
          type = L_Const.photoTemplateDataType.config,
          inputName = _photoTemplateTpl:getName(data),
          bgImgName = _photoTemplateTpl:getIconStr(data),
          tabGroup = self.bindComponents.mainGroup,
          isTex = true
        })
      end
    end
  elseif self.state == STATE_MENU.CUSTOM then
    dataList = L_PhotoStore:getCustomTempList()
    for index, data in ipairs(dataList) do
      table.insert(list, {
        id = data.id,
        type = L_Const.photoTemplateDataType.custom,
        inputName = data.name,
        bgImgName = data.url,
        tabGroup = self.bindComponents.mainGroup
      })
    end
  end
  if self.state == STATE_MENU.CUSTOM and not L_PhotoStore:checkCustomTempIsMax() then
    table.insert(list, {
      isAddBtn = true,
      type = L_Const.photoTemplateDataType.custom,
      tabGroup = self.bindComponents.mainGroup
    })
  end
  return list
end

function this:refreshMain()
  local list = self:getShowDataList()
  self.bind.mainList:clear()
  self.bind.mainList:insert_array(list)
  local showEmptyTxt = self.state == STATE_MENU.CUSTOM and #list <= 1
  self.bind.txt_empty = showEmptyTxt and L_WordsTpl:getValue("notice_modulePhotoGroup_template_02") or ""
  self:refreshTitleTxt()
end

function this:refreshTitleTxt()
  local num = L_PhotoStore:getCustomTempCount()
  local str = string.format("(%s/%s)", num, L_PhotoStore.canAddTempMaxVal)
  self.bind.txt_title1 = L_WordsTpl:getValue("notice_modulePhotoGroup_template_01", {
    [0] = str
  })
  self.bind.txt_title2 = L_WordsTpl:getValue("notice_modulePhotoGroup_template_01", {
    [0] = str
  })
end

function this:refreshMainList(selectId)
  for i, v in pairs(self.modules.mainList) do
    if not math.isEmpty(v.bind.id) then
      local cell = v
      local isSelect = selectId == v.bind.id and not cell.bind.isSelect
      cell.bind.isSelect = isSelect
      cell:refreshSelect()
    end
  end
end

function this:switchState()
  if self.state == STATE_MENU.CONFIG then
    self.state = STATE_MENU.CUSTOM
  else
    self.state = STATE_MENU.CONFIG
  end
  self:refreshMain()
end

return this
