
dofile("diary_pages_description.lua")

local Diary
local LastTab = nil
local SecondBg = nil
local LastMovies = 0

local MoviesTab = {
  movie_1 = "cut_s01_intro",
  movie_2 = "cut_s01_crime_f",
  movie_3 = "cut_s01_trip_to_africa",
  movie_4 = "cut_s03_death_f",
  movie_5 = "cut_s03_trip_to_alexandria"
}

function OnCreate()
  Diary = MystDiary:GetInstance()
  UpdateRichText()
  Diary:SetTextMargin(0.06)
  Diary:AddTab("Diary")
  Diary:AddTab("Dialogs")
  
  SecondBg = self:FindChild("second_bg")
end

function UpdateRichText()
  Diary:AddWidget( self:FindChild("LeftPage") )
  Diary:AddWidget( self:FindChild("RightPage") )
end

--<Text FontName="dupa" Color="#FF0000" Align="Right">asd</Text>
--<Image Resource=\"\" Group=\"\" Name=\"\"/>
function SetTab(tab)
  LastTab = tab
  if LastTab == "Movies" then
    OnMoviesButton()
    return
  elseif tab == "Diary" then
    SetDiaryPage()
    return
  end
  self:FindChild("LeftPage"):SetVisible(true)
  self:FindChild("RightPage"):SetVisible(true)
  HideMovies()
  Diary:SetTab(tab)
  UpdateArrowsVisible()
end

 -- G U I --
function OnShow()
  -- music jurnal
  if Audio:GetMusic() ~= "menu" then
	 s.sys_game_music = Audio:GetMusic()
	  Audio:PauseAllSound()
	  PlayMusic("menu",1,0)
  end

  if states["sys_first_diary_open"] == 0 then
    ShowGameHint("game_hint_7")
    states["sys_first_diary_open"] = 1
  end

  s["sys_block_esc"] = 1
  --if LastTab == nil then LastTab = "Dialogs" end
  --Diary:SetTab(LastTab)
  if s.sys_diary_current_tab == 0 or s.sys_diary_current_tab == "" or s.sys_diary_current_tab == "Diary" then
    OnDiaryButton()
  elseif s.sys_diary_current_tab == "Dialogs" then
    OnDialogsButton()
  end
  SecondBg:SetVisible((s.progress_ethiopia == 1))
  UpdateArrowsVisible()
end

function OnHide()
  s["sys_block_esc"] = 0  
  -- return music game
  Audio:ResumeAllPausedSound()
  Audio:StopMusic("")
  PlayMusic(s.sys_game_music,1,0)
  UnsetDiary()
end

function OnMouseEnter(_sender) SetInterfaceCaption(L10n:ExpandString(_sender:GetCaption())) end
function OnMouseLeave(_sender) SetInterfaceCaption("") end

function  OnNextPage()   

  if s.sys_diary_current_tab == "Movies" then    return  end
  if s.sys_diary_current_tab == "Diary" then
    if s.sys_diary_diary_page + 1 >=  s.sys_diary_entry then return end
    s.sys_diary_diary_page = s.sys_diary_diary_page + 1
    SetDiaryPage()
    return
  end
  Diary:NextPage()
  UpdateArrowsVisible()
end

function  OnPrevPage()
  if s.sys_diary_current_tab == "Movies" then     return  end
  if s.sys_diary_current_tab == "Diary" then
    if s.sys_diary_diary_page <= 0  then return end
    s.sys_diary_diary_page = s.sys_diary_diary_page - 1
    SetDiaryPage()
    return
  end
  Diary:PrevPage()        
  UpdateArrowsVisible()
end

function UpdateArrowsVisible()

  if s.sys_diary_current_tab == "Diary" then
    self:FindChild("PrevPage"):SetVisible( s.sys_diary_diary_page > 0 )
    self:FindChild("NextPage"):SetVisible( s.sys_diary_diary_page + 1 < s.sys_diary_entry )
    
  elseif s.sys_diary_current_tab == "Dialogs" then
    self:FindChild("PrevPage"):SetVisible( not Diary:IsFirstPage() )
    self:FindChild("NextPage"):SetVisible( not Diary:IsLastPage() )

  elseif s.sys_diary_current_tab == "Movies" then
    self:FindChild("PrevPage"):SetVisible( false )
    self:FindChild("NextPage"):SetVisible( false )
    return
  end
  
  if self:FindChild("NextPage"):IsVisible() then
    self:FindChild("NextPage"):SetSkin("EmptyFrame")
    self:FindChild("NextPage"):SetSkin("notes_arrow_right")
    self:FindChild("NextPage"):SetText("sys_next_page")
  end
  
  if self:FindChild("PrevPage"):IsVisible() then
    self:FindChild("PrevPage"):SetSkin("EmptyFrame")
    self:FindChild("PrevPage"):SetSkin("notes_arrow_left")
    self:FindChild("PrevPage"):SetText("sys_prev_page")
  end
  
