require "ISUI/ISPanel"
require "ISUI/ISButton"
require "ISUI/ISInventoryPane"
require "ISUI/ISResizeWidget"
require "ISUI/ISMouseDrag"

require "defines"

NewGameScreen = ISPanelJoypad:derive("NewGameScreen");


function NewGameScreen:initialise()
	ISPanel.initialise(self);
end


	--************************************************************************--
	--** ISPanel:instantiate
	--**
	--************************************************************************--
function NewGameScreen:instantiate()

	--self:initialise();
	self.javaObject = UIElement.new(self);
	self.javaObject:setX(self.x);
	self.javaObject:setY(self.y);
	self.javaObject:setHeight(self.height);
	self.javaObject:setWidth(self.width);
	self.javaObject:setAnchorLeft(self.anchorLeft);
	self.javaObject:setAnchorRight(self.anchorRight);
	self.javaObject:setAnchorTop(self.anchorTop);
	self.javaObject:setAnchorBottom(self.anchorBottom);
end

function NewGameScreen:create()
    self.mainPanel = ISPanel:new(0, 48, self:getWidth() / 2, self:getHeight() - (48*2));
    self.mainPanel:initialise();
    self.mainPanel:instantiate();
    self.mainPanel:setAnchorRight(false);
    self.mainPanel:setAnchorLeft(true);
    self.mainPanel:setAnchorTop(true);
    self.mainPanel:setAnchorBottom(true);
    self.mainPanel:noBackground();
    self.mainPanel.borderColor = {r=0, g=0, b=0, a=0};
    self.mainPanel:setScrollChildren(true);

    self.mainPanel.render = NewGameScreen.subPanelRender;
    self.mainPanel.prerender = NewGameScreen.subPanelPreRender;
    self.mainPanel.onMouseWheel = NewGameScreen.MainPanel_onMouseWheel

    self.mainPanel:addScrollBars();
    self:addChild(self.mainPanel);

    local y = 10;
    local x = 40;
    local xoffset = 20;
    local xoffsetdifficulty = 170;

    self.tutorial = ISLabel:new(x, y, 25, getText("UI_NewGame_Tutorial"), 1, 1, 1, 1, UIFont.NewLarge, true);
    self.tutorial.internal = "Tutorial";
    self.tutorial.mode = "Tutorial";
    self.tutorial.desc = getText("UI_NewGame_Tutorial_desc");
    self.tutorial.thumb = "media/ui/spiffoSkipping.png";
    self.tutorial:initialise();
    self.mainPanel:addChild(self.tutorial);
    self.tutorial.onMouseDown = NewGameScreen.onMenuItemMouseDown;
    y = y + 20;

    local sep1 = ISLabel:new(x, y, 25, "_________________________________________", 0.5, 0.5, 0.5, 1, UIFont.NewLarge, true);
    sep1:initialise();
    self.mainPanel:addChild(sep1);
    y = y + 30;

    local scenarios = ISLabel:new(x, y, 25, getText("UI_NewGame_Scenarios"), 1, 1, 1, 1, UIFont.NewLarge, true);
    scenarios:initialise();
    self.mainPanel:addChild(scenarios);

    local mediumFontHgt = getTextManager():getFontFromEnum(UIFont.NewMedium):getLineHeight() + 4
    local largeFontHgt = getTextManager():getFontFromEnum(UIFont.NewLarge):getLineHeight() + 6

    self.difficulty = ISLabel:new(x + xoffsetdifficulty, y, largeFontHgt, getText("UI_NewGame_StartingCondition"), 1, 1, 1, 1, UIFont.NewLarge, true);
    self.difficulty:initialise();
    self.mainPanel:addChild(self.difficulty);
    self.difficulty:setVisible(false);
    y = y + largeFontHgt + 4;

    local practiceRun = ISLabel:new(x+xoffset, y, mediumFontHgt, getText("UI_NewGame_InitialInfection"), 1, 1, 1, 1, UIFont.NewMedium, true);
    practiceRun.internal = "PRACTICE";
    practiceRun.mode = "Initial Infection";
    practiceRun.desc = getText("UI_NewGame_InitialInfection_desc");
    practiceRun.thumb = "media/ui/spiffoSkipping.png";
    practiceRun:initialise();
    self.mainPanel:addChild(practiceRun);
    practiceRun.onMouseDown = NewGameScreen.onMenuItemMouseDown;

    self.difficultyEasy = ISLabel:new(x + xoffset + xoffsetdifficulty, y, mediumFontHgt, getText("UI_StarterCondition_Easy"), 1, 1, 1, 1, UIFont.NewMedium, true);
    self.difficultyEasy:initialise();
    self.difficultyEasy.difficulty = "Easy";
    self.difficultyEasy.internal = "EASY";
    self.difficultyEasy:setTooltip(getText("UI_StarterCondition_Easy_desc"):gsub("\\n", "\n"));
    self.mainPanel:addChild(self.difficultyEasy);
    self.difficultyEasy:setVisible(false);
    self.difficultyEasy:setOnMouseDoubleClick(self, NewGameScreen.dblClickDifficulty);
    self.difficultyEasy.onMouseDown = NewGameScreen.onChooseDifficulty;
    y = y + 30;

    local begining = ISLabel:new(x+xoffset, y, mediumFontHgt, getText("UI_NewGame_FirstWeek"), 1, 1, 1, 1, UIFont.NewMedium, true);
    begining.internal = "BEGINING";
    begining.mode = "The First Week";
    begining.desc = getText("UI_NewGame_FirstWeek_desc");
    begining.thumb = "media/ui/spiffoSkipping.png"
    begining:initialise();
    self.mainPanel:addChild(begining);
    begining.onMouseDown = NewGameScreen.onMenuItemMouseDown;

    self.difficultyNormal = ISLabel:new(x+xoffset + xoffsetdifficulty, y, mediumFontHgt, getText("UI_StarterCondition_Normal"), 1, 1, 1, 1, UIFont.NewMedium, true);
    self.difficultyNormal:initialise();
    self.difficultyNormal.difficulty = "Normal";
    self.difficultyNormal.internal = "NORMAL";
    self.difficultyNormal:setTooltip(getText("UI_StarterCondition_Normal_desc"):gsub("\\n", "\n"));
    self.mainPanel:addChild(self.difficultyNormal);
    self.difficultyNormal:setVisible(false);
    self.difficultyNormal:setOnMouseDoubleClick(self, NewGameScreen.dblClickDifficulty);
    self.difficultyNormal.onMouseDown = NewGameScreen.onChooseDifficulty;
    y = y + 30;

    local ends = ISLabel:new(x+xoffset, y, mediumFontHgt, getText("UI_NewGame_SixMonths"), 1, 1, 1, 1, UIFont.NewMedium, true);
    ends.internal = "ENDS";
    ends.mode = "Six Months Later";
    ends.desc = getText("UI_NewGame_SixMonths_desc");
    ends.thumb = "media/ui/spiffoPanic.png"
    ends:initialise();
    self.mainPanel:addChild(ends);
    ends.onMouseDown = NewGameScreen.onMenuItemMouseDown;

    self.difficultyHard = ISLabel:new(x+xoffset + xoffsetdifficulty, y, mediumFontHgt, getText("UI_StarterCondition_Hard"), 1, 1, 1, 1, UIFont.NewMedium, true);
    self.difficultyHard:initialise();
    self.difficultyHard.difficulty = "Hard";
    self.difficultyHard.internal = "HARD";
    self.difficultyHard:setTooltip(getText("UI_StarterCondition_Hard_desc"):gsub("\\n", "\n"));
    self.mainPanel:addChild(self.difficultyHard);
    self.difficultyHard:setVisible(false);
    self.difficultyHard:setOnMouseDoubleClick(self, NewGameScreen.dblClickDifficulty);
    self.difficultyHard.onMouseDown = NewGameScreen.onChooseDifficulty;
    y = y + 30;

    local survival = ISLabel:new(x+xoffset, y, mediumFontHgt, getText("UI_NewGame_Survival"), 1, 1, 1, 1, UIFont.NewMedium, true);
    survival.internal = "SURVIVAL";
    survival.mode = "Survival";
    survival.desc = getText("UI_NewGame_Survival_desc");
    survival.thumb = "media/ui/spiffoSurvivor.png"
    survival:initialise();
    self.mainPanel:addChild(survival);
    survival.onMouseDown = NewGameScreen.onMenuItemMouseDown;

    self.difficultyHardcore = ISLabel:new(x+xoffset + xoffsetdifficulty, y, mediumFontHgt,  getText("UI_StarterCondition_Hardcore"), 1, 1, 1, 1, UIFont.NewMedium, true);
    self.difficultyHardcore:initialise();
    self.difficultyHardcore.difficulty = "Hardcore";
    self.difficultyHardcore.internal = "HARDCORE";
    self.difficultyHardcore:setTooltip(getText("UI_StarterCondition_Hardcore_desc"):gsub("\\n", "\n"));
    self.mainPanel:addChild(self.difficultyHardcore);
    self.difficultyHardcore:setVisible(false);
    self.difficultyHardcore:setOnMouseDoubleClick(self, NewGameScreen.dblClickDifficulty);
    self.difficultyHardcore.onMouseDown = NewGameScreen.onChooseDifficulty;
    y = y + 20;

    local sep2 = ISLabel:new(x, y, 25, "_________________________________________", 0.5, 0.5, 0.5, 1, UIFont.NewLarge, true);
    sep2:initialise();
    self.mainPanel:addChild(sep2);
    y = y + 30;

    local sandbox = ISLabel:new(x, y, largeFontHgt, getText("UI_NewGame_Sandbox"), 1, 1, 1, 1, UIFont.NewLarge, true);
    sandbox.internal = "SANDBOX";
    sandbox.mode = "Sandbox";
    sandbox.desc = getText("UI_NewGame_Sandbox_desc");
    sandbox.thumb = "media/ui/spiffoPacking.png"
    sandbox:initialise();
    self.mainPanel:addChild(sandbox);
    sandbox.onMouseDown = NewGameScreen.onMenuItemMouseDown;
    sandbox:setOnMouseDoubleClick(self, NewGameScreen.dblClickChallenge);
    y = y + 20;

    local sep3 = ISLabel:new(x, y, 25, "_________________________________________", 0.5, 0.5, 0.5, 1, UIFont.NewLarge, true);
    sep3:initialise();
    self.mainPanel:addChild(sep3);
    y = y + 30;

    local challenges = ISLabel:new(x, y, largeFontHgt, getText("UI_NewGame_Challenges"), 1, 1, 1, 1, UIFont.NewLarge, true);
    challenges:initialise();
    self.mainPanel:addChild(challenges);
    y = y + largeFontHgt + 4;

    local challenges = {}
    table.sort(LastStandChallenge, function(a,b) return a.name < b.name end)
    for i,info in ipairs(LastStandChallenge) do
        local challenge = ISLabel:new(x+xoffset, y, mediumFontHgt, info.name, 1, 1, 1, 1, UIFont.NewMedium, true);
        challenge:initialise();
        challenge.internal = info.name;
        challenge.challenge= info;
        challenge.desc = info.description or "NO DESCRIPTION";
        challenge.mode = "Challenge";
        challenge.thumb = info.image;
        self.mainPanel:addChild(challenge);
        challenge:setOnMouseDoubleClick(self, NewGameScreen.dblClickChallenge);
        challenge.onMouseDown = NewGameScreen.onMenuItemMouseDown;
        table.insert(challenges, challenge)
        y = y + 30;
    end

    self.mainPanel:setScrollHeight(self:getScrollHeight() + y);

    local width = 0
    for _,child in pairs(self.mainPanel:getChildren()) do
        if child.internal then
            child.prerender = NewGameScreen.prerenderBottomPanelLabel;
            child.setJoypadFocused = NewGameScreen.Label_setJoypadFocused
        end
        if child ~= self.mainPanel.vscroll then
            width = math.max(width, child:getRight())
        end
    end
    width = width + self.mainPanel.vscroll:getWidth() + 4
    self.mainPanel:setX((self.width / 2 - width) / 2)
    self.mainPanel:setWidth(width)

    self:insertNewLineOfButtons(self.tutorial)
    self:insertNewLineOfButtons(practiceRun, self.difficultyEasy)
    self:insertNewLineOfButtons(begining, self.difficultyNormal)
    self:insertNewLineOfButtons(ends, self.difficultyHard)
    self:insertNewLineOfButtons(survival, self.difficultyHardcore)
    self:insertNewLineOfButtons(sandbox)
    for _,challenge in ipairs(challenges) do
        self:insertNewLineOfButtons(challenge)
    end
