#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 "Animation.h"
#include "Crawler.h"
#include "DEFINES.h"
#include "safemap.h"

INCLUDE_game
INCLUDE_ANIMATION_DATA
INCLUDE_DATA
INCLUDE_GFX

void sig::Animation::InitializeAnimations(){

	auto CreateStillAnimation=[&](std::string imgName,vf2d size,AnimationData data={}){
		Animate2D::FrameSequence anim(data.frameDuration,data.style);
		anim.AddFrame({&GFX[imgName],{{0,0},size}});
		ANIMATION_DATA[imgName]=anim;
	};

	auto CreateHorizontalAnimationSequence=[&](std::string imgName,int frameCount,vf2d size,AnimationData data={}){
		Animate2D::FrameSequence anim(data.frameDuration,data.style);
		for(int i=0;i<frameCount;i++){
			anim.AddFrame({&GFX[imgName],{{int(i*size.x),0},size}});
		}
		ANIMATION_DATA[imgName]=anim;
	};

	auto SetupClassWalkIdleAnimations=[&](Renderable&sheet,std::string className){
		Animate2D::FrameSequence pl_walk_s{0.2f};
		pl_walk_s.AddFrame({&sheet,{vi2d{0,0}*24,{24,24}}});
		pl_walk_s.AddFrame({&sheet,{vi2d{1,0}*24,{24,24}}});
		pl_walk_s.AddFrame({&sheet,{vi2d{0,0}*24,{24,24}}});
		pl_walk_s.AddFrame({&sheet,{vi2d{2,0}*24,{24,24}}});
		ANIMATION_DATA[className+"_WALK_S"]=pl_walk_s;
		Animate2D::FrameSequence pl_walk_e{0.2f};
		pl_walk_e.AddFrame({&sheet,{vi2d{0,3}*24,{24,24}}});
		pl_walk_e.AddFrame({&sheet,{vi2d{1,3}*24,{24,24}}});
		pl_walk_e.AddFrame({&sheet,{vi2d{0,3}*24,{24,24}}});
		pl_walk_e.AddFrame({&sheet,{vi2d{2,3}*24,{24,24}}});
		ANIMATION_DATA[className+"_WALK_E"]=pl_walk_e;
		Animate2D::FrameSequence pl_walk_w{0.2f};
		pl_walk_w.AddFrame({&sheet,{vi2d{0,2}*24,{24,24}}});
		pl_walk_w.AddFrame({&sheet,{vi2d{1,2}*24,{24,24}}});
		pl_walk_w.AddFrame({&sheet,{vi2d{0,2}*24,{24,24}}});
		pl_walk_w.AddFrame({&sheet,{vi2d{2,2}*24,{24,24}}});
		ANIMATION_DATA[className+"_WALK_W"]=pl_walk_w;
		Animate2D::FrameSequence pl_walk_n{0.2f};
		pl_walk_n.AddFrame({&sheet,{vi2d{0,1}*24,{24,24}}});
		pl_walk_n.AddFrame({&sheet,{vi2d{1,1}*24,{24,24}}});
		pl_walk_n.AddFrame({&sheet,{vi2d{0,1}*24,{24,24}}});
		pl_walk_n.AddFrame({&sheet,{vi2d{2,1}*24,{24,24}}});
		ANIMATION_DATA[className+"_WALK_N"]=pl_walk_n;
		Animate2D::FrameSequence pl_idle_s;
		pl_idle_s.AddFrame({&sheet,{vi2d{0,0}*24,{24,24}}});
		ANIMATION_DATA[className+"_IDLE_S"]=pl_idle_s;
		Animate2D::FrameSequence pl_idle_e;
		pl_idle_e.AddFrame({&sheet,{vi2d{0,3}*24,{24,24}}});
		ANIMATION_DATA[className+"_IDLE_E"]=pl_idle_e;
		Animate2D::FrameSequence pl_idle_w;
		pl_idle_w.AddFrame({&sheet,{vi2d{0,2}*24,{24,24}}});
		ANIMATION_DATA[className+"_IDLE_W"]=pl_idle_w;
		Animate2D::FrameSequence pl_idle_n;
		pl_idle_n.AddFrame({&sheet,{vi2d{0,1}*24,{24,24}}});
		ANIMATION_DATA[className+"_IDLE_N"]=pl_idle_n;
	};

	//Warrior animations.
	SetupClassWalkIdleAnimations(GFX["nico-warrior.png"],"WARRIOR");
	Animate2D::FrameSequence pl_warrior_swing_s(0.05f),pl_warrior_swing_n(0.05f),pl_warrior_swing_e(0.05f),pl_warrior_swing_w(0.05f);
	Animate2D::FrameSequence pl_warrior_sonic_swing_s(0.1f,Animate2D::Style::OneShot),pl_warrior_sonic_swing_n(0.1f,Animate2D::Style::OneShot),pl_warrior_sonic_swing_e(0.1f,Animate2D::Style::OneShot),pl_warrior_sonic_swing_w(0.1f,Animate2D::Style::OneShot);
	for (int i=0;i<4;i++){
		pl_warrior_swing_s.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,0}*24,{24,24}}});
		pl_warrior_sonic_swing_s.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,4}*24,{24,24}}});
	}
	for (int i=0;i<4;i++){
		pl_warrior_swing_n.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,1}*24,{24,24}}});
		pl_warrior_sonic_swing_n.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,5}*24,{24,24}}});
	}
	for (int i=0;i<4;i++){
		pl_warrior_swing_w.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,2}*24,{24,24}}});
		pl_warrior_sonic_swing_w.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,6}*24,{24,24}}});
	}
	for (int i=0;i<4;i++){
		pl_warrior_swing_e.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,3}*24,{24,24}}});
		pl_warrior_sonic_swing_e.AddFrame({&GFX["nico-warrior.png"],{vi2d{4+i,7}*24,{24,24}}});
	}
	ANIMATION_DATA["WARRIOR_SWINGSWORD_N"]=pl_warrior_swing_n;
	ANIMATION_DATA["WARRIOR_SWINGSWORD_E"]=pl_warrior_swing_e;
	ANIMATION_DATA["WARRIOR_SWINGSWORD_S"]=pl_warrior_swing_s;
	ANIMATION_DATA["WARRIOR_SWINGSWORD_W"]=pl_warrior_swing_w;
	ANIMATION_DATA["WARRIOR_SWINGSONICSWORD_N"]=pl_warrior_sonic_swing_n;
	ANIMATION_DATA["WARRIOR_SWINGSONICSWORD_E"]=pl_warrior_sonic_swing_e;
	ANIMATION_DATA["WARRIOR_SWINGSONICSWORD_S"]=pl_warrior_sonic_swing_s;
	ANIMATION_DATA["WARRIOR_SWINGSONICSWORD_W"]=pl_warrior_sonic_swing_w;

	//Ranger animations
	SetupClassWalkIdleAnimations(GFX["nico-ranger.png"],"RANGER");
	Animate2D::FrameSequence pl_ranger_shoot_s,pl_ranger_shoot_n,pl_ranger_shoot_e,pl_ranger_shoot_w;
	for(int i=0;i<3;i++){
		pl_ranger_shoot_s.AddFrame({&GFX["nico-ranger.png"],{vi2d{3+i,0}*24,{24,24}}});
		pl_ranger_shoot_n.AddFrame({&GFX["nico-ranger.png"],{vi2d{3+i,1}*24,{24,24}}});
		pl_ranger_shoot_e.AddFrame({&GFX["nico-ranger.png"],{vi2d{3+i,3}*24,{24,24}}});
		pl_ranger_shoot_w.AddFrame({&GFX["nico-ranger.png"],{vi2d{3+i,2}*24,{24,24}}});
	}
	ANIMATION_DATA["RANGER_SHOOT_S"]=pl_ranger_shoot_s;
	ANIMATION_DATA["RANGER_SHOOT_N"]=pl_ranger_shoot_n;
	ANIMATION_DATA["RANGER_SHOOT_E"]=pl_ranger_shoot_e;
	ANIMATION_DATA["RANGER_SHOOT_W"]=pl_ranger_shoot_w;

	//Wizard animations
	SetupClassWalkIdleAnimations(GFX["nico-wizard.png"],"WIZARD");
	Animate2D::FrameSequence pl_wizard_idle_attack_s;
	pl_wizard_idle_attack_s.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,0}*24,{24,24}}});
	ANIMATION_DATA["WIZARD_IDLE_ATTACK_S"]=pl_wizard_idle_attack_s;
	Animate2D::FrameSequence pl_wizard_idle_attack_e;
	pl_wizard_idle_attack_e.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,3}*24,{24,24}}});
	ANIMATION_DATA["WIZARD_IDLE_ATTACK_E"]=pl_wizard_idle_attack_e;
	Animate2D::FrameSequence pl_wizard_idle_attack_w;
	pl_wizard_idle_attack_w.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,2}*24,{24,24}}});
	ANIMATION_DATA["WIZARD_IDLE_ATTACK_W"]=pl_wizard_idle_attack_w;
	Animate2D::FrameSequence pl_wizard_idle_attack_n;
	pl_wizard_idle_attack_n.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,1}*24,{24,24}}});
	ANIMATION_DATA["WIZARD_IDLE_ATTACK_N"]=pl_wizard_idle_attack_n;
	Animate2D::FrameSequence pl_wizard_attack_s(0.2f);
	for(int i=0;i<3;i++){
		pl_wizard_attack_s.AddFrame({&GFX["nico-wizard.png"],{vi2d{4+i,0}*24,{24,24}}});
		if(i==1){
			pl_wizard_attack_s.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,0}*24,{24,24}}});
		}
	}
	ANIMATION_DATA["WIZARD_ATTACK_S"]=pl_wizard_attack_s;
	Animate2D::FrameSequence pl_wizard_attack_e(0.2f);
	for(int i=0;i<3;i++){
		pl_wizard_attack_e.AddFrame({&GFX["nico-wizard.png"],{vi2d{4+i,3}*24,{24,24}}});
		if(i==1){
			pl_wizard_attack_e.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,3}*24,{24,24}}});
		}
	}
	ANIMATION_DATA["WIZARD_ATTACK_E"]=pl_wizard_attack_e;
	Animate2D::FrameSequence pl_wizard_attack_w(0.2f);
	for(int i=0;i<3;i++){
		pl_wizard_attack_w.AddFrame({&GFX["nico-wizard.png"],{vi2d{4+i,2}*24,{24,24}}});
		if(i==1){
			pl_wizard_attack_w.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,2}*24,{24,24}}});
		}
	}
	ANIMATION_DATA["WIZARD_ATTACK_W"]=pl_wizard_attack_w;
	Animate2D::FrameSequence pl_wizard_attack_n(0.2f);
	for(int i=0;i<3;i++){
		pl_wizard_attack_n.AddFrame({&GFX["nico-wizard.png"],{vi2d{4+i,1}*24,{24,24}}});
		if(i==1){
			pl_wizard_attack_n.AddFrame({&GFX["nico-wizard.png"],{vi2d{4,1}*24,{24,24}}});
		}
	}
	ANIMATION_DATA["WIZARD_ATTACK_N"]=pl_wizard_attack_n;
	Animate2D::FrameSequence pl_wizard_cast_s(0.1f);
	for(int i=0;i<2;i++){
		pl_wizard_cast_s.AddFrame({&GFX["nico-wizard.png"],{vi2d{7+i,0}*24,{24,24}}});
	}
	ANIMATION_DATA["WIZARD_CAST_S"]=pl_wizard_cast_s;
	Animate2D::FrameSequence pl_wizard_cast_e(0.1f);
	for(int i=0;i<2;i++){
		pl_wizard_cast_e.AddFrame({&GFX["nico-wizard.png"],{vi2d{7+i,3}*24,{24,24}}});
	}
	ANIMATION_DATA["WIZARD_CAST_E"]=pl_wizard_cast_e;
	Animate2D::FrameSequence pl_wizard_cast_n(0.1f);
	for(int i=0;i<2;i++){
		pl_wizard_cast_n.AddFrame({&GFX["nico-wizard.png"],{vi2d{7+i,1}*24,{24,24}}});
	}
	ANIMATION_DATA["WIZARD_CAST_N"]=pl_wizard_cast_n;
	Animate2D::FrameSequence pl_wizard_cast_w(0.1f);
	for(int i=0;i<2;i++){
		pl_wizard_cast_w.AddFrame({&GFX["nico-wizard.png"],{vi2d{7+i,2}*24,{24,24}}});
	}
	ANIMATION_DATA["WIZARD_CAST_W"]=pl_wizard_cast_w;

	CreateHorizontalAnimationSequence("ground-slam-attack-back.png",5,{64,64},{0.02f,Animate2D::Style::OneShot});
	CreateHorizontalAnimationSequence("ground-slam-attack-front.png",5,{64,64},{0.02f,Animate2D::Style::OneShot});
	CreateHorizontalAnimationSequence("battlecry_effect.png",5,{84,84},{0.02f,Animate2D::Style::OneShot});
	CreateHorizontalAnimationSequence("sonicslash.png",4,{60,60},{0.04f,Animate2D::Style::OneShot});

	CreateStillAnimation("energy_bolt.png",{24,24});

	CreateHorizontalAnimationSequence("energy_particle.png",3,{3,3});

	CreateHorizontalAnimationSequence("splash_effect.png",5,{24,24},{0.05f});
	
	CreateStillAnimation("circle.png",{3,3});

	CreateHorizontalAnimationSequence("lightning_bolt.png",5,{24,24},{0.03f,Animate2D::Style::PingPong});

	CreateStillAnimation("lightning_bolt_part1.png",{5,5});
	CreateStillAnimation("lightning_bolt_part2.png",{5,5});
	CreateStillAnimation("lightning_bolt_part3.png",{5,5});
	CreateStillAnimation("lightning_bolt_part4.png",{5,5});

	CreateStillAnimation("chain_lightning.png",{1,9});

	CreateHorizontalAnimationSequence("lightning_splash_effect.png",5,{24,24});

	CreateHorizontalAnimationSequence("monsters/Slime King - Cast.png",10,{24,24},{0.04f});

	CreateStillAnimation("meteor.png",{192,192});

	for(int i=0;i<5;i++){
		Animate2D::FrameSequence firering;
		firering.AddFrame({&GFX["fire_ring"+std::to_string(i)+".png"],{{0,0},{24,24}}});
		ANIMATION_DATA["fire_ring"+std::to_string(i)+".png"]=firering;
	}
	CreateStillAnimation("arrow.png",{24,24});
	CreateStillAnimation("charged_shot_arrow.png",{48,48});
	CreateStillAnimation("laser.png",{5,1});
	CreateStillAnimation("range_indicator.png",{24,24});

	for(auto&dat:GFX){
		std::string imgFile=dat.first;
		if(!ANIMATION_DATA.count(imgFile)){
			std::cout<<"WARNING! Animation data for "<<imgFile<<" not found! Auto-generating..."<<std::endl;
			CreateStillAnimation(imgFile,GFX[imgFile].Sprite()->Size());
		}
	}
}

void sig::Animation::SetupPlayerAnimations(){
	int counter=0;
	while("Player"_A.HasProperty("PLAYER_ANIMATION["+std::to_string(counter)+"]")){
		game->GetPlayer()->AddAnimation(DATA["Player"]["PLAYER_ANIMATION["+std::to_string(counter)+"]"].GetString()+"_N");
		game->GetPlayer()->AddAnimation(DATA["Player"]["PLAYER_ANIMATION["+std::to_string(counter)+"]"].GetString()+"_E");
		game->GetPlayer()->AddAnimation(DATA["Player"]["PLAYER_ANIMATION["+std::to_string(counter)+"]"].GetString()+"_S");
		game->GetPlayer()->AddAnimation(DATA["Player"]["PLAYER_ANIMATION["+std::to_string(counter)+"]"].GetString()+"_W");
		counter++;
	}

	ANIMATION_DATA.SetInitialized();
}