--------------------------------------------------------------------------------
--- Globals
--------------------------------------------------------------------------------
-- The Window
Global("gwtSettings", View.Settings)
Global("gwtPageContainer", gwtSettings.DockPanel.PageContainer)
gwtPageContainer.widget:SetBackgroundColor({r = 1.0; g = 1.0; b = 1.0; a = 0.1})
Global("gwtPositionGrid", gwtPageContainer.PositionGrid)
Global("gwtConditionGrid", gwtPageContainer.ConditionGrid)
Global("gwtExtendedConditions", gwtPageContainer.extendedConditions)
Global("gwtImportExport", gwtPageContainer.ImportExportGrid)

-- The Bottom Buttons
Global("gwtBottomButtons", gwtSettings.DockPanel.BottomButtons)
Global("gwtPageNavigation", gwtSettings.DockPanel.PageNavigation)
Global("gwtPrevPage", gwtPageNavigation.PrevPage)
Global("gwtNextPage", gwtPageNavigation.NextPage)
Global("gwtPageTitel", gwtPageNavigation.Titel)
Global("gwtSettingsClose", gwtBottomButtons.Close)
Global("gwtSettingsSave", gwtBottomButtons.Save)

-- Picture View
Global("gwtPictureView", gwtPositionGrid.PictureView)
Global("gwtPrevPicture", gwtPositionGrid.Prev)
Global("gwtNextPicture", gwtPositionGrid.Next)

Global("gwtUseBuffImage", gwtPositionGrid.useBuffCheckBox)
Global("gwtShowTimer", gwtPositionGrid.showTimerCheckBox)
Global("gwtTimerFormat", gwtPositionGrid.timerFormat)
Global("gwtShowBar", gwtPositionGrid.showBarCheckBox)
Global("gwtShowImage", gwtPositionGrid.showImageCheckBox)

-- Settings Preview
Global("gwtSettingsPreview", View.SettingsPreview)

-- Picture display condition
Global("gwtItemNameLabel", gwtConditionGrid.itemNameLabel)
Global("gwtItemName", gwtConditionGrid.itemName)

Global("gwtShow", gwtConditionGrid.show)
Global("gwtHide", gwtConditionGrid.hide)

Global("gwtWhile", gwtConditionGrid["while"])

Global("gwtBuff", gwtConditionGrid.buff)
Global("gwtDebuff", gwtConditionGrid.debuff)
Global("gwtSpellCD", gwtConditionGrid.spellCD)
Global("gwtItemCD", gwtConditionGrid.itemCD)

Global("gwtWithExactName", gwtConditionGrid.withExactName)
Global("gwtName", gwtConditionGrid.name)
Global("gwtNameSearchButton", gwtConditionGrid.nameSearch)
Global("gwtOn", gwtConditionGrid.on)

Global("gwtYou", gwtConditionGrid.you)
Global("gwtYourTarget", gwtConditionGrid.target)
Global("gwtTargetOfTarget", gwtConditionGrid.targetOfTarget)
Global("gwtPet", gwtConditionGrid.pet)
Global("gwtAnyUnit", gwtConditionGrid.anyUnit)

-- Extended Conditions
Global("gwtIfGroupMemberNumber", gwtExtendedConditions.ifGroupMemberNumber)
Global("gwtGroupMemberNumber", gwtExtendedConditions.groupMemberNumber)
Global("gwtCastedByMe", gwtExtendedConditions.castedByMe)
Global("gwtNotInFight", gwtExtendedConditions.notInFight)
Global("gwtInFight", gwtExtendedConditions.inFight)
Global("gwtSolo", gwtExtendedConditions.solo)
Global("gwtInGroup", gwtExtendedConditions.inGroup)
Global("gwtInRaid", gwtExtendedConditions.inRaid)
Global("gwtNotMounted", gwtExtendedConditions.notMounted)
Global("gwtMounted", gwtExtendedConditions.mounted)
Global("gwtHostile", gwtExtendedConditions.hostile)
Global("gwtFriendly", gwtExtendedConditions.friendly)
Global("gwtIfOtherVisible", gwtExtendedConditions.ifOtherVisible)
Global("gwtOtherRule", gwtExtendedConditions.otherrule)
Global("gwtStackMin", gwtExtendedConditions.stackmin)
Global("gwtStackMinEdit", gwtExtendedConditions.stackminedit)
Global("gwtStackMax", gwtExtendedConditions.stackmax)
Global("gwtStackMaxEdit", gwtExtendedConditions.stackmaxedit)
Global("gwtTimerMax", gwtExtendedConditions.timermax)
Global("gwtTimerMaxEdit", gwtExtendedConditions.timermaxedit)
Global("gwtTimerMin", gwtExtendedConditions.timermin)
Global("gwtTimerMinEdit", gwtExtendedConditions.timerminedit)
Global("gwtTimerElapsed", gwtExtendedConditions.timerelapsed)
Global("gwtTimerRemaining", gwtExtendedConditions.timerremaining)

