local this = class("moduleLogin", G_UIModuleBase)

function this.bind()
  return {
    appVersionText = nil,
    resVersionText = nil,
    dev_box = true,
    dev_ipToggleValue = false,
    dev_InterimValue = false,
    dev_accountInput = "",
    dev_ipInput = "",
    dev_portInput = "",
    ipBoxActive = false,
    goPanelServerList = false,
    txtEnterGameServerName = L_WordsTpl:getValue("ui_moduleLogin_02"),
    serverList = {
      moduleName = "modulePages/selectServerServerItem"
    },
    versionText = "",
    mainUi = true,
    sdkLogging = false
  }
end

function this.methods()
  return {
    onClickAgeMark = function(self)
      local data = {
        txtTitle = L_WordsTpl:getTplById("login_age_tip_title"),
        hideCancel = true,
        hideConfirm = true,
        txtContent = L_WordsTpl:getTplById("notice_login_agetips_1")
      }
      L_UI:open("pageTextNoticeWindow", data)
    end,
    dev_onInputIPMethodToggleChanged = function(self, isOn)
      self:setIpInputShow(isOn)
    end,
    dev_onEndEditAccountInput = function(self, text)
      self.dev_account = text
      C_PlayerPrefsUtility.SetString(L_Const.prefsKey.playerAccount, text)
    end,
    dev_onEndIpInput = function(self, text)
      self.dev_inputIp = text
      C_PlayerPrefsUtility.SetString(L_Const.prefsKey.localInputIp, text)
    end,
    dev_onEndPortInput = function(self, text)
      self.dev_inputPort = text
      C_PlayerPrefsUtility.SetString(L_Const.prefsKey.localInputPort, text)
    end,
    onClick_selectServer = function(self)
      self.bind.goPanelServerList = true
      self:refreshServerList()
    end,
    dev_onInterimStateToggleChanged = function(self, isOn)
      self:setInterim(isOn)
    end,
    serverList = {
      onClick = function(self, bind)
        self:refreshServerView(bind.id)
        self.bind.goPanelServerList = false
      end
    },
    dev_onClickShowDebugToggleButton = function(self)
      if self._showDebugToggleButtonClickCount == nil then
        self._showDebugToggleButtonClickCount = 0
      end
      self._showDebugToggleButtonClickCount = self._showDebugToggleButtonClickCount + 1
      if self._showDebugToggleButtonClickCount >= 10 then
        self._showDebugToggleButtonClickCount = 0
        CS.Lens.Tools.RuntimeDebug.RuntimeDebugActionHelper.SetTouchToggleActive(true)
      end
    end,
    onClick_startBtn = function(self)
      print("startBtn 1")
      self:startGame()
      self.isStartClick = true
    end,
    onEnter_startBtn = function(self)
      L_AudioUtil.playSound("Play_SFX_System_UI_Login_Begin_Enter")
    end,
    onExit_starBtn = function(self)
      if self.isStartClick then
        return
      end
      L_AudioUtil.playSound("Play_SFX_System_UI_Login_Begin_Exit")
    end,
    onClick_checkBtn = function(self)
      L_GameUtil.showCommonTip({
        txtTitle = L_WordsTpl:getValue("title_notice_option_ifcheckintegrity"),
        txtContent = L_WordsTpl:getValue("notice_option_ifcheckintegrity")
      })
    end,
    onClick_clearBtn = function(self)
      L_GameUtil.showCommonTip({
        txtTitle = L_WordsTpl:getValue("title_notice_login_clear"),
        txtContent = L_WordsTpl:getValue("notice_login_clear"),
        confirmCallback = function()
          C_PlayerPrefsUtility.DeleteAll()
        end
      })
    end,
    onClick_announceBtn = function(self)
      self:tryShowGameAnnounce()
    end,
    onClick_logoutBtn = function(self)
      L_LoginManager:exitGame()
    end,
    onClick_sdkBtn = function(self)
      L_LoginManager:sdkLogout()
    end
  }
end

function this:show()
end

function this:open(options)
  self.bind.appVersionText = string.format("AppVer: %s ResVer: %s", C_InfoManager.AppVersion, C_InfoManager.AssetVersion)
  if L_DeviceTpl:getIsPc() then
    self.bind.versionText = L_WordsTpl:getValue("copyright_information_pc_text")
  else
    self.bind.versionText = L_WordsTpl:getValue("copyright_information_mobile_text")
  end
  local csServers = C_InfoManager.ServerList
  self.serverList = L_CommonUtil.csListToTable(csServers)
  self:initPlaySaveData()
  L_LoginManager:addListener(L_LoginManager.events.loginUiShow, self.onLoginUiShow, self)
