#include "npc.h"
#include "functions.h"
#include "item.h"

extern void sub_08068780(Entity*);

extern void (*const gUnk_08111530[])(Entity*);

extern SpriteLoadData gUnk_08111520;
extern u16 gUnk_0811153E[];
extern u16 gUnk_08111538[];

void Melari(Entity* this) {
    if ((this->flags & ENT_SCRIPTED) == 0) {
        gUnk_08111530[this->action](this);
        sub_0806ED78(this);
    } else {
        sub_08068780(this);
    }
}

void sub_08068708(Entity* this) {
    if (LoadExtraSpriteData(this, &gUnk_08111520) != 0) {
        this->action = 1;
        InitializeAnimation(this, 0);
        sub_08078778(this);
    }
}

void sub_08068730(Entity* this) {
    s32 animIndex;

    animIndex = GetAnimationStateInRectRadius(this, 0x20, 0x20);
    if (-1 < animIndex) {
        if (this->field_0xf == 0) {
            this->field_0xf = 0x10;
            if (this->animIndex != animIndex) {
                InitializeAnimation(this, animIndex);
            }
        } else {
            this->field_0xf--;
        }
    }
    if (this->interactType != 0) {
        this->interactType = 0;
        MessageFromTarget(this->actionDelay + 0x1200);
    }
}

void sub_08068780(Entity* this) {
    Entity* ent;

    switch (this->action) {
        case 0:
            if (LoadExtraSpriteData(this, &gUnk_08111520) == 0) {
                return;
            }
            this->action = 1;
            this->spriteSettings.draw = TRUE;
            this->animationState = this->type;
            SetDefaultPriority(this, PRIO_MESSAGE);
            sub_0807DD50(this);
            break;
        case 1:
            if (this->interactType == 2) {
                this->action = 2;
                this->interactType = 0;
                sub_0806F118(this);
            } else {
                ExecuteScriptForEntity(this, NULL);
                HandleEntity0x82Actions(this);
                GetNextFrame(this);
            }
            break;
        case 2:
            if (UpdateFuseInteraction(this)) {
                this->action = 1;
            }
    }

    if (this->frame & 0x40) {
        this->frame &= 0xbf;
        if (CheckOnScreen(this) == 0) {
            SoundReq(gUnk_0811153E[(s32)Random() % 3]);
        } else {
            EnqueueSFX(gUnk_08111538[(s32)Random() % 3]);
        }
        ent = CreateFx(this, FX_STARS2, 0x20);
        if (ent != NULL) {
            PositionRelative(this, ent, Q_16_16(24.0), Q_16_16(-10.0));
        }
    }
}

void sub_08068884(Entity* this) {
    this->field_0x68.HALF.LO = sub_0801E99C(this);
    sub_08078784(this, this->field_0x68.HALF.LO);
}

void Melari_Head(Entity* this) {
    u32 bVar1;

    bVar1 = this->frame;
    bVar1 &= -0xc1;
    SetExtraSpriteFrame(this, 1, this->frameIndex);
    if ((bVar1 & 0x20) != 0) {
        SetExtraSpriteFrame(this, 0, bVar1 & -0xe1);
        SetExtraSpriteFrame(this, 2, 0xff);
        SetSpriteSubEntryOffsetData1(this, 1, 0);
    } else {
        SetExtraSpriteFrame(this, 0, 0xff);
        SetExtraSpriteFrame(this, 2, bVar1 & -0xe1);
        SetSpriteSubEntryOffsetData2(this, 1, 2);
    }
    sub_0807000C(this);
}

void sub_08068910(Entity* this) {
    s32 iVar1;

    if (this->actionDelay != 0) {
        this->actionDelay--;
    } else {
        this->actionDelay = 0x10;
        if ((this->frame & 0x20) != 0) {
            iVar1 = GetFacingDirectionInRectRadius(this, 0x30, 0x30);
            if (iVar1 < 0) {
                iVar1 = 0x10;
            } else {
            }
            iVar1 = sub_0806F5A4(iVar1);
            InitializeAnimation(this, (this->animIndex & -0x4) + iVar1);
        }
    }
}

void sub_08068964(Entity* this) {
    sub_080A7C18(ITEM_QST_BROKEN_SWORD, 0, 3);
    SetInventoryValue(ITEM_QST_BROKEN_SWORD, 2);
}

void Melari_Fusion(Entity* this) {
    if (this->action == 0) {
        if (LoadExtraSpriteData(this, &gUnk_08111520)) {
            this->action++;
            this->spriteSettings.draw = TRUE;
            InitializeAnimation(this, 6);
        }
    } else {
        GetNextFrame(this);
    }
}

void nullsub_502(Entity* this) {
}
