
--SET OF INITIAL VARIABLES THAT ARE USED IN THE WHOLE PROGRAM
JB_Cluster = {
Distance = 0,
PlayerPos = {x=0,y=0,world="void"},
SenderPos = {x=0,y=0,world="void"},
Prefix = "Void",
RQPrefix = "JBC_" ..  "Default", --UnitName("player").. tostring(random(1000)),
SongName = "Void",
PrintVar = false,
Chnum = "RAID", --GetChannelName("JukeboxCoin"),
ChanString = "void",
Indicator = ";",
Version = "0.1",
LocalPath = "Interface\\AddOns\\JukeboxCoin\\MyMusic\\",
}

--DEFINE FRAME RELATED TO EVENT HANDLING. I.E. THIS WILL HAVE ASSIGNED AT THE END OF THE CODE AN "ON EVENT" SCRIPT.
local RequestHandler = CreateFrame("FRAME", "ComHand")
local successfulRequest = C_ChatInfo.RegisterAddonMessagePrefix(JB_Cluster.RQPrefix)
--EXTRACTS POSITION FROM GAME
local function JBC_Position()
    local map = C_Map.GetBestMapForUnit("player")
    if (map) then
        local position = C_Map.GetPlayerMapPosition(map, "player")
        if (position) then
           return position:GetXY()
        else
	       return 0.0, 0.0
    end
    else
	     return 0.0, 0.0
    end
end
--CALCULATES DISTANCES
local function calc_distance(x1,y1,x2,y2,range)
return range*((x2-x1)^2+(y2-y1)^2)^0.5
end

---DEBUG FUNTION
local function printA()
	 if JB_Cluster.PrintVar == true then
	 print("|cFFFFFF00Message recived. Here is the data list:|r")
	 print("Player Position: " .. JB_Cluster.PlayerPos.x .. " " .. JB_Cluster.PlayerPos.y)
	 print("Sender Position: " .. JB_Cluster.SenderPos.x .. " " .. JB_Cluster.SenderPos.y)
	 print("Distance: " .. JB_Cluster.Distance)
	 print("Sender Prefix: " .. JB_Cluster.Prefix)
	 print("Player Prefix: " .. JB_Cluster.RQPrefix)
	 print("Sent string: " .. JB_Cluster.SongName)
	 print("String: " .. JB_Cluster.ChanString)
	 end
end 



local function Play_Music()
     JB_Cluster.PlayerPos.x, JB_Cluster.PlayerPos.y = JBC_Position()
	 JB_Cluster.Distance = calc_distance(JB_Cluster.PlayerPos.x,JB_Cluster.PlayerPos.y,JB_Cluster.SenderPos.x,JB_Cluster.SenderPos.y,100.0)
	 if JB_Cluster.Distance < 5  then 
	 
       C_Timer.After(2, printA )
	 
       if JB_Cluster.SongName == "STOP" then
	    StopMusic()
       elseif JB_Cluster.SongName == "TEST" then
	     print("////***TEST SIGNAL ARRIVED***\\\\\\\\")
       else
           PlayMusic(JB_Cluster.SongName)
       end
     end	 