-- Picture Position, Size and Opacity
Global("gwtPositionX", gwtPositionGrid.positionX)
Global("gwtPositionY", gwtPositionGrid.positionY)
Global("gwtSizeX", gwtPositionGrid.sizeX)
Global("gwtSizeY", gwtPositionGrid.sizeY)

Global("gwtOpacityLabel", gwtPositionGrid.opacityLabel_text)
Global("gwtOpacity", gwtPositionGrid.opacity_text)
Global("gwtOpacityIncrease", gwtPositionGrid.OpacityIncrease)
Global("gwtOpacityDecrease", gwtPositionGrid.OpacityDecrease)

-- Export & Import
Global("gwtCode", gwtImportExport.code)
Global("gwtImport", gwtImportExport.importButton)
Global("gwtExport", gwtImportExport.exportButton)

-- Contains the List of the Picture which you can select in the settings
Global("pictureList", { })
Global("currentPicture", 1)
Global("currentOpacity", 1.0)
Global("currentName", common.GetEmptyWString())
Global("currentAuraData", nil)
--------------------------------------------------------------------------------
--- Reactions
--------------------------------------------------------------------------------
onReaction[gwtSettingsClose.reactionName] = function(params)
  gwtSettings:show(false)
  gwtSettingsPreview:show(false)
  gwtSearchWindow:show(false)
  SetOpacity(1.0)
  UpdateSettingsPreview()
  ShowAurasWindow()  
end

onReaction[gwtSettingsSave.reactionName] = function(params)
  SaveSettings(currentAuraData)
  if currentAuraData then
    currentAuraData.isAbort = false
  end
  gwtSettings:show(false)
  gwtSettingsPreview:show(false)
  gwtSearchWindow:show(false)
  SetOpacity(1.0)
  UpdateSettingsPreview()
  ShowAurasWindow()
end

onReaction[gwtPrevPicture.reactionName] = function(params)
  LoadImage(currentPicture - 1)
  UpdateButtons()
  UpdateSettingsPreview()
end

onReaction[gwtNextPicture.reactionName] = function(params)
  LoadImage(currentPicture + 1)
  UpdateButtons()
  UpdateSettingsPreview()
end

onReaction[gwtName.reactionEnter] = function(params)
  currentName=gwtName:setFocus(false)

end

onReaction[gwtName.reactionChanged] = function(params)
  if gwtUseBuffImage:getChecked() then
    UpdateSettingsPreview()
    LoadImage(currentPicture)
  end
end

onReaction[gwtNameSearchButton.reactionName] = function(params)
  if gwtBuff:getChecked() or gwtDebuff:getChecked() then
    OpenSearch(knownBuffs)
  elseif gwtSpellCD:getChecked() then
    local spellList = { }
    for k, id in avatar.GetSpellBook() do
      local spellInfo = avatar.GetSpellInfo(id)
      spellList[spellInfo.name] = spellInfo.texture
    end
    OpenSearch(spellList)
  elseif gwtItemCD:getChecked() then
    local itemList = { }
    for k, itemId in avatar.GetInventoryItemIds() do
      if itemId then
        local itemInfo = avatar.GetItemInfo(itemId)
        if itemInfo then
          itemList[itemInfo.name] = itemInfo.icon
        end
      end
    end
    for k, itemId in unit.GetEquipmentItemIds(avatar.GetId(), ITEM_CONT_EQUIPMENT) do
      if itemId then
        local itemInfo = avatar.GetItemInfo(itemId)
        if itemInfo then
          itemList[itemInfo.name] = itemInfo.icon
        end
      end
    end
    OpenSearch(itemList)
  end
end

onReaction[gwtPositionX.reactionChanged] = function(params)  
  UpdateSettingsPreview()
end