--    self.joypadIndex = 1

    self.backButton = ISButton:new(16, self.height-30, 100, 25, getText("UI_btn_back"), self, NewGameScreen.onOptionMouseDown);
	self.backButton.internal = "BACK";
	self.backButton:initialise();
	self.backButton:instantiate();
	self.backButton:setAnchorLeft(true);
	self.backButton:setAnchorTop(false);
	self.backButton:setAnchorBottom(true);
	self.backButton.borderColor = {r=1, g=1, b=1, a=0.1};

	self.backButton:setFont(UIFont.Small);
	self.backButton:ignoreWidthChange();
	self.backButton:ignoreHeightChange();
	self:addChild(self.backButton);
	
--	self.vehiclesButton = ISTickBox:new(self.width - 286, self.height-30, 150, 25, "", self, NewGameScreen.onVehicleEnable);
--	self.vehiclesButton.internal = "VEHICLES";
--	self.vehiclesButton:initialise();
--	self.vehiclesButton:instantiate();
--	self.vehiclesButton:setAnchorLeft(false);
--	self.vehiclesButton:setAnchorRight(true);
--	self.vehiclesButton:setAnchorTop(false);
--	self.vehiclesButton:setAnchorBottom(true);
--	self.vehiclesButton.borderColor = {r=1, g=1, b=1, a=0.1};

