require( "scripts/style.lua" );

function tome_footer()
  t = 
  {      
    Text
    {
      font = TomeOfLoreTitleFont,
      name = "titre_tome_of_lore",
      label = "tooltip_tome",
      x = 177, 
      y = 485,
      w=150,
      h=50,
      flags = kVAlignCenter + kHAlignCenter,
    },
    
    Text
    {
      font = TomeOfLoreTitleFont,
      name = "titre_tome_of_lore",
      label = "tooltip_tome",
      x = 587, 
      y = 485,
      w=150,
      h=50,
      flags = kVAlignCenter + kHAlignCenter,
    }
  }
  
	table.insert( t, BeginGroup() );
	table.insert( t, SetStyle(TabButtonStyle) );
	table.insert( t,
		Button
		{
			x = kCenter + 438,
			y = 40,
			name="tome_quit",
			--label="quit_cross",
			type = kPush,
			close = true,
			graphics = {
						"buttons/tab_right_quit",
						"buttons/tab_right_quit",
						"buttons/tab_right_quit_over"
					   },
      command = 
        function()
          if IsGameInLinearTuto() then
            DisplayTutorialCallback("TUTO_Tome_of_lore_end");
          end
        end
		}
	);

	return Group(t);
end

function SortQuest()
    local tPBQuests = GetPrimaryBegunQuestsToDisplay();
    local tPCQuests = GetPrimaryCompletedQuestsToDisplay();
	local tPIPQuests = GetPrimaryInProgressQuestsToDisplay();
    local tSBQuests = GetSecondaryBegunQuestsToDisplay();
    local tSCQuests = GetSecondaryCompletedQuestsToDisplay();
	local tSIPQuests = GetSecondaryInProgressQuestsToDisplay();
    local finalbcQuests = {}; --Table finale contenant les flags begun ou completed ou en cours
    local finalQuests = {}; --Table finale contenant les quetes

    local nbprimary = table.getn(tPBQuests) + table.getn(tPCQuests) + table.getn(tPIPQuests);
    local i = 1;
    local j = 1;

    --Primary begun
    for key, value in ipairs(tPBQuests) do
        finalbcQuests[i] = 0;
        i = i + 1;
    end
    --Primary completed
    for key, value in ipairs(tPCQuests) do
        finalbcQuests[i] = 1;
        i = i + 1;
    end
	for key, value in ipairs(tPIPQuests) do
        finalbcQuests[i] = 2;
        i = i + 1;
    end
    --Secondary begun
    for key, value in ipairs(tSBQuests) do
        finalbcQuests[i] = 0;
        i = i + 1;
    end
    --Secondary completed
    for key, value in ipairs(tSCQuests) do
        finalbcQuests[i] = 1;
        i = i + 1;
    end
	for key, value in ipairs(tSIPQuests) do
        finalbcQuests[i] = 2;
        i = i + 1;
    end

    finalQuests = tPBQuests;
    j = table.getn(finalQuests) + 1;
    --Concatenation avec quetes primaires completed
    for h in tPCQuests do
        finalQuests[j] = tPCQuests[h];
        j = j + 1;
    end
	 --Concatenation avec quetes primaires in progress
    for h in tPIPQuests do
        finalQuests[j] = tPIPQuests[h];
        j = j + 1;
    end
    --Concatenation avec quetes secondaires begun
    for h in tSBQuests do
        finalQuests[j] = tSBQuests[h];
        j = j + 1;
    end
    --Concatenation avec quetes secondaires completed
    for h in tSCQuests do
        finalQuests[j] = tSCQuests[h];
        j = j + 1;
    end
    --Concatenation avec quetes secondaires in progress
    for h in tSIPQuests do
        finalQuests[j] = tSIPQuests[h];
        j = j + 1;
    end
	
    local ret = {nbprimary, finalQuests, finalbcQuests};
	
	return ret;
end