onReaction[gwtOpacityIncrease.reactionName] = function(params)
  local value = GetOpacity()
  value = value + 0.05
  if value > 1.0 then
    value = 1.0
  elseif value < 0.0 then
    value = 0.0
  end
  SetOpacity(value)
  UpdateSettingsPreview()
end
onReaction[gwtOpacityDecrease.reactionName] = function(params)
  local value = GetOpacity()
  value = value - 0.05
  if value > 1.0 then
    value = 1.0
  elseif value < 0.0 then
    value = 0.0
  end
  SetOpacity(value)
  UpdateSettingsPreview()
end

onReaction[gwtTimerFormat.reactionChanged] = function(params)
  UpdateSettingsPreview()
end

onReaction[gwtPrevPage.reactionName] = function(params)
  gwtPageContainer:setCurrentPage(gwtPageContainer:getCurrentPage() - 1)
  UpdateButtons()
end

onReaction[gwtNextPage.reactionName] = function(params)
  gwtPageContainer:setCurrentPage(gwtPageContainer:getCurrentPage() + 1)
  UpdateButtons()
end

onReaction[gwtImport.reactionName] = function(params)
  local wstring = gwtCode:getText()
  local settings = ImportSettings(wstring)
  if settings then
    LoadSettings(settings)
	UpdateSettingsPreview()
  end
end

onReaction[gwtExport.reactionName] = function(params)
  local settings = CopyTable(currentAuraData)
  SaveSettings(settings) 
  local wstring = ExportSettings(settings)
  if wstring then
    gwtCode:setText(wstring)
  else
    gwtCode:setText(common.GetEmptyWString())
  end
end
--------------------------------------------------------------------------------
--- Picture Functions and PageContainer Functions
--------------------------------------------------------------------------------
local pageTitelLocalizationReferences = { }
pageTitelLocalizationReferences[0] = "condition"
pageTitelLocalizationReferences[1] = "extendedcondition"
pageTitelLocalizationReferences[2] = "view"
pageTitelLocalizationReferences[3] = "exportimport"

function CanPrevPicture()
  return (currentPicture - 1) > 0
end

function CanNextPicture()
  return (currentPicture + 1) <= GetTableSize(pictureList)
end

function UpdateButtons()
  gwtPrevPicture.widget:Enable(CanPrevPicture())
  gwtPrevPicture.widget:Show(not gwtUseBuffImage:getChecked())
  gwtNextPicture.widget:Enable(CanNextPicture())
  gwtNextPicture.widget:Show(not gwtUseBuffImage:getChecked())
  local currentPage = gwtPageContainer:getCurrentPage()
  gwtPrevPage.widget:Enable(gwtPageContainer:canSetCurrentPage(currentPage - 1))
  gwtNextPage.widget:Enable(gwtPageContainer:canSetCurrentPage(currentPage + 1))
  gwtPageTitel:setVal("position", common.FormatInt(currentPage + 1, "%d"))
  gwtPageTitel:setVal("maximum", common.FormatInt(gwtPageContainer.Values.PageCount, "%d"))
  gwtPageTitel:setVal("title", L[pageTitelLocalizationReferences[currentPage]])
end

function LoadImage(index)
  --LogInfo("if (",index," > 0) and (",index," <= ", GetTableSize(pictureList), ") then")
  local settings = { }
  CreateBasicSettings(settings)
  SaveSettings(settings)
  if gwtUseBuffImage:getChecked() then
    if settings.buff or settings.debuff then
      local texture = SearchImageByBuffName(gwtName:getText())
      if texture then
        gwtPictureView.widget:SetBackgroundTexture(texture)
      else
        gwtPictureView.widget:SetBackgroundTexture(nil)
      end
    elseif settings.spellCD then
      gwtPictureView.widget:SetBackgroundTexture(GetSpellTexture(settings.name))
    elseif settings.itemCD then
      gwtPictureView.widget:SetBackgroundTexture(GetItemTexture(settings.name))
    end
	currentPicture = index
  else
    if (index > 0) and (index <= GetTableSize(pictureList)) then    
      gwtPictureView.widget:SetBackgroundTexture(pictureList[index].image)
      currentPicture = index
    end
  end
end

function LoadPictureList()
  local emotes = avatar.GetEmotes()
  for i = 0, GetTableSize(emotes)-1 do
    --LogInfo("emotes.",k," = ", v)
    local emoteInfo = avatar.GetEmoteInfo(emotes[i])
    for kk, vv in emoteInfo do 
      --LogInfo("Info(emotes.",i,").",kk," = ",vv)
    end 
    table.insert(pictureList, emoteInfo)
  end