--	self.vehiclesButton:setFont(UIFont.Medium);
--	self.vehiclesButton:ignoreWidthChange();
--	self.vehiclesButton:ignoreHeightChange();
--	self:addChild(self.vehiclesButton);
--	self.vehiclesButton:addOption(getText("UI_btn_experimentalVehicles"));
--	self.vehiclesButton:setSelected(1, getCore():isVehiclesEnable())
--	if not getCore():isVehiclesWarningShow() then
--		self.vehiclesButton.choicesColor = {r=1.0, g=0.3, b=0.2, a=1};
--	end
--	self:insertNewLineOfButtons(self.vehiclesButton)

	self.newButton = ISButton:new(self.width - 116, self.height-30, 100, 25, getText("UI_btn_next"), self, NewGameScreen.onOptionMouseDown);
	self.newButton.internal = "NEXT";
	self.newButton:initialise();
	self.newButton:instantiate();
	self.newButton:setAnchorLeft(false);
	self.newButton:setAnchorRight(true);
	self.newButton:setAnchorTop(false);
	self.newButton:setAnchorBottom(true);
	self.newButton.borderColor = {r=1, g=1, b=1, a=0.1};

	self.newButton:setFont(UIFont.Small);
	self.newButton:ignoreWidthChange();
	self.newButton:ignoreHeightChange();
	self:addChild(self.newButton);

    self.richText = ISRichTextPanel:new(16, 10, 500,200);
    self.richText:initialise();
    self.richText.autosetheight = false;
    self.richText.background = false;
    self.richText.clip = true
    self.richText.marginRight = 20
    self.richText:setAnchorBottom(true);
    self.richText:setAnchorRight(true);
