-------------------------------------------------------------------------------
--
--  Title      : 저항의 외침
--  Version    : 0.7
--  Author     : Futurean
--  Date       : 2007/06/08
--  LastUpdate : 2007/06/19
--
--  Special Thanks : Malam, Blink
--
-------------------------------------------------------------------------------

--[[
  ToDo List
    4. 표시 String 편집 가능하게 할 것
    5. Pull-Down Menu 만들 것
    6. 기술들 더 추가할 것
]]

YOR_Settings = {
  ["g_szDisplayOption"] = "SAY" ;
  ["g_szDisplayOptionSkill"] = "DEFAULT" ;
  ["g_szDisplayOptionBuff"] = "DEFAULT" ;
  ["g_szDisplayOptionDeuff"] = "DEFAULT" ;
  ["g_bSound"] = true ;
  ["g_bEnable"] = true ;
  ["g_szName"] = "None" ;
}

g_szStatus = nil ;
g_szSkill = nil ;
g_szTarget = nil ;
g_szEnglishClass = nil ;

function DisplayText( msg )
  DEFAULT_CHAT_FRAME:AddMessage( "|cffff0000"..YOR_NAME.." : |cff00ff00"..msg ) ;
end

function DisplayCenter( msg )
  DisplayText( msg ) ;
end

function DisplayInfo()
  DisplayCenter( YOR_NAME.." "..YOR_VERSION ) ;
  DisplayCenter( YOR_CREATOR ) ;
  DisplayCenter( YOR_USAGE ) ;
end

function SendCustomMessage( szKind, szChannel, szMessage )
  szMessageChannel = YOR_Settings["g_szDisplayOption"] ;
  if ( szKind == "SKILL" ) then
    if ( szChannel == "DEFAULT" ) then
      szMessageChannel = YOR_SKILL_CHANNEL ;
    else
      szMessageChannel = szChannel ;
    end
  elseif ( szKind == "BUFF" ) then
    if ( szChannel == "DEFAULT" ) then
      szMessageChannel = YOR_BUFF_CHANNEL ;
    else
      szMessageChannel = szChannel ;
    end
  elseif ( szKind == "DEBUFF" ) then
    if ( szChannel == "DEFAULT" ) then
      szMessageChannel = YOR_DEBUFF_CHANNEL ;
    else
      szMessageChannel = szChannel ;
    end
  end
  
  if ( szMessageChannel == "DEFAULT" ) then
    szMessageChannel = YOR_Settings["g_szDisplayOption"] ;
  end
  
--  DisplayText( szMessageChannel ) ;
  
  if ( szMessageChannel == "RAID_WARNING" ) then
    if ( ( not IsRaidLeader() ) and ( not IsRaidOfficer() ) ) then
      szMessageChannel = "RAID" ;
    end
  end
    
  if ( szMessageChannel == "RAID" ) then
--    DisplayText( "Raid Member : "..GetNumRaidMembers() ) ;
    if ( GetNumRaidMembers() < 1 ) then
      szMessageChannel = "PARTY" ;
    end
  end

  if ( szMessageChannel == "BATTLEGROUND" ) then
    if ( GetNumBattlefieldScores() < 1 ) then
      szMessageChannel = "PARTY" ;
    end
  end
    
  if ( szMessageChannel == "PARTY" ) then
    if ( GetNumPartyMembers() < 1 ) then
      szMessageChannel = "DISPLAY" ;
    end
  end
    
  if ( szMessageChannel == "DISPLAY" ) then
    DisplayText( szMessage ) ;
  else
    SendChatMessage( szMessage, szMessageChannel ) ;
  end

  if ( YOR_Settings["g_bSound"] ) then
    PlaySoundFile("Interface\\AddOns\\YellOfResist\\YellOfResist.wav");
  end  
end

function YellOfResist_OnLoad()
  SlashCmdList["YOR"] = SlashHandler ;
	SLASH_YOR1 = "/YOR" ;
	SLASH_YOR2 = "/YellOfRegist" ;
	SLASH_YOR2 = "/저항의외침" ;
	
	YOR_Setting = {} ;
	
	DisplayInfo() ;
	
	g_szEnglishClass = UnitClass( "player" ) ;
--	DisplayText( g_szEnglishClass ) ;
	
  this:RegisterEvent("CHAT_MSG_SPELL_SELF_DAMAGE");
  this:RegisterEvent("COMBAT_TEXT_UPDATE");
  this:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED");
  this:RegisterEvent("UNIT_SPELLCAST_SENT");
