profile = {}
stats = {}
local g_logging_out = false
function new(self)
  return self
end
function init(self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_GAME_INITIALIZED, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_NEXT_LEVEL, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_ROVIO_ID_LOGIN_SUCCESS, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_ROVIO_PROFILE_TRY_LOGOUT, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_SYNC_SUCCESS, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_SYNC_FAILURE, self)
end
function update(self, dt, time)
end
function eventTriggered(self, event)
  if event.id == gamelua.events.EID_GAME_INITIALIZED then
    if gamelua.g_rovio_account_available then
      login(false, "sign_in")
    end
  elseif event.id == gamelua.events.EID_NEXT_LEVEL then
    if gamelua.g_rovio_account_available then
      gamelua.settingsWrapper:updateSyncInfoCount()
      local syncInfoCount = gamelua.settingsWrapper:getSyncInfoCount()
      local should_show_info = syncInfoCount == 1 or syncInfoCount % 5 == 0 and syncInfoCount <= 10
      if should_show_info and not gamelua.settingsWrapper:hasPlayerRegistered() then
        gamelua.notificationsFrame:addChild(SyncIntroductionFrame:new())
      end
    end
  elseif event.id == gamelua.events.EID_ROVIO_ID_LOGIN_SUCCESS then
    if gamelua.isPhysicsEnabled() and gamelua.menuManager and gamelua.menuManager:getRoot().returnToMenu then
      gamelua.setPhysicsEnabled(false)
      gamelua.setGameMode(function()
      end)
      gamelua.menuManager:getRoot():returnToMenu()
      gamelua.eventManager:notify({
        id = gamelua.events.EID_LEAVE_GAME,
        reason = "ROVIO_LOGIN"
      })
    end
    if event.promptLogin then
      gamelua.notificationsFrame:addChild(SignedInNotificationScreen:new({
        title = "TEXT_ROVIO_ID_SIGNED_IN_AS",
        content = profile.email
      }))
    end
  elseif event.id == gamelua.events.EID_SYNC_FAILURE and g_logging_out then
    g_logging_out = false
  elseif event.id == gamelua.events.EID_ROVIO_PROFILE_TRY_LOGOUT then
    gamelua.saveLuaFileWrapper("highscores.lua", "highscores", true)
    gamelua.saveLuaFileWrapper("settings.lua", "settings", true)
    g_logging_out = true
    showLoadScreen("logout")
  elseif event.id == gamelua.events.EID_SYNC_SUCCESS and g_logging_out then
    gamelua.logoutRovioId()
    g_logging_out = false
    gamelua.notifyEventManager(gamelua.events.EID_ROVIO_ID_LOGOUT)
  end
end
function showLoadScreen(initial_state)
  local function goto_connection()
    gamelua.notificationsFrame:addChild(RovioIdLoadingScreen:new({initialState = initial_state}))
  end
  gamelua.eventManager:notify({
    id = gamelua.events.EID_DO_LOADING,
    items = {
      function()
        gamelua.loadAssets({"ID"})
      end
    },
    completion_call = goto_connection
  })
end
function calculateStats()
  stats.totalScore = 0
  stats.totalStars = 0
  stats.maxStars = 0
  stats.totalFeathers = 0
  stats.maxFeathers = 0
  for level, _ in gamelua.allLevels() do
    if gamelua.starTable[level] and not _G.string.find(level, "LevelGE") then
      local highscores = gamelua.highscores[level]
      local starTable = gamelua.starTable[level]
      if highscores then
        stats.totalScore = stats.totalScore + highscores.score
        if highscores.eagleScoreMax and highscores.eagleScoreMax >= starTable.eagleScore then
          stats.totalFeathers = stats.totalFeathers + 1
        end
      end
      if highscores and highscores.completed and 0 < highscores.score then
        if highscores.score >= starTable.goldScore then
          stats.totalStars = stats.totalStars + 3
        elseif highscores.score >= starTable.silverScore then
          stats.totalStars = stats.totalStars + 2
        else
          stats.totalStars = stats.totalStars + 1
        end
      end
      if starTable.eagleScore then
        stats.maxFeathers = stats.maxFeathers + 1
      end
      stats.maxStars = stats.maxStars + 3
    end
  end