--    self.richText:setVisible(false);
    self:addChild(self.richText);
    self.richText:addScrollBars()

	self:setVisible(false);
end

NewGameScreen.onVehicleEnable = function(changeOptionTarget, mouseOverOption, selected, changeOptionArgs1, changeOptionArgs2)
	getCore():setVehiclesEnable(selected)
	getCore():saveOptions()
	if not getCore():isVehiclesWarningShow() then
		local modal = ISVehicleConfirmBox:new();
		modal:initialise();
		modal:addToUIManager();
		modal.parent = changeOptionTarget
		modal:setVisible(true, changeOptionTarget.joyfocus)
	end
end

NewGameScreen.onChooseDifficulty = function(item, x, y)
    if item.disabled then return; end
    NewGameScreen.instance.selectedDifficulty = item;
end

NewGameScreen.dblClickChallenge = function(item, x, y)
    NewGameScreen.onMenuItemMouseDown(item, x, y)
    NewGameScreen.instance:clickPlay();
end

NewGameScreen.dblClickDifficulty = function(item, x, y)
    NewGameScreen.onChooseDifficulty(item,x,y);
    NewGameScreen.instance:clickPlay();
end

NewGameScreen.onMenuItemMouseDown = function(item, x, y)
    local screen = NewGameScreen.instance;
    screen.selectedItem = item;

    screen.selectedDifficulty = nil;
    screen.difficulty:setVisible(false);
    screen.difficultyEasy:setVisible(false);
    screen.difficultyNormal:setVisible(false);
    screen.difficultyHard:setVisible(false);
    screen.difficultyHardcore:setVisible(false);

    if screen:needDifficulty() then
        screen.difficulty:setVisible(true);
        screen.difficultyEasy:setVisible(true);
        screen.difficultyNormal:setVisible(true);
        screen.difficultyHard:setVisible(true);
        screen.difficultyHardcore:setVisible(true);
        screen.difficultyEasy.disabled = false;
        screen.difficultyNormal.disabled = false;
        screen.difficultyHard.disabled = false;
        screen.difficultyEasy.a = 1;
        screen.difficultyNormal.a = 1;
        screen.difficultyHard.a = 1;
    elseif  screen.selectedItem.internal == "SURVIVAL" then
        screen.difficultyHardcore:setVisible(true);
        screen.difficulty:setVisible(true);
        screen.difficultyEasy:setVisible(true);
        screen.difficultyNormal:setVisible(true);
        screen.difficultyHard:setVisible(true);
        screen.difficultyEasy.disabled = true;
        screen.difficultyNormal.disabled = true;
        screen.difficultyHard.disabled = true;
        screen.difficultyEasy.a = 0.3;
        screen.difficultyNormal.a = 0.3;
        screen.difficultyHard.a = 0.3;
        screen.selectedDifficulty = screen.difficultyHardcore;
    end
