
local NORMALCOLOR = "#000000"
local TITLECOLOR  = "#00E000"
local NEXTPAGETAG = "/br/"

local ImgWIdx = 3
local ImgHIdx = 4
local ImgSet = 3
local ImgGrp = 4

Notes = public()
NoteTab = public()
NoteEntry = public()

function A()
  d=Notes()

  -- d.Tabs["Diary"]:AddContent("i_batteries" ,true, "SmallItemsSet", "Items")  
  
  for i=1,13 do
    d.Tabs["Diary"]:AddContent(tostring(i).." #{sys_note_nn}")   
  end
  --d.Tabs["Diary"]:AddContent("i_batteries" ,true, "SmallItemsSet", "Items")  
  for i=14,45 do
    d.Tabs["Diary"]:AddContent(tostring(i).." #{sys_note_nn}")   
  end
  d.Tabs["Diary"]:AddContent("a")
  d.Tabs["Diary"]:AddContent("a\n")
  d.Tabs["Diary"]:AddContent("a\n")
  
  d:Open() 
end

function Notes:Create()
  self.Tabs = {Dialogs = NoteTab("Dialogs", self), Diary = NoteTab("Diary", self)}
  self.LastPage = { Dialogs = false, Diary = false}
  self.CurrentEntry = { Dialogs = 1, Diary = 1 }
  self.CurrentPage = { Dialogs = 0, Diary = 0 }
  self.CurrentTab = "Diary"
  self.Pages = {}
  
  self.Window = OpenNotes()
  self.Gui = Widgets.notes_window --_G["Widgets."..self.Window:GetName()]
  self.Gui.SetNotes(self)
  self:PrepareControls()
end

function Notes:Open()
  self:ShowPages()
  self.Window:SetVisible(true)
  UI:Helper("up", false) 
  UI:Helper("down", false) 
end

function Notes:Close()
  self.Window:SetVisible(false)
end

function Notes:ShowPages()
  
  local pageHeight = self.Gui.GetControl(self.Gui.Left):GetHeight()
  local Tab = self.Tabs[self.CurrentTab]
  local CTxt = {}
  local CImg = {}
  local i
  self:Clear()  
  i = self:SetPageText(pageHeight, self.Controls[1]["Txt"], Tab, self.CurrentEntry[self.CurrentTab],self.Controls[1]["Img"]) -- left page  
  i = self:SetPageText(pageHeight,  self.Controls[2]["Txt"], Tab, i, self.Controls[2]["Img"]) -- right page   
  return i
end

function Notes:NextPage()
  if self.LastPage[self.CurrentTab] then
    return
  end
  
  self.CurrentPage[self.CurrentTab] = self.CurrentPage[self.CurrentTab] + 1  
  if self.Pages[self.CurrentPage[self.CurrentTab]] == nil then
    self.Pages[self.CurrentPage[self.CurrentTab]] = {}
    self.Pages[self.CurrentPage[self.CurrentTab]] = self.CurrentEntry[self.CurrentTab]
  end
  
  self.CurrentEntry[self.CurrentTab] = self:ShowPages()
end

function Notes:PrevPage()
  if self.CurrentPage[self.CurrentTab] > 1 then
    self.CurrentPage[self.CurrentTab] = self.CurrentPage[self.CurrentTab] - 1
  else
    return
  end 
  
  self.CurrentEntry[self.CurrentTab] = self.Pages[self.CurrentPage[self.CurrentTab]]
  self:ShowPages()
end

function Notes:SetTab(tab)
  if self.Tabs[tab] ~= nil and self.Tabs[tab] ~= self.Tabs[self.CurrentTab] then
    self.CurrentTab = tab
    self:ShowPages()
  end
end