end

--------------------------------------------------------------------------------
--- Picture Condition Functions
--------------------------------------------------------------------------------
function InitializePictureCondition()
  gwtShow.onClick = function(params)
    gwtShow:setChecked(true)
    gwtHide:setChecked(false)
  end
  gwtHide.onClick = function(params)
    gwtShow:setChecked(false)
    gwtHide:setChecked(true)
  end

  gwtBuff.onClick = function(params)
    gwtBuff:setChecked(true)
    gwtDebuff:setChecked(false)
    gwtSpellCD:setChecked(false)
    gwtItemCD:setChecked(false)
    HideUnpossibleConditions()
  end
  gwtDebuff.onClick = function(params)
    gwtBuff:setChecked(false)
    gwtDebuff:setChecked(true)
    gwtSpellCD:setChecked(false)
    gwtItemCD:setChecked(false)
    HideUnpossibleConditions()
  end
  gwtSpellCD.onClick = function(params)
    gwtBuff:setChecked(false)
    gwtDebuff:setChecked(false)
    gwtSpellCD:setChecked(true)
    gwtItemCD:setChecked(false)
    HideUnpossibleConditions()
  end
  gwtItemCD.onClick = function(params)
    gwtBuff:setChecked(false)
    gwtDebuff:setChecked(false)
    gwtSpellCD:setChecked(false)
    gwtItemCD:setChecked(true)
    HideUnpossibleConditions()
  end

  gwtYou.onClick = function(params)
    gwtYou:setChecked(true)
    gwtYourTarget:setChecked(false)
    gwtTargetOfTarget:setChecked(false)
    gwtPet:setChecked(false)
    gwtAnyUnit:setChecked(false)
  end
  gwtYourTarget.onClick = function(params)
    gwtYou:setChecked(false)
    gwtYourTarget:setChecked(true)
    gwtTargetOfTarget:setChecked(false)
    gwtPet:setChecked(false)
    gwtAnyUnit:setChecked(false)
  end
  gwtTargetOfTarget.onClick = function(params)
    gwtYou:setChecked(false)
    gwtYourTarget:setChecked(false)
    gwtTargetOfTarget:setChecked(true)
    gwtPet:setChecked(false)
    gwtAnyUnit:setChecked(false)
  end
  gwtPet.onClick = function(params)
    gwtYou:setChecked(false)
    gwtYourTarget:setChecked(false)
    gwtTargetOfTarget:setChecked(false)
    gwtPet:setChecked(true)
    gwtAnyUnit:setChecked(false)
  end
  gwtAnyUnit.onClick = function(params)
    gwtYou:setChecked(false)
    gwtYourTarget:setChecked(false)
    gwtTargetOfTarget:setChecked(false)
    gwtPet:setChecked(false)
    gwtAnyUnit:setChecked(true)
  end

  gwtTimerElapsed.onClick = function(params)
    gwtTimerElapsed:setChecked(true)
    gwtTimerRemaining:setChecked(false)
  end

  gwtTimerRemaining.onClick = function(params)
    gwtTimerElapsed:setChecked(false)
    gwtTimerRemaining:setChecked(true)
  end
end