end


function NewGameScreen:needDifficulty()
    if self.selectedItem then
        return self.selectedItem.internal == "PRACTICE" or self.selectedItem.internal == "BEGINING" or self.selectedItem.internal == "ENDS";
    end
    return false;
end

function NewGameScreen:prerenderBottomPanelLabel()
    local padLeft = 6
    local padRight = 6
    local alpha = 0.5
    if NewGameScreen.instance.selectedItem == self or NewGameScreen.instance.selectedDifficulty == self then
        self:drawRect(0 - padLeft, 0, self:getWidth() + padLeft + padRight, self:getHeight(), alpha, 0.3, 0.3, 0.3)
        if self.joypadFocused then
            self:drawRectBorder(0 - padLeft, 0, self:getWidth() + padLeft + padRight, self:getHeight(), 0.9, 0.6, 0.6, 0.6)
        else
            self:drawRectBorder(0 - padLeft, 0, self:getWidth() + padLeft + padRight, self:getHeight(), 0.9, 0.3, 0.3, 0.3)
        end
    elseif self.fadeOut or self.fadeIn then
        if self.fadeIn then
            local fadeIn = getPerformance():getUIRenderFPS() / 12
            alpha = 0.5 * (self.fadeIn / fadeIn)
            self.fadeIn = math.min(self.fadeIn + 1, fadeIn)
        else
            local fadeOut = getPerformance():getUIRenderFPS() / 4
            alpha = 0.5 * (1 - self.fadeOut / fadeOut)
            self.fadeOut = math.min(self.fadeOut + 1, fadeOut)
            if self.fadeOut == fadeOut then
                self.fadeOut = nil
            end
        end
        self:drawRect(0 - padLeft, 0, self:getWidth() + padLeft + padRight, self:getHeight(), alpha, 0.3, 0.3, 0.3)
    end
    ISLabel.prerender(self)