end



    function Jukebox_Sender_Prompt(text,indicator)
	JB_Cluster.PlayerPos.x, JB_Cluster.PlayerPos.y = JBC_Position()
	JB_Cluster.ChanString = text .. JB_Cluster.Indicator .. JB_Cluster.PlayerPos.x .. JB_Cluster.Indicator .. JB_Cluster.PlayerPos.y

    if( UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then 
	 
        success = C_ChatInfo.SendAddonMessage(JB_Cluster.RQPrefix, JB_Cluster.ChanString,JB_Cluster.Chnum)
	    if success == false then
	       print("|cFFFF0000     ERROR:|r  |cFF00FF00You need to be in a " .. JB_Cluster.Chnum .. " party.|r")
		end
	 else 
	   print("|cFFFF0000     ERROR:|r  |cFF00FF00You need to be raid leader or assistant.|r")
	 end
	


	if JB_Cluster.PrintVar == true then
	 print("|cFFFFFF00Here is the data list:|r")
	 print("Player Position: " .. JB_Cluster.PlayerPos.x .. " " .. JB_Cluster.PlayerPos.y)
	 print("Sender Prefix: " .. JB_Cluster.RQPrefix)
	 print("String: " .. JB_Cluster.ChanString)
	 end
   

end

function Abort_deny()
JB_MainFrame:Show();
end



local function eventHandler(self, event, prefix, text, channel, ...)

    if prefix == JB_Cluster.RQPrefix and channel == JB_Cluster.Chnum then

	  JB_Cluster.SongName, JB_Cluster.SenderPos.x, JB_Cluster.SenderPos.y = strsplit(JB_Cluster.Indicator, text)
      JB_Cluster.SenderPos.x = tonumber(JB_Cluster.SenderPos.x)
      JB_Cluster.SenderPos.y = tonumber(JB_Cluster.SenderPos.y)

      Play_Music()
      
      end
  end
  


  
StaticPopupDialogs["MUSIC_PREFIX"] = {
    text = "Current Prefix: " .. JB_Cluster.RQPrefix .. " Insert the new Prefix",
    button1 = "Proceed",
    button2 = "Abort",
    hasEditBox = true,
    OnAccept = function (self, data, data2)
	JB_Cluster.RQPrefix = "JBC_" .. self.editBox:GetText();
    C_ChatInfo.RegisterAddonMessagePrefix(JB_Cluster.RQPrefix)
	StaticPopupDialogs["MUSIC_PREFIX"].text = "Current Prefix: " .. JB_Cluster.RQPrefix .. " Insert the new Prefix";
	JB_MainFrame:Show();
	if JB_Cluster.PrintVar == true then
	print("Prefix set to: " .. JB_Cluster.RQPrefix);
	end
    end,
	OnCancel = function (_,reason)
               JB_MainFrame:Show();
    end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = true,
    preferredIndex = 3,
}

StaticPopupDialogs["MUSIC_PROMPT"] = {
    text = "Insert the name of the music file.",
    button1 = "Proceed",
    button2 = "Abort",
    hasEditBox = true,
    OnAccept = function(self, data, data2)
      Jukebox_Sender_Prompt(JB_Cluster.LocalPath .. self.editBox:GetText() .. ".mp3",JB_Cluster.Indicator)
	 end,
	OnCancel = function (_,reason)
               JB_MainFrame:Show();
    end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = true,
    preferredIndex = 3,
}

StaticPopupDialogs["MUSIC_PROMPT2"] = {
    text = "Insert the name of the music file.",
    button1 = "Proceed",
    button2 = "Abort",
    hasEditBox = true,
    OnAccept =  function(self, data, data2)
      Jukebox_Sender_Prompt(self.editBox:GetText(),JB_Cluster.Indicator)
	 end,
	OnCancel = function (_,reason)
               JB_MainFrame:Show();
    end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = true,
    preferredIndex = 3,
}
   

   
   
   
   
local MH = function()
    print("USE SLASH COMMANDS")
	print("/MusicMenu will open the main menu")
	print ("/Mprefix to set a common prefix")
    print("/Mptr will open the prompt")
	print("/Mptr2 will open the prompt 2")
	print("STOP used in Mptr will stop the music")
	print("/MessPrint to show more informations in the comunication.")
  end

local Mprefix = function()
StaticPopup_Show ("MUSIC_PREFIX")
end  
  
local Mptr = function()
StaticPopup_Show ("MUSIC_PROMPT")
end
  
local Mptr2 = function()
StaticPopup_Show ("MUSIC_PROMPT2")
end  

local MMprint = function()
if JB_Cluster.PrintVar == false then
JB_Cluster.PrintVar = true
print("|cFF00FF00PrintVar set true|r")
print("Prefix set to: " .. JB_Cluster.RQPrefix)
else
JB_Cluster.PrintVar = false
print("|cFFFF0000PrintVar set false|r")
end 
 end

 
SLASH_MMHelp1 = "/MHelp"
SlashCmdList["MMHelp"] = function() 
MH() 
end
SLASH_MPREF1 = "/Mprefix"
SlashCmdList["MPREF"] = function() 
Mprefix() 
end
SLASH_MPMPT1 = "/Mptr"
SlashCmdList["MPMPT"] = function()
Mptr()
end
SLASH_2MPMPT1 = "/Mptr2"
SlashCmdList["2MPMPT"] = function()
Mptr2()
end
SLASH_MMessPrint1 = "/MessPrint"
SlashCmdList["MMessPrint"] = function()
MMprint()
end






RequestHandler:RegisterEvent("CHAT_MSG_ADDON")
RequestHandler:SetScript("OnEvent", eventHandler)


print("[|cFF00FF00JukeBox Coin|r-|cFFFF0000V " .. JB_Cluster.Version .. "|r] JukeBox Initialized");
print("[|cFF00FF00JukeBox Coin|r-|cFFFF0000V " .. JB_Cluster.Version .. "|r] Use slash command [MHelp] for help");



--JBC_MinimapPos = {
--	90, -- [1]
--}

    local LDB = LibStub("LibDataBroker-1.1", true)
    local LDBIcon = LDB and LibStub("LibDBIcon-1.0", true)
    if LDB then
        local JBC_MinimapBtn = LDB:NewDataObject("MainButton", {
            type = "launcher",
			text = "JukeboxAddon",
            icon = "Interface/ICONS/INV_Trinket_80_Kultiras02c.blp",
            OnClick = function(_, button)
                if button == "LeftButton" then JB_MainFrame:Show() end
            end,
            OnTooltipShow = function(tt)
                tt:AddLine("JukeboxCoin")
                tt:AddLine("|cffffff00Click|r Open JukeboxCoin main menu.")
            end,
        })
        if LDBIcon then
           LDBIcon:Register("MainButton", JBC_MinimapBtn, JBC_MinimapPos)
        end
    end

