------------------------------------
-- I'VE NO CLUE OF WHAT THESE ARE --
------------------------------------

local _, core = ... ;
core.Config = {};
local Config = core.Config;

local LibRPMedia = LibStub:GetLibrary("LibRPMedia-1.0");


-------------------------
-- WHEEL SCROLL REWORK --
-------------------------

local function ScrollFrame_OnMouseWheel(self,delta)
local newValue = self:GetVerticalScroll() - (delta * 30);

if (newValue < 0) then
     newValue =0;
	 elseif (newValue > self:GetVerticalScrollRange()) then
	 newValue = self:GetVerticalScrollRange();
	 end
	 
	 self:SetVerticalScroll(newValue);

end

local function ScrollFrame_OnVerticalScroll(self,offset)
local newValue =  offset     
local tmp = 0
local tmp1 = 0
local tmp2 = newValue
local tmp3 = "null"

	 if (newValue < 20  ) then
	     tmp = -1
     else
       tmp = math.floor((newValue)/30 -2)	 
	 end

	   for j = 1,(12) do
	     tmp1 = tostring( j)
	     tmp3 = j + tmp +1
	     JB_ScrollFrame2[tmp1]:SetPoint("TOP",JB_ScrollFrame2.Back,"BOTTOM",0,-30*(j + tmp));
         JB_ScrollFrame2[tmp1]:SetText(LibRPMedia:GetMusicNameByIndex(tmp3))
	     JB_ScrollFrame2[tmp1]:SetScript("OnClick", function(self, button, down)
             if button == "LeftButton" then
	       	 JB_ScrollFrame2:Hide();
	         Jukebox_Sender_Prompt(LibRPMedia:GetMusicFileByIndex(j + tmp +1),"B;");
			 print("        |cFF00FF00"  .. LibRPMedia:GetMusicNameByIndex(j + tmp +1) .. "|r");
	         else
			 PlayMusic(LibRPMedia:GetMusicFileByIndex(j + tmp +1));
	         print("        |cFF00FF00"  .. LibRPMedia:GetMusicNameByIndex(j + tmp +1).. " - " .. LibRPMedia:GetMusicFileByIndex(j+ tmp +1) .. "|r");
	         end
	      end )
	   end
  
     end




--------------------------------------
-- GENERAL BUTTON CREATION FUNCTION --
--------------------------------------

function Config:CreateButton(ParentFrame,point,relativeFrame,relativePoint,xOffset,yOffset,xSize,ySize,text)

local btn = CreateFrame("Button",nil, ParentFrame, "GameMenuButtonTemplate");

btn:SetPoint(point,relativeFrame,relativePoint, xOffset ,yOffset);
btn:SetSize(xSize,ySize);
btn:SetText(text);
btn:SetNormalFontObject("GameFontNormalLarge");
btn:SetHighlightFontObject("GameFontHighlightLarge")
return btn;

end




-----------------------------------
--Frames defined in Parents.lua--
-----------------------------------


---Music Assistance Variables For Both Scroll menus
JukeBox_Music_Labels = {};
local i;
local SCL, SCL2;
local MusicSize = Jukebox_Coin_Music_Size();
JukeBox_Music_Labels = JukeboxCoin_Get_Music_List(JukeBox_Music_Labels);


---------------------------------
----  FIRST SCROLL MENU  --------
---------------------------------

---Main frame dimentions
JB_ScrollFrame1:SetSize(350,400);
JB_ScrollFrame1:SetPoint("CENTER", UIParent, "CENTER");
JB_ScrollFrame1:Hide();

--------------
--------------
JB_ScrollFrame1:SetMovable(true)
JB_ScrollFrame1:EnableMouse(true)
JB_ScrollFrame1:RegisterForDrag("LeftButton")
JB_ScrollFrame1:SetScript("OnDragStart", JB_ScrollFrame1.StartMoving)
JB_ScrollFrame1:SetScript("OnDragStop", JB_ScrollFrame1.StopMovingOrSizing)
JB_ScrollFrame1:SetClampedToScreen(true)   
--------------
--------------