end

function NewGameScreen:update()
    self:updateBottomPanelButtons();
    self:disableBtn();
end

function NewGameScreen:disableBtn()
    self.newButton:setEnable(false);
    self.newButton:setTooltip(nil);
    if self.selectedItem then
        if (self:needDifficulty() and self.selectedDifficulty) or not self:needDifficulty() then
           self.newButton:setEnable(true);
        else
            self.newButton:setTooltip(getText("UI_NewGame_SelectDifficulty"))
        end
    end
end

function NewGameScreen:updateBottomPanelButtons()
    local overButton = nil
    for _,child in pairs(self.mainPanel:getChildren()) do
        if not child.disabled and child:isMouseOver() or child.joypadFocused then
            overButton = child
            break
        end
    end
    if overButton ~= self.overBottomPanelButton then
        if self.overBottomPanelButton then
            self.overBottomPanelButton.fadeIn = nil
            self.overBottomPanelButton.fadeOut = 0
        end
        self.overBottomPanelButton = overButton
        if self.overBottomPanelButton then
            self.overBottomPanelButton.fadeIn = 0
            self.overBottomPanelButton.fadeOut = nil
        end
    end
end

function NewGameScreen:render()
    local selectedItem = self.selectedItem;
    local selectedDifficulty = self.selectedDifficulty;
    if not selectedItem then return; end

    local thumbHeight = 256;
    local thumbY = self.startY + 8

    local text = ""
    if selectedItem.thumb then
        text = " <IMAGECENTRE:" .. selectedItem.thumb .. "> <LINE> "
    end
    local descRectWidth = self.width - 37 - (self.width/2 + 30)
    local descRectHeight = self.mainPanel:getHeight()

    self.richText:setX(self.width/2 + 30)
    self.richText:setY(self.mainPanel:getY())
    self.richText:setWidth(descRectWidth)
    self.richText:setHeight(descRectHeight)
    text = text .. " <H1> " .. selectedItem.name .. " <LINE> ";
    if selectedDifficulty then
        text = text .. " <H1> " .. getText("UI_Difficulty") .. ": " .. selectedDifficulty.name .. " <LINE> <LINE> <H2> ";
    else
        text = text .. " <LINE> <H2> ";
    end
    text = text .. selectedItem.desc;
    self.richText.text = text;
    self.richText:paginate();
    self:drawRectBorder( self.richText:getX(), self.richText:getY(), self.richText:getWidth(), self.richText:getHeight(), 0.3, 1, 1, 1);
end

function NewGameScreen:prerender()
	ISPanel.prerender(self);
	self:drawTextCentre(getText("UI_NewGameScreen_title"), self.width / 2, 10, 1, 1, 1, 1, UIFont.Cred2);
	self:ensureVisible()
end

