JB_Cluster = {
Distance = 0,
PlayerPos = {x=0,y=0,world="void"},
SenderPos = {x=0,y=0,world="void"},
Prefix = "Void",
RQPrefix = "JBC_" ..  UnitName("player").. tostring(random(1000)),
SongName = "Void",
PrintVar = false,
Chnum = GetChannelName("JukeboxCoin"),
ChanString = "void",
}

local frameFooAddon = CreateFrame("FRAME", "FooAddonFrame")

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












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 Channelmanagement()
JB_Cluster.Chnum = GetChannelName("JukeboxCoin")
if JB_Cluster.Chnum == 0 then 
local channel_type, channel_name = JoinChannelByName("JukeboxCoin", nil, ChatFrame1:GetID(), nil);
JB_Cluster.Chnum = GetChannelName("JukeboxCoin")
end
end


------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------

local function myChatFilter(self, event, author, chanName, arg5, arg6, ...)
--print(arg6)
if arg6 == JB_Cluster.Chnum .. "." .. " " .. "JukeboxCoin" then 
--print(true);
--print(JB_Cluster.Chnum .. "." .. " " .. "JukeboxCoin");
return true;
end
---if arg5
end
local function DummyInit()
Channelmanagement()
ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", myChatFilter);
ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL_JOIN", myChatFilter);
ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL_LEAVE", myChatFilter);

end



------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------



    function Jukebox_Sender_Prompt(text,indicator)
	JB_Cluster.PlayerPos.x, JB_Cluster.PlayerPos.y = JBC_Position()
	JB_Cluster.ChanString = JB_Cluster.RQPrefix .. indicator .. text .. ";" .. JB_Cluster.PlayerPos.x .. ";" .. JB_Cluster.PlayerPos.y


	Channelmanagement()
	if JB_Cluster.Chnum ~= 0 then
	SendChatMessage( JB_Cluster.ChanString , "channel",nil, JB_Cluster.Chnum)	
    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, message, ...)

    JB_Cluster.Prefix, JB_Cluster.SongName, JB_Cluster.SenderPos.x, JB_Cluster.SenderPos.y = strsplit(";", message)
    JB_Cluster.SenderPos.x = tonumber(JB_Cluster.SenderPos.x)
    JB_Cluster.SenderPos.y = tonumber(JB_Cluster.SenderPos.y)
    if JB_Cluster.Prefix == JB_Cluster.RQPrefix then
     JB_Cluster.PlayerPos.x, JB_Cluster.PlayerPos.y = JBC_Position()
	 JB_Cluster.Distance = 100.0*((JB_Cluster.PlayerPos.x-JB_Cluster.SenderPos.x)^2+(JB_Cluster.PlayerPos.y-JB_Cluster.SenderPos.y)^2)^0.5
	 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("Interface\\AddOns\\JukeboxCoin\\MyMusic\\" .. JB_Cluster.SongName .. ".mp3")
       end
	 
       end
  end
  
    if JB_Cluster.Prefix == JB_Cluster.RQPrefix .. "B" then
     JB_Cluster.PlayerPos.x, JB_Cluster.PlayerPos.y = JBC_Position()
	 JB_Cluster.Distance = 100.0*((JB_Cluster.PlayerPos.x-JB_Cluster.SenderPos.x)^2+(JB_Cluster.PlayerPos.y-JB_Cluster.SenderPos.y)^2)^0.5
	 if JB_Cluster.Distance < 5  then 
	 
     C_Timer.After(2, printA )
	 
           PlayMusic(JB_Cluster.SongName);
       end
	 
       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();
	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)
    JB_Cluster.PlayerPos.x, JB_Cluster.PlayerPos.y = JBC_Position()
	JB_Cluster.ChanString = JB_Cluster.RQPrefix .. ";" .. self.editBox:GetText() .. ";" .. JB_Cluster.PlayerPos.x .. ";" .. JB_Cluster.PlayerPos.y
	
	Channelmanagement()
	if JB_Cluster.Chnum ~= 0 then
	
	SendChatMessage( JB_Cluster.ChanString , "channel",nil, JB_Cluster.Chnum)
    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("Sent string: " .. self.editBox:GetText())
	 print("String: " .. JB_Cluster.ChanString)
	 end   
	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)
	JB_Cluster.PlayerPos.x, JB_Cluster.PlayerPos.y = JBC_Position()
	JB_Cluster.ChanString = JB_Cluster.RQPrefix .. "B;" .. self.editBox:GetText() .. ";" .. JB_Cluster.PlayerPos.x .. ";" .. JB_Cluster.PlayerPos.y

	Channelmanagement()
	if JB_Cluster.Chnum ~= 0 then
	SendChatMessage( JB_Cluster.ChanString , "channel",nil, JB_Cluster.Chnum)	
    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 .. "B")
	 print("Sent string: " .. self.editBox:GetText())
	 print("String: " .. JB_Cluster.ChanString)
	 end
    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






frameFooAddon:RegisterEvent("CHAT_MSG_CHANNEL")
frameFooAddon:SetScript("OnEvent", eventHandler)


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


C_Timer.After(30,DummyInit)
--Channelmanagement()