end
function login(showPrompt, initialScreen)
  gamelua.saveLuaFileWrapper("highscores.lua", "highscores", true)
  gamelua.saveLuaFileWrapper("settings.lua", "settings", true)
  gamelua.loginRovioId(showPrompt, initialScreen)
end
ui = gamelua.ui
RovioIdLoadingScreen = ui.Frame:inherit()
function RovioIdLoadingScreen:init()
  ui.Frame.init(self)
  local bg = ui.Rect:new()
  bg.name = "bg"
  bg.a = 0.75
  self:addChild(bg)
  local bird = ui.Image:new()
  bird.name = "bird"
  bird:setImage("RED_CONNECTING")
  self:addChild(bird)
  local eyebrows = ui.Image:new()
  eyebrows.name = "eyebrows"
  eyebrows:setImage("RED_BROW_1")
  bird:addChild(eyebrows)
  local phone = ui.Image:new()
  phone.name = "phone"
  phone:setImage("PHONE_CONNECTING")
  bird:addChild(phone)
  for i = 1, 5 do
    local dot = ui.Image:new()
    dot.name = "dot" .. i
    dot:setImage("CONNECTING_2")
    self:addChild(dot)
  end
  local globe = ui.Image:new()
  globe.name = "globe"
  globe:setImage("GLOBE")
  self:addChild(globe)
  local globe_shadow = ui.Image:new()
  globe_shadow.name = "globe_shadow"
  globe_shadow:setImage("GLOBE_SHADOW")
  globe:addChild(globe_shadow)
  local cancel_button = ui.ImageButton:new()
  cancel_button.name = "cancel_button"
  cancel_button.returnValue = ""
  cancel_button:setImage("MENU_NO")
  cancel_button.activateOnRelease = true
  self:addChild(cancel_button)
  local connect_error = ui.Image:new()
  connect_error.name = "connect_error"
  connect_error:setImage("NO_CONNECTION")
  connect_error:setVisible(false)
  self:addChild(connect_error)
end
function RovioIdLoadingScreen:onEntry()
  ui.Frame.onEntry(self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_ROVIO_ID_SHOWING_LOGIN_VIEW, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_ROVIO_ID_LOGIN_LOAD_FAILURE, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_ROVIO_ID_LOGIN_SUCCESS, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_SYNC_SUCCESS, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_SYNC_FAILURE, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_ROVIO_PROFILE_LOAD_SUCCESS, self)
  gamelua.eventManager:addEventListener(gamelua.events.EID_ROVIO_PROFILE_LOAD_FAILURE, self)
  if self.initialState == "sign_up" or self.initialState == "sign_in" then
    login(true, self.initialState)
    self:getChild("cancel_button").returnValue = "CANCEL_ROVIO_ID_LOAD"
  elseif self.initialState == "profile" then
    calculateStats()
    gamelua.showProfileView()
    self:getChild("cancel_button").returnValue = "CANCEL_ROVIO_PROFILE_LOAD"
  elseif self.initialState == "logout" then
    gamelua.notifyEventManager(gamelua.events.EID_START_CLOUD_SYNC)
    self:getChild("cancel_button").returnValue = "CANCEL_ROVIO_LOGOUT"
  end
end
function RovioIdLoadingScreen:onExit()
  ui.Frame.onExit(self)
  gamelua.eventManager:removeEventListener(gamelua.events.EID_ROVIO_ID_SHOWING_LOGIN_VIEW, self)
  gamelua.eventManager:removeEventListener(gamelua.events.EID_ROVIO_ID_LOGIN_LOAD_FAILURE, self)
  gamelua.eventManager:removeEventListener(gamelua.events.EID_ROVIO_ID_LOGIN_SUCCESS, self)
  gamelua.eventManager:removeEventListener(gamelua.events.EID_SYNC_SUCCESS, self)
  gamelua.eventManager:removeEventListener(gamelua.events.EID_SYNC_FAILURE, self)
  gamelua.releaseImages({"ID"})