function NewGameScreen:onOptionMouseDown(button, x, y)
     if self.modal then
         self.modal:setVisible(false);
         self.modal = nil;
     end
	 if button.internal == "BACK" then
         self:setVisible(false);
         MainScreen.instance.bottomPanel:setVisible(true);
         if self.joyfocus then
             self:clearJoypadFocus(self.joyfocus)
             self.joypadIndex = 1
             self.joypadIndexY = 1
             self.joypadButtons = self.joypadButtonsY[self.joypadIndexY]
             self.joyfocus.focus = MainScreen.instance;
             updateJoypadFocus(self.joyfocus);
         end
	 end
	 if button.internal == "NEXT" then
		self:clickPlay();
  	 end
end

function NewGameScreen:clickPlay()
    self:setVisible(false);

    MainScreen.instance.charCreationMain.previousScreen = "NewGameScreen";
    getWorld():setGameMode(self.selectedItem.mode);

    if self.selectedItem.mode == "Tutorial" then
        MainScreen.startTutorial();
        return;
    end

	MainScreen.instance:setDefaultSandboxVars()

    if self.selectedItem.mode == "Challenge" then
        getWorld():setDifficulty("Hardcore");
        LastStandData.chosenChallenge = self.selectedItem.challenge;
        local worldName = ZombRand(100000)..ZombRand(100000)..ZombRand(100000)..ZombRand(100000);
        doChallenge(self.selectedItem.challenge);
        getWorld():setWorld(worldName);
        if getCore():getGameMode() ~= "LastStand" then
            MainScreen.instance.createWorld = true
            CharacterCreationProfession.instance.doSpawnPoint = false;
            MainScreen.instance.charCreationMain.previousScreen = "NewGameScreen";
            MainScreen.instance.charCreationMain:setVisible(true, self.joyfocus);
        elseif #MainScreen.instance.lastStandPlayerSelect.listbox.items > 0 then
            createWorld(worldName);
            MainScreen.instance.lastStandPlayerSelect:setVisible(true, self.joyfocus);
        else
            createWorld(worldName);
            MainScreen.instance.charCreationProfession.doSpawnPoint = false;
            MainScreen.instance.charCreationMain.previousScreen = "NewGameScreen"
            MainScreen.instance.charCreationMain:setVisible(true, self.joyfocus);
        end
        return;
    end

    if self.selectedDifficulty then
        getWorld():setDifficulty(self.selectedDifficulty.difficulty);
    end
    if self.selectedItem.mode == "Initial Infection" then
        MainScreen.instance:setSandboxPreset(MainScreen.instance.sandOptions:getBeginnerPreset());
    end

    if self.selectedItem.mode == "The First Week" then
        MainScreen.instance:setSandboxPreset(MainScreen.instance.sandOptions:getNormalPreset());
    end

    if self.selectedItem.mode == "Six Months Later" then
        MainScreen.instance:setSandboxPreset(MainScreen.instance.sandOptions:getHardPreset());
    end

    if self.selectedItem.mode == "Survival" then
        MainScreen.instance:setSandboxPreset(MainScreen.instance.sandOptions:getDefaultPreset());
    end

    if self.selectedDifficulty then
        if self.selectedDifficulty.internal == "EASY" then
            MainScreen.instance:setEasyPreset();
        end
        if self.selectedDifficulty.internal == "NORMAL" then
            MainScreen.instance:setNormalPreset();
        end
        if self.selectedDifficulty.internal == "HARD" then
            MainScreen.instance:setHardPreset();
        end
        if self.selectedDifficulty.internal == "HARDCORE" then
            MainScreen.instance:setHardcorePreset();
        end
    end
    
    getWorld():setMap("DEFAULT")
    MainScreen.instance.createWorld = true;
    if getWorld():getGameMode() == "Sandbox" then
        getWorld():setDifficulty("Hardcore")
        if WorldSelect.instance:hasChoices() then
            WorldSelect.instance:fillList()
            WorldSelect.instance.previousScreen = "NewGameScreen"
            WorldSelect.instance:setVisible(true, self.joyfocus)
        elseif MainScreen.instance.createWorld or MapSpawnSelect.instance:hasChoices() then
            MapSpawnSelect.instance:fillList()
            MapSpawnSelect.instance.previousScreen = "NewGameScreen"
            MapSpawnSelect.instance:setVisible(true, self.joyfocus)
        else
            MapSpawnSelect.instance:useDefaultSpawnRegion()
            MainScreen.instance.sandOptions:setVisible(true, self.joyfocus)
        end
    else
        if WorldSelect.instance:hasChoices() then
            WorldSelect.instance:fillList()
            WorldSelect.instance.previousScreen = "NewGameScreen"
            WorldSelect.instance:setVisible(true, self.joyfocus)
        elseif MainScreen.instance.createWorld or MapSpawnSelect.instance:hasChoices() then
            MapSpawnSelect.instance:fillList()
            MapSpawnSelect.instance.previousScreen = "NewGameScreen"
            MapSpawnSelect.instance:setVisible(true, self.joyfocus)
        else
            MainScreen.instance.charCreationMain.previousScreen = "NewGameScreen"
            MainScreen.instance.charCreationMain:setVisible(true, self.joyfocus)
        end
    end
