dofile('data/lua/screens/Extras/MusicNotesScreen.lua');

--------------------------------------------------------------------------------
class 'CMusicPage'

function CMusicPage:__init(screen)
	assert(screen);

	local zOffset = 50;
	local tracksPos = {580, 210};
	local captionOffset = {-260, 0};
	local trackPanelHeight = 154;
	local trackPanelsGap = 16;
	local trackCount = 0;
	local playButtonOffset = {200, 0};
	local showNoteButtonOffset = {300, 0};
	local progressbarPos = {650, 685};

    -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	local auxCreateTrackPanel = function(parent, 
										 captionText, 
										 playEventHandlerFn,
										 showNoteHandlerFn)
		local panel = screen:CreateAnimation('data/Texture/GUI/Extras/PageMusic/SoundtrackPanel.png');
		panel:SetXY(tracksPos[1], tracksPos[2] + (trackPanelHeight + trackPanelsGap) * trackCount);
		panel:AttachTo(parent);
    	do
    		local caption = CreateTextEntity(INGAME_MENU_FONT, captionText);
    		caption:SetXY(captionOffset[1], captionOffset[2]);
    		caption:SetZ(2.0);
    		caption:SetScale(1.0);
    		caption:AttachTo(panel);
    		caption:SetTextAlignment(HGETEXT_LEFT + HGETEXT_MIDDLE);
    	end
    	do    
    	    local button = MakeButton(playButtonOffset[1], playButtonOffset[2], 120, 140);
			button:DisableIfHidden(true);
    	    button:GetCaption():SetFont(BUTTONS_FONT);
    		button:GetCaption():SetScale(0.8);
    		button:SetZ(2.0);
    	
    		button:AttachTo(panel);
    		button:SetAction(playEventHandlerFn);
    		button:SetSpriteUp('ButtonPlayMusic');
    		button:SetSpriteOver('ButtonPlayMusicOver');
    		button:SetSpriteDown('ButtonPlayMusicDown');
    		button:SetActionSound('Menu_button_click');
    	end
    	do    
    	    local button = MakeButton(showNoteButtonOffset[1], showNoteButtonOffset[2], 90, 140);
			button:DisableIfHidden(true);
    	    button:GetCaption():SetFont(BUTTONS_FONT);
    		button:GetCaption():SetScale(0.8);
    		button:SetZ(2.0);
    	
    		button:AttachTo(panel);
    		button:SetAction(showNoteHandlerFn);
    		button:SetSpriteUp('ButtonShowNotes');
    		button:SetSpriteOver('ButtonShowNotesOver');
    		button:SetSpriteDown('ButtonShowNotesDown');
    		button:SetActionSound('Menu_button_click');
    	end

		trackCount = trackCount + 1;
		return panel;		
	end


	self.pageRoot = screen:CreateAnimation('data/Texture/Scenes/Common/area.png');
	self.pageRoot:SetColor('0x10FFFFFF');
	self.pageRoot:SetPosZ(zOffset);

	self.tracks = {};
	table.insert(self.tracks, auxCreateTrackPanel(self.pageRoot, 'EXTRACONTENT_SCR_1ST_TACK_NAME', 'OnPlaySoundtrack1', 'OnShowNotes1'));
	table.insert(self.tracks, auxCreateTrackPanel(self.pageRoot, 'EXTRACONTENT_SCR_2ND_TACK_NAME', 'OnPlaySoundtrack2', 'OnShowNotes2'));
	table.insert(self.tracks, auxCreateTrackPanel(self.pageRoot, 'EXTRACONTENT_SCR_3RD_TACK_NAME', 'OnPlaySoundtrack3', 'OnShowNotes3'));

	self.currentTrack = 1;
	self.cursor = screen:CreateAnimation('data/Texture/GUI/Extras/PageMusic/SoundtrackPanelActive.png');
	self.cursor:SetXY(self.tracks[self.currentTrack]:GetX(), self.tracks[self.currentTrack]:GetY());
	self.cursor:SetZ(1.0);
	self.cursor:AttachTo(self.pageRoot);
	self.cursor:SetVisible(false);

	do
		local progressbar = screen:CreateAnimation('data/Texture/GUI/Extras/PageMusic/Progressbar.png');
		progressbar:SetXY(progressbarPos[1], progressbarPos[2]);
		progressbar:SetZ(310.0);
		progressbar:AttachTo(self.pageRoot);

		local progressbarLine = screen:CreateAnimation('data/Texture/GUI/Extras/PageMusic/ProgressbarLine.png');
		progressbarLine:SetHotSpot(5, 10);
		progressbarLine:SetXY(-177, -5.0);
		progressbarLine:SetZ(1.0);
		progressbarLine:AttachTo(progressbar);
        progressbarLine:SetScaleX(0);
        
		local progressbarLineEffector = CreateAnimationEffector();
		progressbarLineEffector:AttachTo(progressbarLine);
		progressbarLineEffector:SetMode(_ANIM_LOOP);

	self.progressbarLineEffector = progressbarLineEffector;
	end

	local eventMap = {};
	eventMap.Prev = 'OnPrevMusic';
	eventMap.Play = 'OnPlayMusicDefault';
	eventMap.Stop = 'OnStopMusic';
	eventMap.Next = 'OnNextMusic';
	eventMap.Save = 'OnSaveResource';
	CreateNavigaionPanel(screen, self, self.pageRoot, 235,725, eventMap);

	assert(self.navigationButtons);
	self.navigationButtons['Save']:Stop();
	self.navigationButtons['Save']:SetVisible(false);