end
function RovioIdLoadingScreen:layout()
  ui.Frame.layout(self)
  local bg = self:getChild("bg")
  bg.x = 0
  bg.y = 0
  bg.width = gamelua.screenWidth
  bg.height = gamelua.screenHeight
  local bird = self:getChild("bird")
  bird.x = gamelua.screenWidth * 0.25
  bird.y = gamelua.screenHeight * 0.5
  local phone = bird:getChild("phone")
  phone.x = bird.w * 0.5
  local globe = self:getChild("globe")
  globe.x = gamelua.screenWidth * 0.75
  globe.y = bird.y
  local globe_shadow = globe:getChild("globe_shadow")
  globe_shadow.y = globe.h * 0.7
  for i = 1, 5 do
    local distance = (globe.x - globe.w / 2 - (bird.x + bird.w / 2)) / 6
    local dot = self:getChild("dot" .. i)
    dot.x = bird.x + bird.w / 2 + distance * i
    dot.y = bird.y
  end
  local cancel_button = self:getChild("cancel_button")
  cancel_button.x = cancel_button.w / 2
  cancel_button.y = gamelua.screenHeight - cancel_button.h / 2
  local connect_error = self:getChild("connect_error")
  connect_error.x = gamelua.screenWidth / 2
  connect_error.y = bird.y
end
function RovioIdLoadingScreen:update(dt, time)
  ui.Frame.update(self, dt, time)
  for i = 1, 5 do
    local dot = self:getChild("dot" .. i)
    if i == _G.math.floor(time) % 5 + 1 then
      dot:setImage("CONNECTING_1")
    else
      dot:setImage("CONNECTING_2")
    end
  end
end
function RovioIdLoadingScreen:eventTriggered(event)
  if event.id == gamelua.events.EID_ROVIO_ID_SHOWING_LOGIN_VIEW or event.id == gamelua.events.EID_ROVIO_ID_LOGIN_SUCCESS or event.id == gamelua.events.EID_SYNC_SUCCESS or event.id == gamelua.events.EID_ROVIO_PROFILE_LOAD_SUCCESS then
    gamelua.notificationsFrame:removeChild(self)
  elseif event.id == gamelua.events.EID_ROVIO_ID_LOGIN_LOAD_FAILURE or event.id == gamelua.events.EID_SYNC_FAILURE or event.id == gamelua.events.EID_ROVIO_PROFILE_LOAD_FAILURE then
    for i = 1, 5 do
      local dot = self:getChild("dot" .. i)
      dot:setVisible(false)
    end
    local connect_error = self:getChild("connect_error")
    connect_error:setVisible(true)
    local eyebrows = self:getChild("bird"):getChild("eyebrows")
    eyebrows:setImage("RED_BROW_2")
    local cancel_button = self:getChild("cancel_button")
    cancel_button:setImage("MENU_YES")
    cancel_button.returnValue = "CLOSE"
  end
end
function RovioIdLoadingScreen:onPointerEvent(eventType, x, y)
  local result, meta, item = ui.Frame.onPointerEvent(self, eventType, x, y)
  if result == "CANCEL_ROVIO_ID_LOAD" then
    gamelua.notificationsFrame:removeChild(self)
    gamelua.cancelRovioIdLogin()
  elseif result == "CANCEL_ROVIO_PROFILE_LOAD" then
    gamelua.notificationsFrame:removeChild(self)
    gamelua.cancelProfileLoading()
  elseif result == "CANCEL_ROVIO_LOGOUT" then
    gamelua.notificationsFrame:removeChild(self)
    g_logging_out = false
  elseif result == "CLOSE" then
    gamelua.notificationsFrame:removeChild(self)
  end
  return "BLOCK", nil, nil