end

function NewGameScreen:onGainJoypadFocus(joypadData)
    ISPanelJoypad.onGainJoypadFocus(self, joypadData);
    self:setISButtonForB(self.backButton);
    self:setISButtonForA(self.newButton);
    if self.joypadIndex == 0 then
        self.joypadIndex = 1
        self.joypadIndexY = 1
        self.joypadButtons = self.joypadButtonsY[1]
    end
    if #self.joypadButtons >= 1 and self.joypadIndex <= #self.joypadButtons then
        self.joypadButtons[self.joypadIndex]:setJoypadFocused(true, joypadData)
    end
end

function NewGameScreen:Label_setJoypadFocused(focused)
    self.joypadFocused = focused
    self:onMouseDown(0, 0)
end

function NewGameScreen:ensureVisible()
    local child = self.joypadButtons[self.joypadIndex]
    if not child then return end
    local y = child:getY()
    if y - 40 < 0 - self.mainPanel:getYScroll() then
        self.mainPanel:setYScroll(0 - y + 40)
    elseif y + child:getHeight() + 40 > 0 - self.mainPanel:getYScroll() + self.mainPanel:getHeight() then
        self.mainPanel:setYScroll(0 - (y + child:getHeight() + 40 - self.mainPanel:getHeight()))
    end
end

function NewGameScreen:onResolutionChange(oldw, oldh, neww, newh)
    self.mainPanel:setX((self.width / 2 - self.mainPanel:getWidth()) / 2)
end

function NewGameScreen:subPanelPreRender()
    self:setStencilRect(0,0,self:getWidth(),self:getHeight());

    ISPanel.prerender(self);
end

function NewGameScreen:subPanelRender()
    ISPanel.render(self);
    self:clearStencilRect();
end

function NewGameScreen:MainPanel_onMouseWheel(del)
	self:setYScroll(self:getYScroll() - (del * 40))
	return true
end

function NewGameScreen:new (x, y, width, height)
	local o = {}
	--o.data = {}
	o = ISPanelJoypad:new(x, y, width, height);
	setmetatable(o, self)
	self.__index = self
	o.x = x;
	o.y = y;
	o.backgroundColor = {r=0, g=0, b=0, a=0.8};
	o.borderColor = {r=1, g=1, b=1, a=0.2};
	o.width = width;
	o.height = height;
	o.anchorLeft = true;
	o.anchorRight = false;
	o.anchorTop = true;
	o.anchorBottom = false;
	o.itemheightoverride = {}
	o.selected = 1;
    o.startY = 80;
	NewGameScreen.instance = o;
	return o
end