--- Scroll Bar Details

JB_ScrollFrame1.ScrollFrame = CreateFrame("ScrollFrame",nil,JB_ScrollFrame1,"UIPanelScrollFrameTemplate");
JB_ScrollFrame1.ScrollFrame:SetPoint("TOPLEFT",AuraTrackerConfigDialogBG,"TOPLEFT",4,-8);
JB_ScrollFrame1.ScrollFrame:SetPoint("BOTTOMRIGHT",AuraTrackerConfigDialogBG,"BOTTOMRIGHT",-3,4);

JB_ScrollFrame1.ScrollFrame.ScrollBar:ClearAllPoints();
JB_ScrollFrame1.ScrollFrame.ScrollBar:SetPoint("TOPLEFT",JB_ScrollFrame1.ScrollFrame, "TOPRIGHT",-12,-18);
JB_ScrollFrame1.ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT",JB_ScrollFrame1.ScrollFrame, "BOTTOMRIGHT",-7,18);



local child = CreateFrame("Frame",nil,JB_ScrollFrame1.ScrollFrame);
child:SetSize(308,30*MusicSize + 90); --- Definex basing on the music list
JB_ScrollFrame1.ScrollFrame:SetScrollChild(child);
JB_ScrollFrame1.ScrollFrame:SetScript("OnMouseWheel",ScrollFrame_OnMouseWheel );

JB_ScrollFrame1.ScrollFrame:SetClipsChildren(true);



----- Buttons

JB_ScrollFrame1["1"] = Config:CreateButton(JB_ScrollFrame1.ScrollFrame,"CENTER",child,"TOP",0,-13,308,30,JukeBox_Music_Labels[1])
JB_ScrollFrame1["1"]:RegisterForClicks("AnyUp");
JB_ScrollFrame1["1"]:SetScript("OnClick", function(self, button, down)
	
	if button == "LeftButton" then
	JB_ScrollFrame1:Hide(); 
	Jukebox_Sender_Prompt(JukeBox_Music_Labels[1],";");
    else
	PlayMusic("Interface\\AddOns\\JukeboxCoin\\MyMusic\\" .. JukeBox_Music_Labels[1] .. ".mp3");
    print("        |cFF00FF00"  .. "Interface\\AddOns\\JukeboxCoin\\MyMusic\\" .. JukeBox_Music_Labels[1] .. ".mp3" .. "|r");
	end
	
	end )



for i = 2,MusicSize do
 SCL = tostring(i);
 SCL2 = tostring(i-1);
 JB_ScrollFrame1[SCL] = Config:CreateButton(JB_ScrollFrame1.ScrollFrame,"TOP",JB_ScrollFrame1[SCL2],"BOTTOM",0,0,308,30,JukeBox_Music_Labels[i]);
 JB_ScrollFrame1[SCL]:RegisterForClicks("AnyUp");
 JB_ScrollFrame1[SCL]:SetScript("OnClick", function(self, button, down)
 	if button == "LeftButton" then
	JB_ScrollFrame1:Hide(); 
	Jukebox_Sender_Prompt(JukeBox_Music_Labels[i],";");
    else
	PlayMusic("Interface\\AddOns\\JukeboxCoin\\MyMusic\\" .. JukeBox_Music_Labels[i] .. ".mp3");
    print("        |cFF00FF00"  .. "Interface\\AddOns\\JukeboxCoin\\MyMusic\\" .. JukeBox_Music_Labels[i] .. ".mp3" .. "|r");
	end
 end )
 end

 JB_ScrollFrame1.Back = Config:CreateButton(JB_ScrollFrame1.ScrollFrame,"TOP",JB_ScrollFrame1[SCL],"BOTTOM",0,0,308,60,"Back");
 JB_ScrollFrame1.Back:SetScript("OnClick", function(self)	JB_ScrollFrame1:Hide(); JB_MainFrame:Show(); end )

 
 
 
 ---Music Assistance Variables For Both Scroll menus