musicPage = self;
end

function CMusicPage:Show()
	self.pageRoot:Show();
end
function CMusicPage:Hide()
	self.pageRoot:Hide();
end

function CMusicPage:SetCursorTo(index)
	assert(self.cursor ~= nil and 
		   self.tracks ~= nil);

	if (index) then
		assert(index <= table.getn(self.tracks));

		self.currentTrack = index;
		self.cursor:SetXY(self.tracks[index]:GetX(), self.tracks[index]:GetY());
		self.cursor:SetVisible(true);
	else
		self.cursor:SetVisible(false);
	end
end

function CMusicPage:ResetProgressbar()
	assert(self.progressbarLineEffector);
	local eff = self.progressbarLineEffector;

	eff:Stop();
	eff:SetToStart();
	eff:EraseAllKeys();
	eff:SetAnimationTime(0.0);
end
function CMusicPage:RunProgressbar(duration)
	assert(self.progressbarLineEffector);

	self:ResetProgressbar();
	local eff = self.progressbarLineEffector;

	eff:SetKey(_TRACK_SX, 0.0,       0.0);
	eff:SetKey(_TRACK_SX, duration,  1.0);
	eff:Play();
end

function CMusicPage:PlaySoundtrack(index)
	local trackList = {
		'music3',
		'music5',
		'music6'
	};
	assert(index and index <= table.getn(trackList));

	self:SetCursorTo(index);
	PlayAmbientMusic(trackList[index]);

	self:RunProgressbar(ScriptGetCurrentMusicTrackDuration());
end

function CMusicPage:StopPlayback()
	self:SetCursorTo(nil);
	StopAmbientMusic(true);

	self:ResetProgressbar();
end

function CMusicPage:IsPlaying()
	return self.cursor:IsVisible();
end 

-- Event handlers ------------------------------------------------------------
function OnPlaySoundtrack1()
	OnPlayMusic(1);
end
function OnPlaySoundtrack2()
	OnPlayMusic(2);
end
function OnPlaySoundtrack3()
	OnPlayMusic(3);
end

function OnShowNotes1()
	ShowMusicNotesScreen(1);
end
function OnShowNotes2()
	ShowMusicNotesScreen(2);
end
function OnShowNotes3()
	ShowMusicNotesScreen(3);
end

function OnPrevMusic()
	assert(musicPage);
	if (musicPage:IsPlaying()) then
		musicPage.currentTrack = (musicPage.currentTrack > 1 and musicPage.currentTrack - 1 or 3);
		OnPlayMusic(trackIndex);
	end
end
function OnPlayMusicDefault()
    OnPlayMusic();
end
function OnPlayMusic(trackIndex)
	assert(musicPage);
	local trackIndexToPlay = (trackIndex and trackIndex or musicPage.currentTrack);
	musicPage:PlaySoundtrack(trackIndexToPlay);

	musicPage.navigationButtons['Play']:Stop();
	musicPage.navigationButtons['Play']:SetVisible(false);
	musicPage.navigationButtons['Stop']:Go();
	musicPage.navigationButtons['Stop']:SetVisible(true);
	musicPage.navigationButtons['Save']:Go();
	musicPage.navigationButtons['Save']:SetVisible(true);
end
function OnStopMusic()
	assert(musicPage);
	musicPage:StopPlayback();

	musicPage.navigationButtons['Stop']:Stop();
	musicPage.navigationButtons['Stop']:SetVisible(false);
	musicPage.navigationButtons['Play']:Go();
	musicPage.navigationButtons['Play']:SetVisible(true);
	musicPage.navigationButtons['Save']:Stop();
	musicPage.navigationButtons['Save']:SetVisible(false);
end
function OnNextMusic()
	if (musicPage:IsPlaying()) then
		musicPage.currentTrack = (musicPage.currentTrack < 3 and musicPage.currentTrack + 1 or 1);
		OnPlayMusic(trackIndex);
	end
end

function OnSaveResource()
	assert(extraContentScreen);

	local musicSource = {
		'data/Texture/GUI/Extras/PageMusic/Tracks/fon_3.wma',
		'data/Texture/GUI/Extras/PageMusic/Tracks/fon_5.wma',
		'data/Texture/GUI/Extras/PageMusic/Tracks/fon_6.wma'
	};
	local destinationName = {
		'Edgar Allan Poe - '..GetString('EXTRACONTENT_SCR_1ST_TACK_NAME')..'.wma',
		'Edgar Allan Poe - '..GetString('EXTRACONTENT_SCR_2ND_TACK_NAME')..'.wma',
		'Edgar Allan Poe - '..GetString('EXTRACONTENT_SCR_3RD_TACK_NAME')..'.wma'
	};

	if (musicPage:IsPlaying()) then
		ShowResourceSavingConfirmationScreen('EXTRACONTENT_SOUNDTRACK_SAVE_TEXT', 
											 musicSource[musicPage.currentTrack],
											 destinationName[musicPage.currentTrack]);
	end
end