end
SyncPopupPrompt = ui.Frame:inherit()
function SyncPopupPrompt:init()
  ui.Frame.init(self)
  local background = ui.Image:new()
  background.name = "background"
  background:setImage("ID_BG")
  background.scaleX = gamelua.g_cutscene_asset_scale
  background.scaleY = gamelua.g_cutscene_asset_scale
  self:addChild(background)
  local signup_background = ui.Image:new()
  signup_background.name = "signup_background"
  signup_background:setImage("COMPO")
  self:addChild(signup_background)
end
function SyncPopupPrompt:onEntry()
  ui.Frame.onEntry(self)
  gamelua.loadAssets({"ID"})
end
function SyncPopupPrompt:layout()
  ui.Frame.layout(self)
  local background = self:getChild("background")
  background.x = 0
  background.y = 0
  local signup_background = self:getChild("signup_background")
  signup_background.x = gamelua.screenWidth / 2
  signup_background.y = gamelua.screenHeight * 0.45
end
function SyncPopupPrompt:update(dt, time)
  ui.Frame.update(self, dt, time)
end
SyncIntroductionFrame = SyncPopupPrompt:inherit()
function SyncIntroductionFrame:init()
  SyncPopupPrompt.init(self)
  local signup_background = self:getChild("signup_background")
  local introduction_text = ui.Image:new()
  introduction_text.name = "introduction_text"
  introduction_text:setImage("SIGN_UP_IT")
  signup_background:addChild(introduction_text)
  local register_button = ui.BackgroundBox:new()
  register_button.name = "register_button"
  register_button.components = {
    left = "BTN_AGREE_LEFT",
    center = "BTN_AGREE",
    right = "BTN_AGREE_RIGHT"
  }
  signup_background:addChild(register_button)
  local register_text = ui.Image:new()
  register_text.name = "register_text"
  register_text:setImage("SIGN_UP_EN")
  register_button:addChild(register_text)
  local register_invisible = ui.InvisibleButton:new()
  register_invisible.name = "register_invisible"
  register_invisible.activateOnRelease = true
  register_invisible.returnValue = "REGISTER"
  register_button:addChild(register_invisible)
  local signin_button = ui.ImageButton:new()
  signin_button.name = "signin_button"
  signin_button:setImage("SIGN_IN_EN")
  signin_button.activateOnRelease = true
  signin_button.returnValue = "SIGNIN"
  signup_background:addChild(signin_button)
  local signin_text = ui.Image:new()
  signin_text.name = "signin_text"
  signin_text:setImage("SIGN_IN_EN")
  signin_button:addChild(signin_text)
  local close_button = ui.ImageButton:new()
  close_button.name = "close_button"
  close_button.returnValue = "CLOSE"
  close_button.activateOnRelease = true
  close_button:setImage("ARROW_LEFT")
  self:addChild(close_button)
  self.available_localizations = {
    "EN",
    "FR",
    "IT",
    "DE",
    "ES",
    "JA",
    "CN",
    "TW"
  }
  self:setLocalizations()
end
function SyncIntroductionFrame:setLocalizations()
  local localization_language = "EN"
  local current_language = _G.res.getLocale():sub(4, 5)
  for _, v in _G.ipairs(self.available_localizations) do
    if current_language == v then
      localization_language = v
    end
  end
  local register_text = self:getChild("register_text")
  local signin_text = self:getChild("signin_text")
  local introduction_text = self:getChild("introduction_text")
  introduction_text:setImage("ID_TEXT_" .. localization_language)
  register_text:setImage("SIGN_UP_" .. localization_language)
  signin_text:setImage("SIGN_IN_" .. localization_language)