--JukeBox_Music_Labels_II = {};
local j;
local SCLII, SCLII2;
local MusicSizeII = LibRPMedia:GetNumMusicFiles();
--JukeBox_Music_Labels_II = JukeboxCoin_Get_Music_List_II(JukeBox_Music_Labels_II);
--JukeBox_Music_Labels_Names_II = JukeboxCoin_Get_Music_ListNames_II(JukeBox_Music_Labels_Names_II);

---------------------------------
----  SECOND SCROLL MENU  -------
---------------------------------

---Main frame dimentions
JB_ScrollFrame2:SetSize(650,400);
JB_ScrollFrame2:SetPoint("CENTER", UIParent, "CENTER");
JB_ScrollFrame2:Hide();

--------------
--------------
JB_ScrollFrame2:SetMovable(true)
JB_ScrollFrame2:EnableMouse(true)
JB_ScrollFrame2:RegisterForDrag("LeftButton")
JB_ScrollFrame2:SetScript("OnDragStart", JB_ScrollFrame2.StartMoving)
JB_ScrollFrame2:SetScript("OnDragStop", JB_ScrollFrame2.StopMovingOrSizing)
JB_ScrollFrame2:SetClampedToScreen(true)   
--------------
--------------

--- Scroll Bar Details

JB_ScrollFrame2.ScrollFrame = CreateFrame("ScrollFrame",nil,JB_ScrollFrame2,"UIPanelScrollFrameTemplate");
JB_ScrollFrame2.ScrollFrame:SetPoint("TOPLEFT",AuraTrackerConfig2DialogBG,"TOPLEFT",4,-8);
JB_ScrollFrame2.ScrollFrame:SetPoint("BOTTOMRIGHT",AuraTrackerConfig2DialogBG,"BOTTOMRIGHT",-3,4);


JB_ScrollFrame2.ScrollFrame.ScrollBar:ClearAllPoints();
JB_ScrollFrame2.ScrollFrame.ScrollBar:SetPoint("TOPLEFT",JB_ScrollFrame2.ScrollFrame, "TOPRIGHT",-12,-18);
JB_ScrollFrame2.ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT",JB_ScrollFrame2.ScrollFrame, "BOTTOMRIGHT",-7,18);


local child2 = CreateFrame("Frame",nil,JB_ScrollFrame2.ScrollFrame);
child2:SetSize(608,30*MusicSizeII+30); --- Definex basing on the music list
JB_ScrollFrame2.ScrollFrame:SetScrollChild(child2);
JB_ScrollFrame2.ScrollFrame:SetScript("OnMouseWheel",ScrollFrame_OnMouseWheel );
JB_ScrollFrame2.ScrollFrame:HookScript("OnVerticalScroll",ScrollFrame_OnVerticalScroll );


JB_ScrollFrame2.ScrollFrame:SetClipsChildren(true);



----- Buttons

JB_ScrollFrame2.Back = Config:CreateButton(JB_ScrollFrame2.ScrollFrame,"CENTER",child2,"TOP",0,-13,608,30,"Back");
JB_ScrollFrame2.Back:SetScript("OnClick", function(self)	JB_ScrollFrame2:Hide(); JB_MainFrame:Show(); end )
 
for j = 1,(12) do
 SCLII = tostring(j);
 SCLII2 = tostring(j-1);
 JB_ScrollFrame2[SCLII] = Config:CreateButton(JB_ScrollFrame2.ScrollFrame,"TOP",JB_ScrollFrame2.Back,"BOTTOM",0,-30*(j-1),608,30,LibRPMedia:GetMusicNameByIndex(j));
 JB_ScrollFrame2[SCLII]:RegisterForClicks("AnyUp");
 JB_ScrollFrame2[SCLII]:SetScript("OnClick", function(self, button, down)

    if button == "LeftButton" then
		JB_ScrollFrame2:Hide();
	Jukebox_Sender_Prompt(LibRPMedia:GetMusicFileByIndex(j),"B;");
	print( "        |cFF00FF00"  .. LibRPMedia:GetMusicNameByIndex(j) .. "|r");
	else
	PlayMusic(LibRPMedia:GetMusicFileByIndex(j));
	print("        |cFF00FF00"  .. LibRPMedia:GetMusicNameByIndex(j) .. " - " .. LibRPMedia:GetMusicFileByIndex(j) .. "|r");
	end
	end )
 end

 