function HideUnpossibleConditions()
  if gwtBuff:getChecked() or gwtDebuff:getChecked() then
    gwtYourTarget.widget:Show(true)
    gwtTargetOfTarget.widget:Show(true)
    gwtPet.widget:Show(true)
    gwtCastedByMe.widget:Show(true)
    gwtHostile.widget:Show(true)
    gwtFriendly.widget:Show(true)
    gwtStackMin.widget:Show(true)
    gwtStackMinEdit.widget:Show(true)
    gwtStackMax.widget:Show(true)
    gwtStackMaxEdit.widget:Show(true)

    gwtConditionGrid.target_text.widget:Show(true)
    gwtConditionGrid.targetOfTarget_text.widget:Show(true)
    gwtConditionGrid.pet_text.widget:Show(true)
    gwtConditionGrid.anyUnit.widget:Show(true)
    gwtConditionGrid.anyUnit_text.widget:Show(true)
    
    gwtExtendedConditions.ifGroupMemberNumber.widget:Show(true)
    gwtExtendedConditions.ifGroupMemberNumber_text.widget:Show(true)
    gwtExtendedConditions.groupMemberNumber.widget:Show(true)	
    gwtExtendedConditions.castedByMe_text.widget:Show(true)
    gwtExtendedConditions.hostile_text.widget:Show(true)
    gwtExtendedConditions.friendly_text.widget:Show(true)
    gwtExtendedConditions.stacksize_text.widget:Show(true)
    gwtExtendedConditions.stackmin_text.widget:Show(true)
    gwtExtendedConditions.stackmax_text.widget:Show(true)
  elseif gwtSpellCD:getChecked() or gwtItemCD:getChecked() then
    gwtYou.onClick()
    gwtYourTarget.widget:Show(false)
    gwtTargetOfTarget.widget:Show(false)
    gwtPet.widget:Show(false)
    gwtCastedByMe.widget:Show(false)
    gwtHostile.widget:Show(false)
    gwtFriendly.widget:Show(false)
    gwtStackMin.widget:Show(false)
    gwtStackMinEdit.widget:Show(false)
    gwtStackMax.widget:Show(false)
    gwtStackMaxEdit.widget:Show(false)

    gwtConditionGrid.target_text.widget:Show(false)
    gwtConditionGrid.targetOfTarget_text.widget:Show(false)
    gwtConditionGrid.pet_text.widget:Show(false)
    gwtConditionGrid.anyUnit.widget:Show(false)
    gwtConditionGrid.anyUnit_text.widget:Show(false)

    gwtExtendedConditions.ifGroupMemberNumber.widget:Show(false)
    gwtExtendedConditions.ifGroupMemberNumber_text.widget:Show(false)
    gwtExtendedConditions.groupMemberNumber.widget:Show(false)	
    gwtExtendedConditions.castedByMe_text.widget:Show(false)
    gwtExtendedConditions.hostile_text.widget:Show(false)
    gwtExtendedConditions.friendly_text.widget:Show(false)
    gwtExtendedConditions.stacksize_text.widget:Show(false)
    gwtExtendedConditions.stackmin_text.widget:Show(false)
    gwtExtendedConditions.stackmax_text.widget:Show(false)
  end
  UpdateBuffImage(false)
end
--------------------------------------------------------------------------------
--- Functions
--------------------------------------------------------------------------------
function GetInt(textbox, fallback)
  local result = common.GetIntFromWString(textbox:getText())
  if not result then
    if not fallback then
      return nil
    end
    return fallback
  end
  return result
end
function SetInt(textbox, value)
  if not value then
    textbox:setText(common.GetEmptyWString())
  else
    textbox:setText(common.FormatInt(value, "%d"))
  end
end

function SetOpacity(value)
  if value > 1.0 then
    value = 1.0
  elseif value < 0.0 then
    value = 0.0
  end
  currentOpacity = value
  gwtOpacity:setVal("text", common.FormatFloat(value, "%.2f"))
end

function GetOpacity()
  return currentOpacity
end

function GetSpellTexture(name)
  local spellbook = avatar.GetSpellBook()
      for i, id in spellbook do 
        local spellInfo = avatar.GetSpellInfo(id)
        if (common.CompareWStringEx(spellInfo.name, name) == 0) then
          if spellInfo.texture then
            return spellInfo.texture
          end
        end
      end
      return nil
end

function SearchItemTexture(ids, name)
  if ids then
    for k, itemId in ids do
      if itemId then
        local itemInfo = avatar.GetItemInfo(itemId)
        if (common.CompareWStringEx(itemInfo.name, name) == 0) then
          return itemInfo.icon
        end
      end
    end
  end
  return nil
end

function GetItemTexture(name)
  return SearchItemTexture(avatar.GetInventoryItemIds(), name) or 
         SearchItemTexture(unit.GetEquipmentItemIds(avatar.GetId(), ITEM_CONT_EQUIPMENT), name)
end