end

function YellOfResist_OnEvent(event)
  
  if ( not YOR_Settings["g_bEnable"]  ) then
    return ;
  end
 
  local szStatus ;
  local szMessage ;
  
  if ( event == "UNIT_SPELLCAST_SENT" ) then
    nIndex = GetSkillIndex( arg2 ) ;
    if ( nIndex ~= 0 ) then
      if ( YOR_SKILL[nIndex][2] ) then
        szMessage = YOR_SKILL[nIndex][2] ;
        szMessage = string.gsub( szMessage, "$target", arg4 ) ;
        szMessage = string.gsub( szMessage, "$skill", arg2 ) ;
        SendCustomMessage( "SKILL", YOR_SKILL[nIndex][4], szMessage ) ;
      end
    else
--      DisplayText( "SPELLCAST_SENT : "..arg2.." "..arg4 ) ;
    end
  elseif ( event == "COMBAT_TEXT_UPDATE" ) then
--[[  
    if ( arg1 ) then DisplayText( "COMBAT_TEXT_UPDATE 1 : ".. arg1 ) ; end
    if ( arg2 ) then DisplayText( "COMBAT_TEXT_UPDATE 2 : ".. arg2 ) ; end
    if ( arg3 ) then DisplayText( "COMBAT_TEXT_UPDATE 3 : ".. arg3 ) ; end
    if ( arg4 ) then DisplayText( "COMBAT_TEXT_UPDATE 4 : ".. arg4 ) ; end
    if ( arg5 ) then DisplayText( "COMBAT_TEXT_UPDATE 5 : ".. arg5 ) ; end
    if ( arg6 ) then DisplayText( "COMBAT_TEXT_UPDATE 6 : ".. arg6 ) ; end
  ]]  
    if ( arg1 == YOR_AURA_END ) then
      nIndex = GetBuffIndex( arg2 ) ;
      if ( nIndex ~= 0 ) then
        if ( YOR_BUFF[nIndex][2] ) then
          szMessage = YOR_BUFF[nIndex][2] ;
          szMessage = string.gsub( szMessage, "$buff", arg2 ) ;
          SendCustomMessage( "BUFF", YOR_BUFF[nIndex][3], szMessage ) ;
        end
      end
    elseif ( arg1 == YOR_AURA_START ) then
      nIndex = GetDebuffIndex( arg2 ) ;
      if ( nIndex ~= 0 ) then
        if ( YOR_DEBUFF[nIndex][2] ) then
          szMessage = YOR_DEBUFF[nIndex][2] ;
          szMessage = string.gsub( szMessage, "$debuff", arg2 ) ;
          SendCustomMessage( "DEBUFF", YOR_DEBUFF[nIndex][3], szMessage ) ;
        end
      end
    end
  elseif ( event == "CHAT_MSG_SPELL_SELF_DAMAGE" ) then
  --[[
    if ( arg1 ) then DisplayText( "CHAT_MSG_SPELL_SELF_DAMAGE 1 : ".. arg1 ) ; end
    if ( arg2 ) then DisplayText( "CHAT_MSG_SPELL_SELF_DAMAGE 2 : ".. arg2 ) ; end
    if ( arg3 ) then DisplayText( "CHAT_MSG_SPELL_SELF_DAMAGE 3 : ".. arg3 ) ; end
    if ( arg4 ) then DisplayText( "CHAT_MSG_SPELL_SELF_DAMAGE 4 : ".. arg4 ) ; end
    if ( arg5 ) then DisplayText( "CHAT_MSG_SPELL_SELF_DAMAGE 5 : ".. arg5 ) ; end
    if ( arg6 ) then DisplayText( "CHAT_MSG_SPELL_SELF_DAMAGE 6 : ".. arg6 ) ; end
    ]]
    szStatus = GetStatus( arg1 ) ;
    if ( not szStatus ) then
      return ;
    end
    szSkill = GetSkill( arg1 ) ;
    if ( not szSkill ) then
      return ;
    end
    szTarget = GetTarget( arg1 ) ;
    if ( not szTarget ) then
      return ;
    end
    
    nIndex = GetSkillIndex( szSkill ) ;
    if ( nIndex == 0 ) then
      return ;
    elseif ( YOR_SKILL[nIndex][3] == false ) then
      return ;
    end
        
    szNamePlayer = UnitName( "player" ) ;
    szNameTarget = UnitName( "target" ) ;
    szNameTargetTarget = UnitName( "targettarget" ) ;
    
    if ( szNameTarget ~= szTarget ) then
      szNameTargetTarget = nil ;
    end
    
    if ( szNamePlayer == szNameTargetTarget ) then
      szNameTargetTarget = nil ;
    end
    
    if ( szNameTargetTarget == nil ) then
      szMessage = YOR_MESSAGE_WITHOUT_TARGET ;
      szMessage = string.gsub( szMessage, "$target", szTarget ) ;
      szMessage = string.gsub( szMessage, "$skill", szSkill ) ;
      szMessage = string.gsub( szMessage, "$status", szStatus ) ;
    else
      szMessage = YOR_MESSAGE_WITH_TARGET ;
      szMessage = string.gsub( szMessage, "$target", szTarget ) ;
      szMessage = string.gsub( szMessage, "$skill", szSkill ) ;
      szMessage = string.gsub( szMessage, "$tot", szNameTargetTarget ) ;
      szMessage = string.gsub( szMessage, "$status", szStatus ) ;
    end
    
    SendCustomMessage( "SKILL", YOR_SKILL[nIndex][4], szMessage ) ;
  end
