local DialogsWindow = nil
local CurrentDialog = nil
local CurrentSubjects = {}

local ForceShowGui = false

function ShowDialogGui(show)
  ForceShowGui = show
  if show then
  else    
  end  
end

local function IsHero(actorName)
  local heroNames     = Config["all_hero_short_names"]
  local GameHeroNames = heroNames:split(",")
  for i =1,#GameHeroNames do
    if GameHeroNames[i] == actorName then
      return true
    end
  end
  return false
end

local function IsComment(dlg)
  local dlgName = dlg:GetName()
  local dlgType = string.sub(dlgName, 0, 3)
  if dlgType == "com" or dlgType == "abt" then  
    return true
  end
  return false
end

--AddToDiary("<Text Align=\"Right\" Color=\"" .. Config["diary_character_name_color"].."\">"..L10n:ExpandString(states["ch_hero_name"]).."</Text>", "Dialogs")
local function StartDialogLog(dlg)
  if IsComment(dlg) then return end
  str = "<Text Align = \"Center\" Color=\"" .. Config["diary_dialog_name_color"].."\">"..L10n:ExpandString(dlg:GetName()) .. "</Text>"
  AddToDiary(str, "Dialogs")
end

local function StopDialogLog(dlg)
  AddToDiary("<Text>BreakLine</Text>", "Dialogs")
end

local function AddDialogLog(line, character)
  local charName 
  if IsHero(character) then
    charName = L10n:ExpandString(states["ch_hero_name"])
  else
    charName = L10n:ExpandString(states["ch_"..character.."_name"])
  end
  if charName ~= 0 then
    AddToDiary("<Text Align=\"Center\" Color=\"" .. Config["diary_character_name_color"].."\">"..charName.."</Text>", "Dialogs")
  end
  AddToDiary("<Text>"..line.."</Text>", "Dialogs")  
end

function Talk(line, character, dlg)
  local lAvatar = ""
  local rAvatar = ""
  
  Widgets.DialogsWindow.SetBg(IsComment(dlg))
  Widgets.DialogsWindow.ShowLine(line)
  if  IsComment(dlg) then
    lAvatar = ""
    rAvatar = ""
  else
    if IsHero(character) then
      lAvatar = "ch_"..s.hero_short_name
    else
      rAvatar = "ch_"..character
    end
  end  
  Widgets.DialogsWindow.SetAvatar(1, lAvatar)
  Widgets.DialogsWindow.SetAvatar(2, rAvatar)  
  if not IsComment(dlg) then 
    AddDialogLog(line, character)
  end
end

function StopLine()
  if CurrentDialog then
    MystDialog:BreakLine()
  end
end

function StopTalkLine()
  Widgets.DialogsWindow.ShowLine("")
end

function OnDialogEnd(dlgName, dlg)
  if not ForceShowGui then
    ShowGui(false)
  end
  CurrentDialog = nil
  if not IsComment(dlg) then 
    StopDialogLog(dlg)
  end
end

function OnDialogStart(dlg)
  CreateGui()
  CurrentDialog = dlg
  ShowSubjects()
  ShowGui(true)
  
  StartDialogLog(dlg)
  
end

function OnSubjectEnd(subjectName)
  CurrentSubjects = {}
  ShowSubjects()
end

function OnSubjectStart()
  Widgets.DialogsWindow.ShowSubjects(false)  
end

function CreateGui()
  --dialogs_window.layout
  if not DialogsWindow then
    UI:AddWidget("dialogs_window.layout")
    DialogsWindow = Widget("DialogsWindow")
  end
end

function ShowGui(show)
  DialogsWindow:SetVisible(show)
  if show then
    Widgets.Inventory.Lock(false)
  else
    if s.sys_ho_active == 0 or s.sys_ho_inventory == 1 then
      Widgets.Inventory.Unlock() end
  end
end

function ShowSubjects()
  local subjects = {}
  local i =1
  
  Widgets.DialogsWindow.ClearSubjects()
  subjects[i] = CurrentDialog:GetFirstSubject()
  while ( subjects[i] ~= nil ) do
    Widgets.DialogsWindow.AddSubject(subjects[i]:GetName())
    CurrentSubjects[subjects[i]:GetName()] = subjects[i]
    i = i + 1
    subjects[i] = CurrentDialog:GetNextSubject()
  end
  Widgets.DialogsWindow.ShowSubjects(true)
end

function StartSubject(subject)
  CurrentDialog:StartSubject(CurrentSubjects[subject]:GetLabel())
end
