#include "Class.h"
#include "olcPixelGameEngine.h"
#include "DEFINES.h"
#include "Player.h"
#include "Effect.h"
#include "Crawler.h"
#include "BulletTypes.h"

INCLUDE_MONSTER_LIST
INCLUDE_BULLET_LIST
INCLUDE_game

std::string Wizard::name="Wizard";
Class Wizard::cl=WIZARD;
Ability Wizard::rightClickAbility={"Teleport",8,5,VERY_DARK_BLUE,DARK_BLUE};
Ability Wizard::ability1={"Firebolt",6,30};
Ability Wizard::ability2={"Lightning Bolt",6,25};
Ability Wizard::ability3={"Meteor",40,75,VERY_DARK_RED,VERY_DARK_RED,{1.5,900,400}};
Ability Wizard::ability4={"???",0,0};
AnimationState Wizard::idle_n=WIZARD_IDLE_N;
AnimationState Wizard::idle_e=WIZARD_IDLE_E;
AnimationState Wizard::idle_s=WIZARD_IDLE_S;
AnimationState Wizard::idle_w=WIZARD_IDLE_W;
AnimationState Wizard::walk_n=WIZARD_WALK_N;
AnimationState Wizard::walk_e=WIZARD_WALK_E;
AnimationState Wizard::walk_s=WIZARD_WALK_S;
AnimationState Wizard::walk_w=WIZARD_WALK_W;

SETUP_CLASS(Wizard)

void Wizard::OnUpdate(float fElapsedTime){
    if(attack_cooldown_timer>0){
		idle_n=AnimationState::WIZARD_IDLE_ATTACK_N;
		idle_e=AnimationState::WIZARD_IDLE_ATTACK_E;
		idle_s=AnimationState::WIZARD_IDLE_ATTACK_S;
		idle_w=AnimationState::WIZARD_IDLE_ATTACK_W;
		walk_n=AnimationState::WIZARD_ATTACK_N;
		walk_e=AnimationState::WIZARD_ATTACK_E;
		walk_s=AnimationState::WIZARD_ATTACK_S;
		walk_w=AnimationState::WIZARD_ATTACK_W;
	} else {
		idle_n=AnimationState::WIZARD_IDLE_N;
		idle_e=AnimationState::WIZARD_IDLE_E;
		idle_s=AnimationState::WIZARD_IDLE_S;
		idle_w=AnimationState::WIZARD_IDLE_W;
		walk_n=AnimationState::WIZARD_WALK_N;
		walk_e=AnimationState::WIZARD_WALK_E;
		walk_s=AnimationState::WIZARD_WALK_S;
		walk_w=AnimationState::WIZARD_WALK_W;
	}
	if(GetState()==State::CASTING){
		switch(GetFacingDirection()){
			case UP:{
				UpdateAnimation(AnimationState::WIZARD_CAST_N,WIZARD|WITCH);
			}break;
			case DOWN:{
				UpdateAnimation(AnimationState::WIZARD_CAST_S,WIZARD|WITCH);
			}break;
			case LEFT:{
				UpdateAnimation(AnimationState::WIZARD_CAST_W,WIZARD|WITCH);
			}break;
			case RIGHT:{
				UpdateAnimation(AnimationState::WIZARD_CAST_E,WIZARD|WITCH);
			}break;
		}
	}
}

bool Wizard::AutoAttack(){
	attack_cooldown_timer=MAGIC_ATTACK_COOLDOWN;
	float angleToCursor=atan2(game->GetWorldMousePos().y-GetPos().y,game->GetWorldMousePos().x-GetPos().x);
	BULLET_LIST.push_back(std::make_unique<EnergyBolt>(EnergyBolt(GetPos(),{cos(angleToCursor)*200,sin(angleToCursor)*200},12,GetAttack(),upperLevel,true,WHITE)));
	return true;
}
void Wizard::InitializeClassAbilities(){
    #pragma region Wizard Right-click Ability (Teleport)
        Wizard::rightClickAbility.action=
            [&](){
                float pointMouseDirection=atan2(game->GetWorldMousePos().y-GetPos().y,game->GetWorldMousePos().x-GetPos().x);
                vf2d pointTowardsMouse={cos(pointMouseDirection),sin(pointMouseDirection)};
                float dist=std::clamp(geom2d::line<float>{GetPos(),game->GetWorldMousePos()}.length(),0.f,6.5f*24);
                if(dist<12)return false;
                vf2d teleportPoint=GetPos()+pointTowardsMouse*dist;
                while(dist>0&&game->HasTileCollision(game->GetCurrentLevel(),teleportPoint)&&CanPathfindTo(GetPos(),teleportPoint)){
                    dist-=24;
                    teleportPoint=GetPos()+pointTowardsMouse*dist;
                }
                if(dist>0&&CanPathfindTo(GetPos(),teleportPoint)){
                    SetState(State::TELEPORT);
                    teleportAnimationTimer=0.35;
                    teleportTarget=teleportPoint;
                    teleportStartPosition=GetPos();
                    iframe_time=0.35;
                    for(int i=0;i<16;i++){
                        game->AddEffect(std::make_unique<Effect>(GetPos()+vf2d{(rand()%160-80)/10.f,(rand()%160-80)/10.f},float(rand()%300)/1000,AnimationState::DOT_PARTICLE,upperLevel,0.3,0.2,vf2d{float(rand()%1000-500)/100,float(rand()%1000-500)/100},BLACK));
                    }
                    return true;
                } else {
                    notificationDisplay={"Cannot Teleport to that location!",0.5};
                    return false;
                }
            };
    #pragma endregion
    #pragma region Wizard Ability 1 (Fire Bolt)
        Wizard::ability1.action=
            [&](){
                float angleToCursor=atan2(game->GetWorldMousePos().y-GetPos().y,game->GetWorldMousePos().x-GetPos().x);
                BULLET_LIST.push_back(std::make_unique<FireBolt>(FireBolt(GetPos(),{cos(angleToCursor)*275,sin(angleToCursor)*275},12,GetAttack(),upperLevel,true,{240,120,60})));
                return true;
            };
    #pragma endregion
    #pragma region Wizard Ability 2 (Lightning Bolt)
        Wizard::ability2.action=
            [&](){
                float angleToCursor=atan2(game->GetWorldMousePos().y-GetPos().y,game->GetWorldMousePos().x-GetPos().x);
                BULLET_LIST.push_back(std::make_unique<LightningBolt>(LightningBolt(GetPos(),{cos(angleToCursor)*230,sin(angleToCursor)*230},12,GetAttack()*4,upperLevel,true,WHITE)));
                return true;
            };
    #pragma endregion
    #pragma region Wizard Ability 3 (Meteor)
        Wizard::ability3.action=
            [&](){
                CastSpell(Wizard::ability3);
                game->AddEffect(std::make_unique<Meteor>(GetPos(),3,AnimationState::METEOR,OnUpperLevel(),vf2d{1,1},2));
                return true;
            };
    #pragma endregion
}