end

function SlashHandler(msg)
  msg = string.lower(msg) ;
  
  if ( ( msg == "enable" ) or ( msg == "on" ) ) then
    DisplayText( YOR_ON ) ;
    YOR_Settings["g_bEnable"] = true ;
  elseif ( ( msg == "disable" ) or ( msg == "off" ) ) then
    DisplayText( YOR_OFF ) ;
    YOR_Settings["g_bEnable"] = false ;
  elseif ( ( msg == "say" ) or ( msg == "대화" ) ) then
    DisplayText( YOR_SAY ) ;
    YOR_Settings["g_szDisplayOption"] = "SAY" ;
  elseif ( ( msg == "party" ) or ( msg == "파티" ) ) then
    DisplayText( YOR_PARTY ) ;
    YOR_Settings["g_szDisplayOption"] = "PARTY" ;
  elseif ( ( msg == "yell" ) or ( msg == "외침" ) ) then
    DisplayText( YOR_YELL ) ;
    YOR_Settings["g_szDisplayOption"] = "YELL" ;
  elseif ( ( msg == "raid" ) or ( msg == "공대" ) ) then
    DisplayText( YOR_RAID ) ;
    YOR_Settings["g_szDisplayOption"] = "RAID" ;
  elseif ( ( msg == "battleground" ) or ( msg == "전장" ) ) then
    DisplayText( YOR_BATTLEGROUND ) ;
    YOR_Settings["g_szDisplayOption"] = "BATTLEGROUND" ;
  elseif ( ( msg == "raid_warning" ) or ( msg == "공대경보" ) ) then
    DisplayText( YOR_RAID_WARNING ) ;
    YOR_Settings["g_szDisplayOption"] = "RAID_WARNING" ;
  elseif ( ( msg == "guild" ) or ( msg == "길드" ) ) then
    DisplayText( YOR_GUILD ) ;
    YOR_Settings["g_szDisplayOption"] = "GUILD" ;
  elseif ( ( msg == "display" ) or ( msg == "화면" ) ) then
    DisplayText( YOR_DISPLAY ) ;
    YOR_Settings["g_szDisplayOption"] = "DISPLAY" ;
  elseif ( ( msg == "sound" ) or ( msg == "소리내기" ) ) then
    DisplayText( YOR_SOUND ) ;
    YOR_Settings["g_bSound"] = true ;
  elseif ( ( msg == "mute" ) or ( msg == "소리끄기" ) ) then
    DisplayText( YOR_MUTE ) ;
    YOR_Settings["g_bSound"] = false ;
  else
    DisplayText( "Option List" ) ;
    
    if ( YOR_Settings["g_bEnable"] ) then
      DisplayText( "/YOR On(Off) or Enable(Disable) : On" ) ;
    else
      DisplayText( "/YOR On(Off) or Enable(Disable) : Off" ) ;
    end
    
    if ( YOR_Settings["g_bSound"] ) then
      DisplayText( "/YOR SOUND("..YOR_SOUND..") or MUTE("..YOR_MUTE..") : SOUND" ) ;
    else
      DisplayText( "/YOR SOUND("..YOR_SOUND..") or MUTE("..YOR_MUTE..") : MUTE" ) ;
    end
    
    DisplayText( "/YOR SAY : "..YOR_SAY ) ;
    DisplayText( "/YOR PARTY : "..YOR_PARTY ) ;
    DisplayText( "/YOR GUILD : "..YOR_GUILD ) ;
    DisplayText( "/YOR YELL : "..YOR_YELL ) ;
    DisplayText( "/YOR RAID : "..YOR_RAID ) ;
    DisplayText( "/YOR RAID_WARNING : "..YOR_RAID_WARNING ) ;
    DisplayText( "/YOR BATTLEGROUND : "..YOR_BATTLEGROUND ) ;
    DisplayText( "/YOR DISPLAY :"..YOR_DISPLAY ) ;
    
    DisplayText( "Current Option : "..YOR_Settings["g_szDisplayOption"] ) ;
  end