end

function  OnClose()         UnsetDiary() self:SetVisible(false)  end
function  OnDiaryButton()   SetDiary() end --SetTab("Diary")         end
function  OnDialogsButton() UnsetDiary() SetTab("Dialogs")  s.sys_diary_current_tab = "Dialogs"  UpdateBookmarksVisible()  UpdateArrowsVisible()  end

function PostLoad()
  UpdateRichText()
end

function UpdateBookmarksVisible()
  self:FindChild("Diary_"):SetVisible(  s.sys_diary_current_tab == "Diary"  )
  self:FindChild("dialogs_"):SetVisible(s.sys_diary_current_tab == "Dialogs")
  self:FindChild("Movies_"):SetVisible( s.sys_diary_current_tab == "Movies" )
end

function UnsetDiary()
--[[
  local res = {
    "page_1", "page_3", "page_4", "page_5", "page_6",
    "page_7", "page_8", "page_9", "page_10", "page_11", "page_12", 
    "page_13", "page_18", "page_18", "page_19", "page_20", "page_21",
    "page_22", "page_23","page_24", "page_25", "page_26", "page_14",
    "page_15", "page_16", "page_17"    
  }
  ]]
  self:FindChild("Diary_"):SetSkin("EmptyFrame")
  --[[
  for _,v in pairs(res) do
    UI:UnloadResource(v) 
  end
  ]]
end

function SetDiary()
  --UI:LoadResource("diary_pages.xml")
  s.sys_diary_current_tab = "Diary"
  UpdateBookmarksVisible()  
  --if DiaryPages[s.sys_diary_diary_page + 1] == nil then return end  
  SetDiaryPage()
end

function SetDiaryPage(p)
  if p == nil then p = s.sys_diary_diary_page + 1 end
  if p > s.sys_diary_entry then 
    self:FindChild("Diary_"):SetVisible(false)
    UpdateArrowsVisible()
    return 
  end
 local function setText(txt, name, t)
    local textSet = DiaryPages[t][name]
    local text = ""
    if textSet ~= nil then
      for i = 1,#textSet do text = text.."\n\t"..L10n:ExpandString(textSet[i]) end
      txt:SetText(text)
      txt:SetVisible(true)
    else
      txt:SetVisible(false)
    end
  end
 
  local img = self:FindChild("Diary_")
  local left  = self:FindChild("DiaryLeftPage")
  local right = self:FindChild("DiaryRightPage")
  
  setText(left, "left", p)
  setText(right, "right", p)
  
  if DiaryPages[p]["bg"] ~= nil then 
    img:SetSkin(DiaryPages[p]["bg"])
  else
    img:SetSkin("diary_page_empty")
  end
  UpdateArrowsVisible()
end

function SetDiaryTextAlign()
  local left  = self:FindChild("DiaryLeftPage")
  local right = self:FindChild("DiaryRightPage")
  
  if DiaryPages[s.sys_diary_diary_page + 1][3] == nil or DiaryPages[s.sys_diary_diary_page + 1][3] == "left" then
    left:SetVisible(true)
    right:SetVisible(false)
    return left
  else
    right:SetVisible(true)
    left:SetVisible(false)
    return right
  end  
end

local moviesCount = 5
function  OnMoviesButton(sender, start)
  UnsetDiary()
  LastTab = "Movies"
  s.sys_diary_current_tab = "Movies"
  UpdateBookmarksVisible()
  UpdateArrowsVisible()
  
  self:FindChild("LeftPage"):SetVisible(false)
  self:FindChild("RightPage"):SetVisible(false)
  
  HideMovies()
  local n = tonumber(states["sys_movies_played"])
  self:FindChild("Movies_"):SetVisible(true) 
  for i=1,moviesCount do 
    self:FindChild("movie_"..tostring(i)):SetVisible( i <= n)
  end
end

function OnMovieButton(_sender)   self:SetVisible(false)  Movie(MoviesTab[_sender:GetName()]) end
function OnMovieEnter(_sender)  end

function HideMovies()
  self:FindChild("Movies_"):SetVisible(false)
  for i=1,moviesCount do 
    self:FindChild("movie_"..tostring(i)):SetVisible(false)
  end
end

function AddEntry(n)
  s.sys_diary_entry = n
end 