--------------------------
------  MAIN MENU  -------
--------------------------


JB_MainFrame:SetSize(250,400);
JB_MainFrame:SetPoint("CENTER", UIParent, "CENTER");



JB_MainFrame.title = JB_MainFrame:CreateFontString(nil,"OVERLAY","GameFontHighlight");
JB_MainFrame.title:SetPoint("LEFT",JB_MainFrame.TitleBg, "LEFT",5,0);
JB_MainFrame.title:SetText("Main Interface");

JB_MainFrame.PrefixMusic = Config:CreateButton(JB_MainFrame,"CENTER",JB_MainFrame,"TOP",0,-70,140,30,"Prefix")
JB_MainFrame.PrefixMusic:SetScript("OnClick", function(self)
	JB_MainFrame:Hide();
	StaticPopup_Show ("MUSIC_PREFIX");
end)

JB_MainFrame.GameMusic = Config:CreateButton(JB_MainFrame,"TOP",JB_MainFrame.PrefixMusic,"BOTTOM",0,-35,140,30,"Game Music")
JB_MainFrame.GameMusic:SetScript("OnClick", function(self)
	JB_MainFrame:Hide();
	JB_ScrollFrame2:Show();
end)

JB_MainFrame.GameMusicPrompt = Config:CreateButton(JB_MainFrame,"TOP",JB_MainFrame.GameMusic,"BOTTOM",0,-10,140,30,"Manual")
JB_MainFrame.GameMusicPrompt:SetScript("OnClick", function(self)
	JB_MainFrame:Hide();
	StaticPopup_Show ("MUSIC_PROMPT2")
end)

JB_MainFrame.CustomMusic = Config:CreateButton(JB_MainFrame,"TOP",JB_MainFrame.GameMusic,"BOTTOM",0,-60,140,30,"Custom Music")
JB_MainFrame.CustomMusic:SetScript("OnClick", function(self)
	JB_MainFrame:Hide();
	JB_ScrollFrame1:Show();
end)

JB_MainFrame.CustomMusicPrompt = Config:CreateButton(JB_MainFrame,"TOP",JB_MainFrame.CustomMusic,"BOTTOM",0,-10,140,30,"Manual")
JB_MainFrame.CustomMusicPrompt:SetScript("OnClick", function(self)
	JB_MainFrame:Hide();
	StaticPopup_Show ("MUSIC_PROMPT")
end)

JB_MainFrame.StopMusic = Config:CreateButton(JB_MainFrame,"TOP",JB_MainFrame.CustomMusicPrompt,"BOTTOM",0,-30,140,30,"Stop Music")
JB_MainFrame.StopMusic:SetScript("OnClick", function(self)
	StopMusic();
end)

--------------
--------------
JB_MainFrame:SetMovable(true)
JB_MainFrame:EnableMouse(true)
JB_MainFrame:RegisterForDrag("LeftButton")
JB_MainFrame:SetScript("OnDragStart", JB_MainFrame.StartMoving)
JB_MainFrame:SetScript("OnDragStop", JB_MainFrame.StopMovingOrSizing)
JB_MainFrame:SetClampedToScreen(true)   
--------------
--------------
JB_MainFrame:Hide();

----------------------------------
---- MAIN MENU SLASH COMMAND  ----
----------------------------------

local MMMenu = function()
 JB_MainFrame:Show();
end  
SLASH_MMenu1 = "/MusicMenu"
SlashCmdList["MMenu"] = function() 
MMMenu() ;
end