end

function GetStatus( msg )
  if ( string.find( msg, YOR_MESSAGE_REGIST ) ) then
    return YOR_RESIST ;
  elseif ( string.find( msg, YOR_MESSAGE_IMMUNE ) ) then
    return YOR_IMMUNE ;
  else
    return nil ;
  end
end

function GetSkill( msg )
  nSwitchResistStart = string.find( arg1, YOR_SKILL_REGIST_PREV_STRING ) ;
  nSwitchResistEnd = string.find( arg1, YOR_SKILL_REGIST_NEXT_STRING ) ;
    
  nStart = 0 ;
  nEnd = 0 ;
    
  if ( nSwitchResistStart and nSwitchResistEnd ) then
    nStart = nSwitchResistStart + string.len( YOR_SKILL_REGIST_PREV_STRING ) + YOR_SKILL_REGIST_PREV_INDEX ;
    nEnd = nSwitchResistEnd + YOR_SKILL_REGIST_NEXT_INDEX ;
  else
    nSwitchImmuneStart = string.find( arg1, YOR_SKILL_IMMUNE_PREV_STRING ) ;
    nSwitchImmuneEnd = string.find( arg1, YOR_SKILL_IMMUNE_NEXT_STRING ) ;
    if ( nSwitchImmuneStart and nSwitchImmuneEnd ) then
      nStart = nSwitchImmuneStart + string.len( YOR_SKILL_IMMUNE_PREV_STRING ) + YOR_SKILL_IMMUNE_PREV_INDEX ;
      nEnd = nSwitchImmuneEnd + YOR_SKILL_IMMUNE_NEXT_INDEX ;
    else
      bDisplaySkill = false ;
    end
  end
  
  szTextSkill = string.sub( arg1, nStart, nEnd ) ;

  return szTextSkill ;
end

function GetTarget( msg )
  nSwitchResistStart = string.find( arg1, YOR_TARGET_REGIST_PREV_STRING ) ;
  nSwitchResistEnd = string.find( arg1, YOR_TARGET_REGIST_NEXT_STRING ) ;
  
  nStart = 0 ;
  nEnd = 0 ;
    
  if ( nSwitchResistStart and nSwitchResistEnd ) then
    nStart = nSwitchResistStart + string.len( YOR_TARGET_REGIST_PREV_STRING ) + YOR_TARGET_REGIST_PREV_INDEX ;
    nEnd = nSwitchResistEnd + YOR_TARGET_REGIST_NEXT_INDEX ;
  else
    nSwitchImmuneStart = string.find( arg1, YOR_TARGET_IMMUNE_PREV_STRING ) ;
    nSwitchImmuneEnd = string.find( arg1, YOR_TARGET_IMMUNE_NEXT_STRING ) ;
    if ( nSwitchImmuneStart and nSwitchImmuneEnd ) then
      nStart = nSwitchImmuneStart + string.len( YOR_TARGET_IMMUNE_PREV_STRING ) + YOR_TARGET_IMMUNE_PREV_INDEX ;
      nEnd = nSwitchImmuneEnd + YOR_TARGET_IMMUNE_NEXT_INDEX ;
    else
      bDisplaySkill = false ;
    end
  end
  
   szTextTarget = string.sub( arg1, nStart, nEnd ) ;
   return szTextTarget ;
end

function GetSkillIndex( szSkill )
  for i = 1, getn( YOR_SKILL ) do
    if ( YOR_SKILL[i][1] == szSkill ) then
      return i ;
    end
  end
  return 0 ;
end

function GetBuffIndex( szBuff )
  for i = 1, getn( YOR_BUFF ) do
    if ( YOR_BUFF[i][1] == szBuff ) then
      return i ;
    end
  end
  return 0 ;
end

function GetDebuffIndex( szDebuff )
  for i = 1, getn( YOR_DEBUFF ) do
    if ( YOR_DEBUFF[i][1] == szDebuff ) then
      return i ;
    end
  end
  return 0 ;
end

