using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.Events
{
    public class RoadMap {
        private delegate void RoadMapEvent(GameCore core);
        private readonly List<RoadMapEvent> _events = new List<RoadMapEvent>();
        private int _curEvent = -1;

        public RoadMap() {
            Build();
        }

        private void Build() {
            SpecificEvent("Intro");

            StartAct(Act.Act_1);
            RandomEvents(3);
            SendTag("cityApproaching");
            SpecificEvent("City1");
            SpecificEvent("OldWoman1");
            RandomEvents(3);
            SpecificEvent("Healer1");
            RandomEvents(1);
            SpecificEvent("Boss1");

            StartAct(Act.Act_2);
            RandomEvents(4);
            SpecificEvent("OldWoman2");
            SendTag("cityApproaching");
            OneOfTheseEvents(new[]{"City2", "City3"});
            RandomEvents(3);
            SpecificEvent("GraveDigger");
            OneOfTheseEvents(new[] { "NecromancerWithDead", "NecromancerWithoutDead" });

            StartAct(Act.Act_3);
            RandomEvents(1);
            SpecificEvent("Cultists1");
            RandomEvents(1);
            SendTag("cityApproaching");
            SpecificEvent("City4");
            RandomEvents(1);
            SpecificEvent("Cultists2");
            RandomEvents(1);
            SpecificEvent("Cultists3");
            RandomEvents(1);
            SpecificEvent("Dracula");
            SpecificEvent("Necromancer2WithoutDead");
            SpecificEvent("Necromancer2WithDead");

            StartAct(Act.Epilogue);
            SpecificEvent("Finale");
            SpecificEvent("TrueFinale");
        }

        private void SendTag(string tag) {
            _events.Add(c => c.OnTagSent(tag));
        }

        private void StartAct(Act act) {
            _events.Add(c => c.StartAct(act));
        }

        private void RandomEvents (int n) {
            for (int i = 0; i < n; i++) {
                _events.Add(c => c.StartRandomEvent());
            }
        }

        private void SpecificEvent (string name) {
            _events.Add(c => c.StartSpecificEvent(name));
        }

        private void OneOfTheseEvents (string[] eventNames) {
            _events.Add(c => c.StartOneOfEvents(eventNames));
        }

        public void Advance(GameCore core) {
            _curEvent++;
            if (_curEvent < _events.Count) {
                _events[_curEvent](core);
            } else {
                core.GameComplete();
            }
        }
    }
}