function Notes:SetPageText(pageHeight, CTxt, Tab, startEntry, CImg)
  local text = ""
  local finalText = ""
  local n = Tab:GetEntriesCount()
  local i = startEntry
  local txtSize = Ogre.Vector2()
  local leftSpace = pageHeight
  local lastTextHeight = 0
  
  local resetControls = function(tab)
    for k,v in pairs(tab) do
      if type(v) == "userdata" then
        v:SetVisible(false) 
        v:SetRealPos(v:GetRealX(), 0)
        v:SetSize(-1,0)
      end
    end
    tab.Iterator = 1
  end
  
  if i > n then
    return self.CurrentEntry[self.CurrentTab]
  end
  
  resetControls(CTxt)
  resetControls(CImg)
  position = 0  
  
  local ctrl = nil
  
  while (i <= n) 
  do    
    local entry = Tab:GetEntry(i)
    
    if entry[2] == true then -- img
      if CImg[CImg.Iterator] == nil then
        break
      end
      
      if ctrl == nil then
        position = CTxt[CTxt.Iterator]:GetY() + CTxt[CTxt.Iterator]:GetHeight()
      else
        position = ctrl:GetY() + ctrl:GetHeight()
      end
      
      CImg[CImg.Iterator]:SetPosition(CImg[CImg.Iterator]:GetX(), position)
      CImg[CImg.Iterator]:SetImage(entry[1], entry[ImgSet], entry[ImgGrp], true)
      
      if CImg[CImg.Iterator]:GetY() + CImg[CImg.Iterator]:GetHeight() > pageHeight then
        CImg[CImg.Iterator]:SetImage("","","")
        break
      end
      
      CImg[CImg.Iterator]:SetVisible(true)
      
      if ctrl == nil then
        CTxt[CTxt.Iterator]:SetVisible(true)
        CTxt[CTxt.Iterator]:SetText(finalText)
        finalText = ""
        CTxt.Iterator = CTxt.Iterator +1
      end
      
      ctrl =  CImg[CImg.Iterator]
      position = CImg[CImg.Iterator]:GetY() + CImg[CImg.Iterator]:GetHeight()
      CImg.Iterator = CImg.Iterator + 1
    else -- txt
      if CTxt[CTxt.Iterator] == nil then 
        break
      end
      ctrl = nil
      
      CTxt[CTxt.Iterator]:SetPosition(CTxt[CTxt.Iterator]:GetX(), position)
      text = finalText..entry[1]
      
      CTxt[CTxt.Iterator]:SetText(text)
      textSize = CTxt[CTxt.Iterator]:GetTextSize()
      
      if CTxt[CTxt.Iterator]:GetHeight() <= textSize.y then
        CTxt[CTxt.Iterator]:SetSize(CTxt[CTxt.Iterator]:GetWidth(), textSize.y)
      end
      
      if CTxt[CTxt.Iterator]:GetY() + CTxt[CTxt.Iterator]:GetHeight() < pageHeight then
        finalText = text
      else
        break
      end
      
    end
    i = i + 1
  end
  
  self.LastPage[self.CurrentTab] = (i > n)
  
  BreakPoint()
  if CTxt[CTxt.Iterator] ~= nil and finalText ~= "" then
    CTxt[CTxt.Iterator]:SetVisible(true)
    CTxt[CTxt.Iterator]:SetText(finalText)
  end
  
  return i
end

function Notes:AddToTable(tab, element)
  table.insert(tab, element)
end

function Notes:GetTableSize(tab)
  return #tab
end

function Notes:PrepareControls()
  self.Controls = {}
  self.Controls[1] = { Txt = {
      self.Gui.GetControl(self.Gui.Left, "Text1"),
      self.Gui.GetControl(self.Gui.Left, "Text2"),
      self.Gui.GetControl(self.Gui.Left, "Text3")
    },
    Img = {
      self.Gui.GetControl(self.Gui.Left, "Img1"),
      self.Gui.GetControl(self.Gui.Left, "Img2")
    }
  }
  self.Controls[2] = { Txt = {
      self.Gui.GetControl(self.Gui.Right, "Text1"),
      self.Gui.GetControl(self.Gui.Right, "Text2"),
      self.Gui.GetControl(self.Gui.Right, "Text3")
    },
    Img = {
      self.Gui.GetControl(self.Gui.Right, "Img1"),
      self.Gui.GetControl(self.Gui.Right, "Img2")
    }
  }  
end

function Notes:Clear()
  local T = function(tab)
    for i = 1, #tab do
      tab[i]:SetText("")
    end
  end
  local I = function(tab)
    for i = 1, #tab do
      tab[i]:SetImage("","","")
    end
  end
  T(self.Controls[1]["Txt"])
  T(self.Controls[2]["Txt"])
  I(self.Controls[1]["Img"])
  I(self.Controls[2]["Img"])
end

function Notes:Save(element)
end

function Notes:Load(element)
end
--[[   NoteTab  ]]--

function NoteTab:Create(title)
  self.Title = self[1]
  self.Notes = self[2]
  self.Content = {}
  self.CurrentEntry = 0
end

function NoteTab:GetTitle()
  return self.Title
end

function NoteTab:StartEntry()
  tab = self.Notes.CurrentTab
  self.Notes.SetTab(self.Title)
  while not self.Notes.LastPage[self.Title] do
    self.Notes.NextPage()
  end
end

function NoteTab:AddContent(content, isImg, imgSet, imgGrp)
  if isImg == nil then
    isImg = false
  end
  if not isImg then
    content = L10n:ExpandLine(content)
  end
  Notes:AddToTable(self.Content, {content, isImg, imgSet, imgGrp} )
end

function NoteTab:GetEntry(i)
  if i < 0 or i > self:GetEntriesCount() then
    return nil end
  return self.Content[i]
end

function NoteTab:GetEntriesCount()
  return #self.Content
end


