using SlugBase.Features;
using static SlugBase.Features.FeatureTypes;
using SlugBase;

namespace JilloSlug.Features;

internal static class MarkFeature {
    public static readonly PlayerFeature<bool> Mark = PlayerBool("jillo/has_mark");

    public static void AddHooks() {
        On.SaveState.ctor += SaveState_ctor;
        On.SSOracleBehavior.NewAction += SSOracleBehavior_NewAction;
    }

    private static void SaveState_ctor(On.SaveState.orig_ctor orig, SaveState self, SlugcatStats.Name slugcat, PlayerProgression progression) {
        orig(self, slugcat, progression);

        if (SlugBaseCharacter.TryGet(slugcat, out var chara) && Mark.TryGet(chara, out bool hasMark) && hasMark) {
            self.deathPersistentSaveData.theMark = true;
        }
    }

    private static void SSOracleBehavior_NewAction(On.SSOracleBehavior.orig_NewAction orig, SSOracleBehavior self, SSOracleBehavior.Action nextAction) {
        if (nextAction == SSOracleBehavior.Action.General_GiveMark && Mark.TryGet(self.player, out var hasMark) && hasMark) {
            orig(self, SSOracleBehavior.Action.General_MarkTalk);
            return;
        }
        orig(self, nextAction);
    }
}