function bestiary_footer( page )
	t = { BeginGroup() };
	
	local qPages = GetBestiaryPagesToDisplay();
	
	local iPage = 0;
	local find = false;
	local sPage = "bestiary_page"..page;
    
    SetBestiaryPage( sPage );
	
	while ( iPage ~= table.getn(qPages) and find == false ) do
		iPage = iPage + 1;
		if (qPages[iPage] == sPage) then
			find = true;
		end
	end
	
	if ( qPages[iPage + 1] ~= nil ) then
    table.insert( t, SetStyle( RightCornerButtonGraphics ) );
		table.insert( t,
      AnimatedButton
      {
        x = 817,
				y = 480,    
        name="animated_corner_right",
        graphics = RightCornerButtonGraphics,
        --label="map",
        type=kPush,
          
        image_uphover = "anim/widgets/corner_right_tome_of_lore.xml",
        image_up      = "buttons/corner_right_knowledge_tome_00.png",
        image_down    = "buttons/corner_right_knowledge_tome_00.png",
           
        command=
          function()
            SwapToModal( "scripts/"..qPages[iPage + 1]..".lua" );
          end
      }
		);
	end
	
	if ( qPages[iPage - 1] ~= nil ) then
		table.insert( t, SetStyle( LeftCornerButtonGraphics ) );
		table.insert( t,    
      AnimatedButton
      {
        x = 61,
				y = 480,    
        name="animated_corner_left",
        graphics = LeftCornerButtonGraphics,
        --label="map",
        type=kPush,
          
        image_uphover = "anim/widgets/corner_left_tome_of_lore.xml",
        image_up      = "buttons/corner_left_knowledge_tome_00.png",
        image_down    = "buttons/corner_left_knowledge_tome_00.png",
           
        command=
          function()
            SwapToModal( "scripts/"..qPages[iPage - 1]..".lua" );
          end
      }			
		);
	end
			
	return Group(t);
end

function tab_placement( chapter )
	t = { SetStyle( TabButtonStyle ), BeginGroup(), };
	
	local commands = { 
        function()
			SwapToModal( "scripts/sommaire.lua" );
		end,
		function()
            DisplayTutorialCallback( "TUTO_knowledge_tree_welcome" );
			SwapToModal( "scripts/work.lua" );
		end,
		function()
			DisplayTutorialCallback( "TUTO_tome_bestiary" );
			SwapToModal( "scripts/bestiary_page2.lua" );
		end,
		function()
			DisplayTutorialCallback( "TUTO_tome_quest" );
			SwapToModal( "scripts/quest_report.lua" );
		end,
		function()
			DisplayTutorialCallback( "TUTO_tome_collection_welcome" );
			SwapToModal( "scripts/collection.lua" );
		end,
	};
	
	local graphics = {
    {
	"buttons/tab_right_som",
	"buttons/tab_right_som",
	"buttons/tab_right_som_over"
	},
	{
	"buttons/tab_right_work",
	"buttons/tab_right_work",
	"buttons/tab_right_work_over"
	},
	{
	"buttons/tab_right_bestiary",
	"buttons/tab_right_bestiary",
	"buttons/tab_right_bestiary_over"
	},
	{
	"buttons/tab_right_quest",
	"buttons/tab_right_quest",
	"buttons/tab_right_quest_over"
	},
	{
	"buttons/tab_right_collection",
	"buttons/tab_right_collection",
	"buttons/tab_right_collection_over"
	},
	};
	
	local images = {
    "buttons/tab_right_som_over",
	"buttons/tab_right_work_over",
	"buttons/tab_right_bestiary_over",
	"buttons/tab_right_quest_over",
	"buttons/tab_right_collection_over"
	}
		
	local button_pos_x 			= kCenter + 438;
	local bitmap_pos_x			= kCenter - 413;
	local button_pos_y_begin 	= kCenter - 107;
	local button_pos_y_space	= 76;
	
	for i = 1, 5 do
		if i == chapter then
			table.insert( t,
				Bitmap
				{
				    name = "tab"..i,
					x = button_pos_x,
					y = button_pos_y_begin + button_pos_y_space * ( i - 1 ),
					image = images[i],
				}
			);
		else
			table.insert( t,
				Button
				{
				    name = "tab"..i,
					x = button_pos_x,
					y = button_pos_y_begin + button_pos_y_space * ( i - 1 ),

					command = commands[ i ],
					graphics = graphics[ i ],
				}
			);
		end
	end
	
	return Group(t);
end