end
function SyncIntroductionFrame:layout()
  SyncPopupPrompt.layout(self)
  self:setLocalizations()
  local signup_background = self:getChild("signup_background")
  local introduction_text = signup_background:getChild("introduction_text")
  introduction_text.y = -signup_background.h * 0.1
  local register_button = signup_background:getChild("register_button")
  local register_text = register_button:getChild("register_text")
  local register_invisible = register_button:getChild("register_invisible")
  local _, button_height = _G.res.getSpriteBounds("BTN_AGREE")
  register_button.width = introduction_text.w * 0.9
  register_button.height = button_height
  register_button.x = 0
  register_button.y = signup_background.h * 0.1
  register_invisible.width = register_button.width
  register_invisible.height = register_button.height
  local signin_button = signup_background:getChild("signin_button")
  local signin_text = signin_button:getChild("signin_text")
  signin_button.y = register_button.y + register_button.height / 2 + signin_button.h / 2
  local close_button = self:getChild("close_button")
  close_button.x = close_button.w * 0.5
  close_button.y = gamelua.screenHeight - close_button.h * 0.5
end
function SyncIntroductionFrame:update(dt, time)
  SyncPopupPrompt.update(self, dt, time)
  local currentRoot = gamelua.menuManager:getRoot()
  if currentRoot.name == "gameHud" then
    local pausePage = currentRoot:getChild("pausePage")
    if pausePage ~= nil then
      pausePage.news_disabled = true
      pausePage.rovio_news:setVisible(false)
    end
  end
  if gamelua.isPhysicsEnabled() then
    gamelua.setPhysicsEnabled(false)
    gamelua.setGameMode(function()
    end)
  end
end
function SyncIntroductionFrame:onPointerEvent(eventType, x, y)
  local result, meta, item = ui.Frame.onPointerEvent(self, eventType, x, y)
  if result == "CLOSE" then
    gamelua.releaseAssets({"ID"})
    self:closeFrame()
  elseif result == "REGISTER" then
    showLoadScreen("sign_up")
    self:closeFrame()
  elseif result == "SIGNIN" then
    showLoadScreen("sign_in")
    self:closeFrame()
  end
  return "BLOCK", nil, self
end
function SyncIntroductionFrame:closeFrame()
  local currentRoot = gamelua.menuManager:getRoot()
  local currentlyInGame = currentRoot.name == "gameHud"
  local currentlyInPauseMenu = currentRoot:getChild("pausePage") ~= nil and currentRoot:getChild("pausePage").visible == true
  if currentlyInGame and currentlyInPauseMenu then
    currentRoot:getChild("pausePage").news_disabled = false
  elseif currentlyInGame then
    gamelua.setPhysicsEnabled(true)
    gamelua.setGameMode(gamelua.updateGame)
  end
  gamelua.notificationsFrame:removeChild(self)
end
SyncRewardFrame = SyncPopupPrompt:inherit()
function SyncRewardFrame:init()
  SyncPopupPrompt.init(self)
  local signup_background = self:getChild("signup_background")
  local reward_text = ui.Text:new()
  reward_text.name = "reward_text"
  reward_text.text = [[
Wohoo, registration successful
Here you go: a power-up.PLACEHOLDER]]
  reward_text:setFont(gamelua.getFontBasic)
  signup_background:addChild(reward_text)
  local close_button = ui.ImageButton:new()
  close_button.name = "close_button"
  close_button.returnValue = "CLOSE"
  close_button.activateOnRelease = true
  close_button:setImage("MENU_YES")
  self:addChild(close_button)
end
function SyncRewardFrame:layout()
  SyncPopupPrompt.layout(self)
  local signup_background = self:getChild("signup_background")
  local reward_text = signup_background:getChild("reward_text")
  reward_text.textBoxSize = signup_background.w * 0.75
  reward_text:clip()
  reward_text.y = -signup_background.h / 2 + signup_background.h / 3 + reward_text:getHeight() / 2
  local close_button = self:getChild("close_button")
  close_button.x = signup_background.x + signup_background.w / 2.2
  close_button.y = signup_background.y + signup_background.h / 3
end
function SyncRewardFrame:onPointerEvent(eventType, x, y)
  local result, meta, item = ui.Frame.onPointerEvent(self, eventType, x, y)
  if result == "CLOSE" then
    gamelua.releaseAssets({"ID"})
    self:closeFrame()
  end
  return "BLOCK", nil, self
