/**
 * @file octorok.c
 * @ingroup Enemies
 *
 * @brief Octorok enemy
 */

#include "enemy.h"
#include "functions.h"

void Octorok_Pause(Entity*);
bool32 Octorok_FacesPlayer(Entity*);
void Octorok_Turn(Entity*);

extern void (*const Octorok_Functions[6])(Entity*);
extern void (*const gOctorokActions[4])(Entity*);
extern void (*const gUnk_080CA158[6])(Entity*);

extern Entity* gUnk_020000B0;
extern const u8 gOctorokWalkDuration[4];
extern const u8 gOctorokSpitChanceModifier[2];
extern const u8 gOctorokNutOffset[8];
extern const s8 gUnk_080CA17E[2];

// Main
void Octorok(Entity* this) {
    EnemyFunctionHandler(this, Octorok_Functions);
    SetChildOffset(this, 0, 1, -16);
}

// Idle
void Octorok_OnTick(Entity* this) {
    gOctorokActions[this->action](this);
}

// Touch player
void Octorok_OnCollision(Entity* this) {
    if (this->confusedTime != 0) {
        Create0x68FX(this, FX_STARS);
    }
    EnemyFunctionHandlerAfterCollision(this, Octorok_Functions);
}

// Death
void Octorok_OnDeath(Entity* this) {
    if (this->type == 0) {
        GenericDeath(this);
    } else {
        CreateDeathFx(this, 241, 0);
    }
}

void Octorok_OnGrabbed(Entity* this) {
    if (this->subAction < 3 && !sub_0806F520(this)) {
        Octorok_Pause(this);
        InitializeAnimation(this, this->animationState);
    } else {
        gUnk_080CA158[this->subAction](this);
    }
}

void sub_0801EB68(Entity* this) {
    this->subAction = 1;
    this->field_0x1d = 60;
}

void sub_0801EB74(Entity* this) {
    sub_0806F4E8(this);
}

void sub_0801EB7C(Entity* this) {
    sub_0806F3E4(this);
}

void sub_0801EB84(Entity* this) {
    COLLISION_OFF(this);
    UpdateAnimationVariableFrames(this, 2);
}

void nullsub_3(Entity* this) {
    /* ... */
}

void sub_0801EB9C(Entity* this) {
    if (this->flags & ENT_COLLIDE) {
        COLLISION_ON(this);
        this->field_0x3a &= 0xfb;
    } else {
        this->health = 0;
    }
}

// Init
void Octorok_Initialize(Entity* this) {
    sub_0804A720(this);
    if (this->type == 2) {
        this->animationState = this->type;
    } else {
        this->animationState = Random() & 3;
    }
    this->field_0x1c = 18;
    Octorok_Pause(this);
    InitializeAnimation(this, this->animationState);
}

void Octorok_Idle(Entity* this) {
    if (--this->actionDelay == 0) {
        this->action = 2;
        this->actionDelay = gOctorokWalkDuration[Random() & 3];
        Octorok_Turn(this);
    }
    GetNextFrame(this);
}

void Octorok_Move(Entity* this) {
    ProcessMovement0(this);
    GetNextFrame(this);
    if (--this->actionDelay == 0) {
        if (Octorok_FacesPlayer(this) && gOctorokSpitChanceModifier[this->type] <= (Random() & 3)) {
            this->action = 3;
            InitializeAnimation(this, this->animationState + 4);
        } else {
            Octorok_Pause(this);
        }
    }
}

void Octorok_ShootNut(Entity* this) {
    GetNextFrame(this);
    if (this->frame & 1) {
        Entity* ent = CreateProjectileWithParent(this, ROCK_PROJECTILE, 0);
        if (ent) {
            const s8* off;
            ent->direction = this->direction;
            off = &gOctorokNutOffset[this->direction / 4];
            ent->x.HALF.HI += off[0];
            ent->y.HALF.HI += off[1];
            ent->z.HALF.HI = -3;
            this->frame &= 0xfe;
            EnqueueSFX(SFX_18D);
        }
    }

    if (this->frame & 0x80)
        Octorok_Pause(this);
}

void Octorok_Pause(Entity* this) {
    this->action = 1;
    this->actionDelay = (Random() & 0x38) + 0x18;
}

void Octorok_Turn(Entity* this) {
    if (this->type != 2) {
        if (sub_08049FA0(this)) {
            if (this->type == 1 && (Random() & 3) == 0 && sub_08049FDC(this, 1)) {
                this->direction = DirectionRoundUp(GetFacingDirection(this, gUnk_020000B0));
            } else {
                this->direction = DirectionRound(Random());
            }
        } else if (Random() & 3) {
            this->direction = DirectionRound(sub_08049EE4(this) + gUnk_080CA17E[Random() & 1]);
        } else {
            this->direction = DirectionRound(Random());
        }
    } else {
        if (sub_08049FDC(this, 1) == 0) {
            if (sub_08049FA0(this)) {
                this->direction = DirectionRound(Random());
            } else if (Random() & 3) {
                this->direction = DirectionRound(sub_08049EE4(this) + gUnk_080CA17E[Random() & 1]);
            } else {
                this->direction = DirectionRoundUp(GetFacingDirection(this, gUnk_020000B0));
            }
        } else {
            this->direction = DirectionRoundUp(GetFacingDirection(this, gUnk_020000B0));
        }
    }

    this->animationState = this->direction >> 3;
    InitializeAnimation(this, this->animationState);
}

bool32 Octorok_FacesPlayer(Entity* this) {
    Entity* ent = sub_08049DF4(1);

    if (ent == NULL)
        return FALSE;

    if (DirectionRoundUp(GetFacingDirection(this, ent)) != this->direction)
        return FALSE;

    return TRUE;
}

// clang-format off
void (*const Octorok_Functions[])(Entity*) = {
    Octorok_OnTick,
    Octorok_OnCollision,
    GenericKnockback,
    Octorok_OnDeath,
    GenericConfused,
    Octorok_OnGrabbed,
};

void (*const gOctorokActions[])(Entity*) = {
    Octorok_Initialize,
    Octorok_Idle,
    Octorok_Move,
    Octorok_ShootNut,
};

void (*const gUnk_080CA158[])(Entity*) = {
    sub_0801EB68,
    sub_0801EB74,
    sub_0801EB7C,
    sub_0801EB84,
    nullsub_3,
    sub_0801EB9C,
};

const u8 gOctorokWalkDuration[] = {
    30, 60, 60, 90,
};

const u8 gOctorokSpitChanceModifier[] = {
    1, 0,
};

const u8 gOctorokNutOffset[] = {
     0, -3,
     4,  0,
     0,  2,
    -4,  0,
};

const s8 gUnk_080CA17E[] = {
    4, -4,
};
// clang-format on