function UpdateSettingsPreview()
  local settings = { }
  CreateBasicSettings(settings)
  SaveSettings(settings)
  gwtSettingsPreview:setPosition(settings.posX, settings.posY-20)
  gwtSettingsPreview:setSize(settings.sizeX, settings.sizeY+20)
  if gwtUseBuffImage:getChecked() then
    if settings.buff or settings.debuff then
      local texture = SearchImageByBuffName(gwtName:getText())
      if texture then
        gwtSettingsPreview.Image.widget:SetBackgroundTexture(texture)
      else
        gwtSettingsPreview.Image.widget:SetBackgroundTexture(nil)
      end
    elseif settings.spellCD then
      gwtSettingsPreview.Image.widget:SetBackgroundTexture(GetSpellTexture(settings.name))
    elseif settings.itemCD then
      gwtSettingsPreview.Image.widget:SetBackgroundTexture(GetItemTexture(settings.name))
    end
  else
    if (currentPicture > 0) and (currentPicture <= GetTableSize(pictureList)) then    
      gwtSettingsPreview.Image.widget:SetBackgroundTexture(pictureList[currentPicture].image)
    end
  end
  gwtSettingsPreview.widget:SetFade(settings.opacity)
  gwtSettingsPreview.Cooldown.timerFormat = settings.timerFormat
  gwtSettingsPreview.Cooldown.widget:Show(settings.showTimer)  
  gwtSettingsPreview.Image.widget:Show(settings.showImage)
  gwtSettingsPreview.ProgressBar.widget:Show(settings.showProgressBar)  
end

function UpdateBuffImage(setCheckedTrue)
  if setCheckedTrue then
    gwtUseBuffImage:setChecked(true)
  end
  UpdateSettingsPreview()
  LoadImage(currentPicture)
  UpdateButtons()
end

function CreateBasicSettings(settings)
  settings.version = 0
  settings.entryname = common.GetEmptyWString()
  settings.pictureIndex = 1
  settings.useBuffImage = true
  settings.show = true
  settings.hide = false
  settings.buff = true
  settings.debuff = false
  settings.spellCD = false
  settings.itemCD = false
  settings.name = common.GetEmptyWString()
  settings.you = true
  settings.yourTarget = false  
  settings.targetoftarget = false
  settings.pet = false
  settings.sizeX = 50
  settings.sizeY = 50
  settings.posX = 400
  settings.posY = 400
  settings.opacity = 1.0
  settings.castedbyme = true
  settings.infight = true
  settings.notinfight = true
  settings.solo = true
  settings.ingroup = true
  settings.inraid = true
  settings.notmounted = true
  settings.mounted = true
  settings.hostile = true
  settings.friendly = true
  settings.useotherrule = false
  settings.otherrule = common.GetEmptyWString()
  settings.stackmin = false
  settings.stackmax = false
  settings.stackminvalue = 0
  settings.stackmaxvalue = 0
  settings.showTimer = true
  settings.timerFormat = "mm:ss.f"
  settings.showProgressBar = true
  settings.showImage = true
  settings.ifgroupmembernumber = false
  settings.groupmembernumber = nil
  settings.anyunit = false
  settings.timerelapsed = false
  settings.timerremaining = true
  settings.timermin = false
  settings.timermax = false
  settings.timerminvalue = 0
  settings.timermaxvalue = 0
end

function LoadSettings(settings)  
  gwtItemName:setText(settings.entryname)  
  gwtUseBuffImage:setChecked(settings.useBuffImage)
  LoadImage(settings.pictureIndex)
  gwtShow:setChecked(settings.show)
  gwtHide:setChecked(settings.hide)
  gwtBuff:setChecked(settings.buff)
  gwtDebuff:setChecked(settings.debuff)
  gwtSpellCD:setChecked(settings.spellCD)
  gwtItemCD:setChecked(settings.itemCD)
  gwtName:setText(settings.name)
  gwtYou:setChecked(settings.you)
  gwtYourTarget:setChecked(settings.yourTarget)
  gwtTargetOfTarget:setChecked(settings.targetoftarget)
  gwtPet:setChecked(settings.pet)
  gwtCastedByMe:setChecked(settings.castedbyme)
  gwtNotInFight:setChecked(settings.notinfight)
  gwtInFight:setChecked(settings.infight)
  gwtSolo:setChecked(settings.solo)
  gwtInGroup:setChecked(settings.ingroup)
  gwtInRaid:setChecked(settings.inraid)
  gwtNotMounted:setChecked(settings.notmounted)
  gwtMounted:setChecked(settings.mounted)
  gwtHostile:setChecked(settings.hostile)
  gwtFriendly:setChecked(settings.friendly)
  gwtIfOtherVisible:setChecked(settings.useotherrule)
  gwtOtherRule:setText(settings.otherrule)
  SetInt(gwtPositionX, settings.posX)
  SetInt(gwtPositionY, settings.posY)
  SetInt(gwtSizeX, settings.sizeX)
  SetInt(gwtSizeY, settings.sizeY)
  gwtStackMin:setChecked(settings.stackmin)
  gwtStackMax:setChecked(settings.stackmax)
  SetInt(gwtStackMinEdit, settings.stackminvalue or 0)
  SetInt(gwtStackMaxEdit, settings.stackmaxvalue or 0)
  SetOpacity(settings.opacity)
  gwtShowTimer:setChecked(settings.showTimer)  
  gwtTimerFormat:setText(userMods.ToWString(settings.timerFormat))
  gwtShowBar:setChecked(settings.showProgressBar)
  gwtShowImage:setChecked(settings.showImage)
  gwtIfGroupMemberNumber:setChecked(settings.ifgroupmembernumber)
  SetInt(gwtGroupMemberNumber, settings.groupmembernumber)
  gwtAnyUnit:setChecked(settings.anyunit)
  gwtTimerElapsed:setChecked(settings.timerelapsed)
  gwtTimerRemaining:setChecked(settings.timerremaining)
  gwtTimerMin:setChecked(settings.timermin)
  gwtTimerMax:setChecked(settings.timermax)
  SetInt(gwtTimerMinEdit, settings.timerminvalue or 0)
  SetInt(gwtTimerMaxEdit, settings.timermaxvalue or 0)