end
function SyncRewardFrame:closeFrame()
  gamelua.notificationsFrame:removeChild(self)
end
SignedInNotificationScreen = ui.Frame:inherit()
function SignedInNotificationScreen:init()
  ui.Frame.init(self)
  self.lifeTime = self.lifeTime or 4
  self.title = self.title or "You are signed in as"
  self.content = self.content or ""
  self.animation = 0
  self.animation_duration = self.lifeTime
  self.entry_duration = self.animation_duration / 3
  self.exit_duration = self.animation_duration / 3
  self.width = gamelua.screenWidth
  self.height = gamelua.screenHeight
  self.background_target_alpha = 0.75
  local background = ui.Rect:new()
  background.name = "background"
  background.a = 0
  background.width = gamelua.screenWidth
  background.height = gamelua.screenHeight
  self:addChild(background)
  local bgbox = ui.BGBox:new()
  bgbox.name = "bgbox"
  bgbox.hanchor = "HCENTER"
  bgbox.vanchor = "TOP"
  self:addChild(bgbox)
  local titleText = ui.Text:new()
  titleText.name = "titleText"
  titleText.text = self.title
  titleText:setFont(gamelua.getFontBasic)
  bgbox:addChild(titleText)
  local contentText = ui.Text:new()
  contentText.name = "contentText"
  contentText.text = self.content
  contentText:setFont(gamelua.getFontBasic)
  bgbox:addChild(contentText)
end
function SignedInNotificationScreen:onEntry()
  gamelua.ui.Frame.onEntry(self)
end
function SignedInNotificationScreen:update(dt, time)
  ui.Frame.update(self, dt, time)
  self.lifeTime = self.lifeTime - dt
  self.animation = self.animation + dt
  self:updateDialogPosition()
  if self.lifeTime < 0 then
    self:dismiss()
  end
  local background = self:getChild("background")
  if self.animation < self.entry_duration then
    background.a = _G.math.min(background.a + dt * 2, self.background_target_alpha)
  elseif self.animation > self.animation_duration - self.exit_duration / 4 then
    background.a = background.a - dt * 2
    if 0 > background.a then
      background.a = 0
    end
  end
end
function SignedInNotificationScreen:updateDialogPosition()
  local bgbox = self:getChild("bgbox")
  if self.animation < self.entry_duration then
    bgbox.x = gamelua.tweenEaseCubicOut(self.animation, gamelua.screenWidth + self.width * 1.5, gamelua.screenWidth * -0.5 - self.width * 1.5, self.entry_duration)
  elseif self.animation < self.animation_duration - self.exit_duration then
    bgbox.x = gamelua.screenWidth * 0.5
  elseif self.animation <= self.animation_duration then
    local exit_time = self.animation - (self.animation_duration - self.exit_duration)
    bgbox.x = gamelua.tweenEaseCubicIn(exit_time, gamelua.screenWidth * 0.5, gamelua.screenWidth * -0.5 - self.width * 1.5, self.exit_duration)
  end
end
function SignedInNotificationScreen:layout()
  gamelua.ui.Frame.layout(self)
  local bgbox = self:getChild("bgbox")
  bgbox.y = gamelua.screenHeight / 2
  local titleText = self:getChild("titleText")
  local contentText = self:getChild("contentText")
  local width = 0.8 * gamelua.screenWidth
  local height = titleText:getHeight() + contentText:getHeight()
  bgbox.width = width
  bgbox.height = height
  titleText.y = titleText:getHeight() / 2
  contentText.y = titleText.y + titleText:getHeight()
  self:updateDialogPosition()
end
function SignedInNotificationScreen:onPointerEvent(eventType, x, y)
  local result, meta, item = ui.Frame.onPointerEvent(self, eventType, x, y)
  if eventType == "LRELEASE" then
    self:dismiss()
  end
  return "BLOCK", nil, self
end
function SignedInNotificationScreen:dismiss()
  gamelua.notificationsFrame:removeChild(self)
end
filename = "RovioAccount.lua"
