#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~

Copyright 2018 - 2023 OneLoneCoder.com

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following	disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS	"AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT	HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#pragma endregion
#include "State_OverworldMap.h"
#include "Crawler.h"
#include "DEFINES.h"
#include "Menu.h"
#include "Unlock.h"
#include "ConnectionPoint.h"
#include "util.h"
#include "drawutil.h"
#include "MenuLabel.h"
#include "EncountersSpawnListScrollableWindowComponent.h"
#include "VisualNovel.h"

INCLUDE_MONSTER_LIST
INCLUDE_game

std::vector<ConnectionPoint>State_OverworldMap::connections;

State_OverworldMap::State_OverworldMap(){
	SetStageMarker("Stage I-I"); //Eventually we will load the game from a file and this will not be necessary. We just set it to this for now.
}
void State_OverworldMap::OnStateChange(GameState*prevState){
	game->LoadLevel(WORLD_MAP);
	if(Menu::IsMenuOpen()){
		Menu::CloseAllMenus();
	}
	game->GetPlayer()->SetPos(currentConnectionPoint->rect.pos+currentConnectionPoint->rect.size/2+vf2d{0,16});
	playerTargetPos=currentConnectionPoint->rect.pos+currentConnectionPoint->rect.size/2+vf2d{0,16};
	game->GetPlayer()->UpdateWalkingAnimation(DOWN);
	game->GetPlayer()->SetState(State::FORCE_WALK);
	game->GetPlayer()->SetSizeMult(1);
	game->camera.MoveCamera(currentConnectionPoint->rect.middle()+vf2d{game->GetScreenSize().x/6.0f,0});
	Component<MenuLabel>(OVERWORLD_LEVEL_SELECT,"Chapter Label")->SetLabel("Chapter "+std::to_string(game->GetCurrentChapter()));
	Component<MenuLabel>(OVERWORLD_LEVEL_SELECT,"Stage Label")->SetLabel(currentConnectionPoint->name);
	Component<EncountersSpawnListScrollableWindowComponent>(OVERWORLD_LEVEL_SELECT,"Spawns List")->UpdateSpawns(currentConnectionPoint->spawns);
	Component<MenuComponent>(OVERWORLD_LEVEL_SELECT,"Enter Button")->Enable(currentConnectionPoint->levelDataExists);
	Menu::OpenMenu(OVERWORLD_LEVEL_SELECT,false);
};
void State_OverworldMap::OnUserUpdate(Crawler*game){
	game->camera.SetTarget(currentConnectionPoint->rect.middle()+vf2d{game->GetScreenSize().x/6.0f,0});
	game->UpdateCamera(game->GetElapsedTime());
	game->GetPlayer()->Update(game->GetElapsedTime());

	if(game->GetPlayer()->GetPos()!=playerTargetPos){
		if(geom2d::line<float>(game->GetPlayer()->GetPos(),playerTargetPos).length()<2){
			game->GetPlayer()->SetPos(playerTargetPos);
		}else{
			game->GetPlayer()->SetPos(game->GetPlayer()->GetPos()+util::pointTo(game->GetPlayer()->GetPos(),playerTargetPos)*playerMoveSpd*game->GetElapsedTime());
		}
	}

	for(ConnectionPoint&cp:connections){
		if(game->GetMouse(Mouse::LEFT).bPressed&&geom2d::overlaps(game->GetWorldMousePos(),cp.rect)){
			for(int neighborInd:currentConnectionPoint->neighbors){
				if(neighborInd==-1)continue;
				ConnectionPoint&neighbor=ConnectionPointFromIndex(neighborInd);
				if(Unlock::IsUnlocked(neighbor.unlockCondition)&&&cp==&neighbor){
					currentConnectionPoint=&neighbor;
					playerTargetPos=currentConnectionPoint->rect.pos+currentConnectionPoint->rect.size/2+vf2d{0,16};
					float angleTo=util::angleTo(game->GetPlayer()->GetPos(),playerTargetPos);
					if(angleTo>=-3*PI/4&&angleTo<-PI/4){
						game->GetPlayer()->UpdateWalkingAnimation(UP);
					}else
					if(angleTo<PI/4&&angleTo>=-PI/4){
						game->GetPlayer()->UpdateWalkingAnimation(RIGHT);
					}else
					if(angleTo>=PI/4&&angleTo<3*PI/4){
						game->GetPlayer()->UpdateWalkingAnimation(DOWN);
					}else{
						game->GetPlayer()->UpdateWalkingAnimation(LEFT);
					}
					Component<MenuLabel>(OVERWORLD_LEVEL_SELECT,"Stage Label")->SetLabel(currentConnectionPoint->name);
					Component<EncountersSpawnListScrollableWindowComponent>(OVERWORLD_LEVEL_SELECT,"Spawns List")->UpdateSpawns(currentConnectionPoint->spawns);
					Component<MenuComponent>(OVERWORLD_LEVEL_SELECT,"Enter Button")->Enable(currentConnectionPoint->levelDataExists);
					break;
				}
			}
		}
	}
};
void State_OverworldMap::Draw(Crawler*game){
	currentTime+=game->GetElapsedTime();
	for(ConnectionPoint&cp:connections){
		if(!Unlock::IsUnlocked(cp)){
			game->view.FillRectDecal(cp.rect.pos,cp.rect.size,{0,0,0,128});
		}
	}
	for(ConnectionPoint&cp:connections){
		if(Unlock::IsUnlocked(cp)&&geom2d::overlaps(game->GetWorldMousePos(),cp.rect)&&(&cp==currentConnectionPoint||cp.IsNeighbor(*currentConnectionPoint))){
			drawutil::DrawCrosshairDecalTransformedView(game->view,cp.rect,currentTime);
			break;
		}
	}
};
void State_OverworldMap::SetStageMarker(std::string connectionName){
	for(ConnectionPoint&connection:connections){
		if(connection.name==connectionName){
			currentConnectionPoint=&connection;
			return;
		}
	}
	ERR("WARNING! Could not find a connection point with name "<<connectionName<<"!");
}

ConnectionPoint&State_OverworldMap::ConnectionPointFromIndex(int ind){
	return connections.at(ind);
}

ConnectionPoint&State_OverworldMap::GetCurrentConnectionPoint(){
	return *((State_OverworldMap*)(GameState::states.at(States::OVERWORLD_MAP)))->currentConnectionPoint;
}

void State_OverworldMap::StartLevel(){
	if(State_OverworldMap::GetCurrentConnectionPoint().map.starts_with("STORY")){
		VisualNovel::LoadVisualNovel(State_OverworldMap::GetCurrentConnectionPoint().map);
	}else{
		game->LoadLevel(LEVEL_NAMES.at(State_OverworldMap::GetCurrentConnectionPoint().map));
		GameState::ChangeState(States::GAME_RUN);
	}
}