end

function SaveSettings(settings)
  settings.entryname = gwtItemName:getText()
  settings.pictureIndex = currentPicture
  settings.useBuffImage = gwtUseBuffImage:getChecked()
  settings.show = gwtShow:getChecked()
  settings.hide = gwtHide:getChecked()
  settings.buff = gwtBuff:getChecked()
  settings.debuff = gwtDebuff:getChecked()
  settings.spellCD = gwtSpellCD:getChecked()
  settings.itemCD = gwtItemCD:getChecked()
  settings.name = gwtName:getText()
  settings.you = gwtYou:getChecked()
  settings.yourTarget = gwtYourTarget:getChecked()
  settings.targetoftarget = gwtTargetOfTarget:getChecked()
  settings.pet = gwtPet:getChecked()
  settings.castedbyme = gwtCastedByMe:getChecked()
  settings.notinfight = gwtNotInFight:getChecked()
  settings.infight = gwtInFight:getChecked()
  settings.solo = gwtSolo:getChecked()
  settings.ingroup = gwtInGroup:getChecked()
  settings.inraid = gwtInRaid:getChecked()
  settings.notmounted = gwtNotMounted:getChecked()
  settings.mounted = gwtMounted:getChecked()
  settings.hostile = gwtHostile:getChecked()
  settings.friendly = gwtFriendly:getChecked()
  settings.useotherrule = gwtIfOtherVisible:getChecked()
  settings.otherrule = gwtOtherRule:getText()
  settings.posX = GetInt(gwtPositionX, settings.posX)
  settings.posY = GetInt(gwtPositionY, settings.posY)
  settings.sizeX = GetInt(gwtSizeX, settings.sizeX)
  settings.sizeY = GetInt(gwtSizeY, settings.sizeY)
  settings.stackmin = gwtStackMin:getChecked()
  settings.stackmax = gwtStackMax:getChecked()
  settings.stackminvalue = GetInt(gwtStackMinEdit)
  settings.stackmaxvalue = GetInt(gwtStackMaxEdit)
  settings.opacity = GetOpacity()
  settings.widget = nil
  settings.showTimer = gwtShowTimer:getChecked()
  settings.timerFormat = userMods.FromWString(gwtTimerFormat:getText())
  settings.showProgressBar = gwtShowBar:getChecked()
  settings.showImage = gwtShowImage:getChecked()
  settings.ifgroupmembernumber = gwtIfGroupMemberNumber:getChecked()
  settings.groupmembernumber = GetInt(gwtGroupMemberNumber)
  settings.anyunit = gwtAnyUnit:getChecked()
  settings.timerelapsed = gwtTimerElapsed:getChecked()
  settings.timerremaining = gwtTimerRemaining:getChecked()
  settings.timermin = gwtTimerMin:getChecked()
  settings.timermax = gwtTimerMax:getChecked()
  settings.timerminvalue = GetInt(gwtTimerMinEdit)
  settings.timermaxvalue = GetInt(gwtTimerMaxEdit)
end

function ImportSettings(wstring)
  local settings = Deserialize(wstring)
  if settings then
    local cleanupTemplate = CreateEmptyAuraData()
    CreateBasicSettings(cleanupTemplate)
	UpdateSettings(settings)
    CleanUpSettings(settings, cleanupTemplate)
	SetDefaultsSettings(settings, cleanupTemplate)
	return settings
  end
