--constants
local BUTTON_HEIGHT = 0.2
local BUTTON_WIDTH  = 1


local dynamicButtons = {}
local ButtonsCount = 0

local TalkWidget = nil
local TalkWidgetBg = nil
local SubjectFrame = nil
local AvatarsWidgets = {}

local AvatarsImageSet = "Avatars"
local AvatarsImageGroup = "images"

function OnCreate()
  TalkWidget = Widget("TalkArea")
  TalkWidgetBg = Widget("TalkArea_BG")
  SubjectFrame = Widget("SubjectsFrame")
  AvatarsWidgets[1] = StaticImage( self:FindChild("left_avatar") )
  AvatarsWidgets[2] = StaticImage( self:FindChild("right_avatar") )
end

function OnHide()
  AvatarsWidgets[2]:SetImage("", AvatarsImageSet, AvatarsImageGroup)
end




function AddSubject(subject)
  dynamicButtons[subject] = SubjectFrame:AddButton(subject, "dlg_subject_button", 0, BUTTON_HEIGHT * ButtonsCount, BUTTON_WIDTH, BUTTON_HEIGHT)
  local txt = MystLocaleManager:ExpandString(subject)
  dynamicButtons[subject]:SetText(txt)
  dynamicButtons[subject]:AddEventCallback(EVT_MOUSECLICK, "OnSubjectClick")
  ButtonsCount = ButtonsCount + 1
end

function ClearSubjects()
  ButtonsCount = 0
  for k,v in pairs(dynamicButtons) do
    v:Destroy()
    dynamicButtons[k] = nil
  end
end

function ShowSubjects(show)
  if show == nil then
    show = true
  end
  SubjectFrame:SetVisible(show)
  TalkWidgetBg:SetVisible(false)
end

function OnSubjectClick(_sender)
  Dialogs.StartSubject(_sender:GetName())
end

function SetBg(comment)
  --local comBg = self:FindChild("SubjectsFrame")
  local dlgBg = self:FindChild(("SubjectsFrame_dialog"))
  --comBg:SetVisible(comment)
  dlgBg:SetVisible(not comment)
end

function ShowLine(line)
  if not TalkWidgetBg:IsVisible() then
    TalkWidgetBg:SetVisible(true)
  end
  TalkWidget:SetText( line )
end

function BreakDialogLine(_sender)
  Dialogs.StopLine()
end

function SetAvatar( pos, name)
    AvatarsWidgets[pos]:SetImage(name, AvatarsImageSet, AvatarsImageGroup)
end