end

function this:close()
  L_LoginManager:removeListener(L_LoginManager.events.loginUiShow, self.onLoginUiShow, self)
end

function this:onLoginUiShow(data)
  local show = data.showUi
  self.bind.mainUi = show
  self.bind.sdkLogging = not show
end

function this:initPlaySaveData()
  local isDev = L_LoginManager.isDevLogin
  self.bind.dev_box = isDev
  local serverId = not isDev and 1 or L_SettingStore:getPrefsInt(L_Const.prefsKey.serverId)
  self:refreshServerView(serverId, not isDev)
  local isOnIp = L_SettingStore:getPrefsInt(L_Const.prefsKey.selectIPButton) == 1
  self.bind.dev_ipToggleValue = isOnIp
  self:setIpInputShow(isOnIp)
  local isInterim = L_SettingStore:getPrefsInt(L_Const.prefsKey.selectInterim) == 1
  self.bind.dev_InterimValue = isInterim
  self:setInterim(isInterim)
  if isDev then
    self.dev_account = L_SettingStore:getLocalAccount() or ""
    self.bind.dev_accountInput = self.dev_account
    self.dev_inputIp = C_PlayerPrefsUtility.GetString(L_Const.prefsKey.localInputIp) or ""
    self.bind.dev_ipInput = self.dev_inputIp
    self.dev_inputPort = C_PlayerPrefsUtility.GetString(L_Const.prefsKey.localInputPort) or ""
    self.bind.dev_portInput = self.dev_inputPort
  end
end

function this:setIpInputShow(isOn)
  self.bind.ipBoxActive = isOn
  L_SettingStore:setPrefsInt(L_Const.prefsKey.selectIPButton, isOn and 1 or 0)
end

function this:setInterim(isOn)
  L_LoginManager.isOpenInterim = isOn
  L_SettingStore:setPrefsInt(L_Const.prefsKey.selectInterim, isOn and 1 or 0)
end

function this:refreshServerView(id, isSave)
  id = self.serverList[id] and id or 1
  self.selectedServerId = id
  local server = self.serverList[id]
  self.bind.txtEnterGameServerName = server and server.name or L_WordsTpl:getValue("ui_moduleLogin_01")
  if isSave ~= false then
    L_SettingStore:setLoginSave(self.selectedServerId, server and server.name or "")
  end
end

function this:refreshServerList()
  self.bind.serverList:clear()
  local array = {}
  for i, v in ipairs(self.serverList) do
    table.insert(array, {
      id = i,
      displayName = v.name
    })
  end
  self.bind.serverList:insert_array(array)
end

function this:tryShowNotice(force)
  local notice = L_NoticeStore:getLoginNotice()
  if notice == nil then
    return
  end
  if not force then
    local read = L_NoticeStore:isNoticeRead(notice.id)
    if read then
      return
    end
  end
  local data = {
    txtTitle = notice.title,
    txtContent = L_LoginPlatformManager:convertHtmlTag(notice.content),
    hideCancel = true,
    hideConfirm = true
  }
  L_UI:open("pageTextNoticeWindow", data)
  L_NoticeStore:setNoticeReadMark(notice.id, true, true)
end

function this:tryShowGameAnnounce()
  L_UI:open("pageAnnouncementLogin", {
    announcementShowType = L_Const.AnnouncementShowType.OnlySystem,
    fromLogin = true
  })
end

function this:startGame()
  print("startBtn 2")
  local ip, port = self:getIpAddress()
  if ip == nil then
    L_GameUtil.showCommonTip({
      txtContent = L_Lang:get(L_WordsTpl:getValue("ui_notice_server_not_open")),
      closeCallback = function()
      end,
      hideConfirm = true,
      hideCancel = true,
      overrideSortingOrder = 30000
    })
    return
  end
  print("startBtn 3")
  local data = {
    ip = ip,
    port = port,
    account = self.dev_account
  }
  print("startBtn 4")
  L_LoginManager:sdkLoginin(data)
end

function this:getIpAddress()
  local ip, port
  print("getIpAddress 1 ")
  if self.bind.ipBoxActive then
    print("getIpAddress 2 ")
    ip = self.dev_inputIp
    port = self.dev_inputPort
  else
    print("getIpAddress 3 " .. tostring(self.selectedServerId))
    local server = self.serverList[self.selectedServerId]
    if server then
      ip = server.addr
      port = server.port
      print("getIpAddress 4 " .. tostring(ip) .. "  " .. tostring(port))
    end
  end
  print("getIpAddress 5 ")
  return ip, tonumber(port)
end

return this