end

function ExportSettings(settings)
  local cleanupTemplate = CreateEmptyAuraData()
  CreateBasicSettings(cleanupTemplate)
  CleanUpSettings(settings, cleanupTemplate)
  return Serialize(settings)
end

function DragNDropPreview()
  DnD:Init(gwtSettingsPreview.DNDID, wtReacting, gwtSettingsPreview, fUseCfg, fLockedToScreenArea, Padding)
end

function OpenAuraSettings(auraData)
  if auraData then
    currentAuraData = auraData
    auraData.isAbort = true
    if auraData.isNew then
      CreateBasicSettings(auraData)
	  auraData.version = 1
    end
    LoadSettings(auraData)
    gwtSettings:show(true)
    gwtAuras:show(false)
    gwtSettingsPreview:show(true)
    gwtAuraPanel.widget:SetFade(0.3)
    gwtAuraPanel:UpdateItems()
    UpdateBuffImage()	
    HideUnpossibleConditions()
	gwtCode:setText(common.GetEmptyWString())
  end
end
--------------------------------------------------------------------------------
--- INITIALIZATION
--------------------------------------------------------------------------------
gwtSettingsPreview.initDnD = function(self, wtReacting, fUseCfg, fLockedToScreenArea, Padding )
  if not wtReacting then
    wtReacting = self.widget
  end
  if not true then -- is DragAndDrop disabled?
    LogWarning("On the Window Settings was called the initDnD-Function, but DnD is disabled.")
  else
    DnD:Init(self.DNDID, wtReacting, self.widget, fUseCfg, fLockedToScreenArea, Padding)
  end
end
gwtSettingsPreview.show = function(self, visible)
  if(self.isVisible~=visible) then
    self.isVisible=visible
    if not true then -- is DragAndDrop disabled?
      if not visible then
        DnD:Unreg(self)
      else
        DnD:Reg(self)
      end
    end
  end
  self.widget:Show(visible)
end
gwtSettingsPreview.DNDID = 141
gwtSettingsPreview.DnDEvent = function(params)
  local placementPlain = gwtSettingsPreview.widget:GetPlacementPlain()
  SetInt(gwtPositionX, placementPlain.posX)
  SetInt(gwtPositionY, placementPlain.posY+20)
end
gwtSettingsPreview.Cooldown:setVal("text", userMods.ToWString("3.27"))
common.RegisterEventHandler( gwtSettingsPreview.DnDEvent, "EVENT_DND_DRAG_TO" )
common.RegisterEventHandler( gwtSettingsPreview.DnDEvent, "EVENT_DND_DROP_ATTEMPT" )
common.RegisterEventHandler( gwtSettingsPreview.DnDEvent, "EVENT_DND_DRAG_CANCELLED" )

gwtUseBuffImage._onClick = gwtUseBuffImage.onClick
gwtUseBuffImage.onClick = function(params)
  gwtUseBuffImage._onClick(params)
  UpdateBuffImage(false)
end

gwtShowTimer._onClick = gwtShowTimer.onClick
gwtShowTimer.onClick = function(params)
  gwtShowTimer._onClick(params)
  UpdateSettingsPreview()
end

gwtShowImage._onClick = gwtShowImage.onClick
gwtShowImage.onClick = function(params)
  gwtShowImage._onClick(params)
  UpdateSettingsPreview()
end

gwtShowBar._onClick = gwtShowBar.onClick
gwtShowBar.onClick = function(params)
  gwtShowBar._onClick(params)
  UpdateSettingsPreview()
end

function Init()
  LoadPictureList()
  LoadImage(1)
  UpdateButtons()
  local placement = gwtPrevPicture.widget:GetPlacementPlain()
  placement.sizeX = 100
  gwtPrevPicture.widget:SetPlacementPlain(placement)
  placement = gwtNextPicture.widget:GetPlacementPlain()
  placement.sizeX = 100
  gwtNextPicture.widget:SetPlacementPlain(placement)
  gwtSettings:setBackgroundFade(0.8)
  gwtSettings:show(false)
  gwtSettingsPreview:initDnD(nil, true, false)
  gwtSettingsPreview:show(false)
  gwtSettings.widget:SetPriority(2005)
  InitializePictureCondition()
end

Init()