using System.Linq;
using Assets.Scripts.Player;
using Node = Assets.Scripts.Events.GameEvent.Node;
using Option = Assets.Scripts.Events.GameEvent.Option;

namespace Assets.Scripts.Events
{
    public class Act2Events : EventBuilder
    {
        public Act2Events() : base(Act.Act_2) {}

        protected override void AddEvents() {
            AddSpecificEvent("OldWoman2",
                new Node("And old lady approaches and demands you stop the wagon. She seems fragile, trusting the weight of her body with only a small wooden staff. You ask her who she is, to which she replies, very serious: \"I am you from the future. There is no time to explain\". An awkward silence follows. The old lady bursts into laugh: \"Just kidding! Time-bending is not my kind of gig. And you kids don't even look like you have a distant future, hah! Anyway, I'm Rosa, the town's witch.\"",
                    new Option("Continue") {
                        NextNode = new Node("A witch!? You prepare to battle, but she quickly jumps onto the wagon and stops you with a loud, white beam that makes your whole body feel like cold stone. The witch explains: \"You really don't want to fight me, I can make you disappear with a snap of a dark magic loaded finger.\"", 
                            new Option("Continue") {
                                NextNode = new Node("With another beam, she gives you your movements back. \"I am here to help, silly! My sister told me how nice you were to her back at the dark forest.\" She opens a slightly bizarre, but still very kind smile. Rosa the witch has joined your party, I guess!") {
                                    PostEffect = c => c.PlayerStats.AddHero(MobFactory.MakeRosa())
                                }
                            }
                        )
                    }
                )
            ).TriggerCondition = stats => stats.GetFlag("OldWoman1:Helped");

            AddEvent(
                new Node("As you venture further within the village, you see a small commotion near a fountain. It's some adorable kids! And they're playing with the water. As you come closer, you notice they're something in the water. Or... someone...? Oh god. The kids are trying to drown one of their friends.",
                    new Option("Stop them!") {
                        NextNode = new Node("You pull each of the kids by their clothes, finally giving some space to the one who was being, ehm, \bullied\" doesn't even begin to define it. \"What did you do that for?\", the kid doesn't seem very grateful. She actually looks angry.",
                            new Option("Continue") {
                                NextNode = new Node("You look around and realize people are disappointed at you. When you take another look at the girl, you notice she has... gills? She's definitely from one of those half-fish, half-human tribes. \"They weren't drowning me, I was showing off my amazing skills! Now beat it, will you?\"",
                                    new Option("Continue") {
                                        NextNode = new Node("As she moves away, she seems to grasp a strange charm that was hanging from her neck. It gives you the chills, though you're sure that you're just being paranoid. [-10% accuracy to all]") {
                                            Effect = c => c.PlayerStats.ForAllHeroes(h => h.AddBuff(new Buff{ Attribute = MobAttribute.Accuracy, Value = -0.1f }))
                                        }
                                    }
                                )
                            }
                        )
                    },
                    new Option("Nope!") {
                        NextNode = new Node("You know how kids are. Their parents are probably around and will intervene if things get out of control. Or not. You don't really care. Oh hey, here's some gold coins on the ground! [+8 gold]") {
                            Effect = c => c.PlayerStats.Gold += 8
                        }
                    }
                )
            );

            AddRecurringEvent(
                new Node("You see some hooded people drawing bizarre symbols on a house's wall, using charcoal. It looks extremely suspicious, but hey, the people from this town are indeed a bit strange.",
                    new Option("Check what they're up to.") {
                        NextNode = new Node("They turn to you with an empty look on their faces. Their skin is white like ivory, their clothes have a very off-putting shade of gray. And they bear the sign of a cross, but... there's something wrong with it. \"We live for our lord and our lord lives through us\", they mumble. Who are they talking about? And why are they pointing knives at you?") {
                            PostEffect = c => c.StartBattle(MobFactory.MakePriest(), MobFactory.MakePriest())
                        }
                    },
                    new Option("Pretend you never saw them.") {
                        NextNode = new Node("The art they're making is actually kind of nice if you keep an open mind. It's especially nice how the art seems to whisper into your head. [-1 Sanity to all]") {
                            Effect = c => c.PlayerStats.ForAllHeroes(h => h.LoseSanity(1))
                        }
                    }
                )
            );

            AddRecurringEvent(
                new Node("A large flock of homeless people surrounds your wagon. \"Please! Give us some help! Just any spare change... We need food\", they say.",
                    new Option("Generously tip them [10 gold]") {
                         Condition = p => p.Gold >= 10,
                         NextNode = new Node("You give them a generous donation, which fills their eyes with happiness. They then leave without giving you any sort of supernatural boost. What did you expect? [+1 sanity to all]") {
                             Effect = c => {
                                 c.PlayerStats.Gold -= 10;
                                 c.PlayerStats.ForAllHeroes(h => h.HealSanity(1));
                             }
                         }
                    },
                    new Option("Tip them [5 gold]") {
                        Condition = p => p.Gold >= 5,
                        NextNode = new Node("You give them a modest donation, for which they thank you, before leaving to beg to the next adventurer. [+1 sanity to all]") {
                            Effect = c => {
                                c.PlayerStats.Gold -= 5;
                                c.PlayerStats.ForAllHeroes(h => h.HealSanity(1));
                            }
                        }
                    },
                    new Option("Tell them to bugger off") {
                        NextNode = new Node("They don't take the dismissal very well, and start surrounding and punching the wagon. After some attrition, you manage to send them off, amidst a wave of insults coming from them, but not without taking some bruises yourself. [-1 HP to all]") {
                            Effect = c => c.PlayerStats.ForAllHeroes(h => h.TakeDamage(1))
                        }
                    }
                )
            );

            AddEvent(
                new Node("A very weird man throws himself in front of the wagon and you are forced to stop. \"Want to play a gaaaame?\", he asks in an annoying, high-pitched voice.", 
                    new Option("Uh... sure.") {
                        NextNode = new Node("What harm could a game do? The man is very happy that you said yes. He starts explaining the rules, warning you that it's still just a prototype. He asks you to choose one side of a coin:",
                            new Option("Heads") {
                                NextNode = new Node("He laughs and tosses the coin upwards, making it spin several times. You wait with patience. When the coin lands, it spins around itself for a while, finally resting with the Ribs side faced up, which is the the side between Heads and Tails. These coins are quite thicker than the normal ones.",
                                    new Option("Continue") {
                                        NextNode = new Node("The euphoric man starts jumping and laughing in a maniacal manner! \"You have won, YOU HAVE WON!\", he screams, trying to grab you for a hug as you discretely take a few steps back. \"My game works! Did you have fun?\", he asks. You nod without thinking what could be the consequences of not agreeing with him. \"Here's your prize!\" [+20 gold]") {
                                            Effect = c => c.PlayerStats.Gold += 20
                                        }
                                    }
                                )
                            },
                            new Option("Tails") {
                                NextNode = new Node("He laughs and tosses the coin upwards, making it spin several times. You wait with patience. When the coin lands, it spins around itself for a while, finally resting with the Ribs side faced up, which is the the side between Heads and Tails. These coins are quite thicker than the normal ones.",
                                    new Option("Continue") {
                                        NextNode = new Node("\"Oh...\", he sighs. \"I still have to adjust the rules a little bit. I'm so sorry that you didn't have fun with my game.\" He slowly walks away, dragging his feet.")
                                    }
                                )
                            },
                            new Option("Ribs") {
                                NextNode = new Node("He laughs and tosses the coin upwards, making it spin several times. You wait with patience. When the coin lands, it spins around itself for a while, finally resting with the Ribs side faced up, which is the the side between Heads and Tails. These coins are quite thicker than the normal ones.",
                                    new Option("Continue") {
                                        NextNode = new Node("\"Oh...\", he sighs. \"I still have to adjust the rules a little bit. I'm so sorry that you didn't have fun with my game.\" He slowly walks away, dragging his feet.")
                                    }
                                )
                            }
                        )
                    },
                    new Option("No, thanks!") {
                        NextNode = new Node("More like \"lame\", amirite. You move forward, ignoring him. He looks at you with a very disappointed face, that soon becomes a very suspicious expression. You ignore that as well. After a while, you notice some of your money is gone. [-5 gold]") {
                            Effect = c => c.PlayerStats.Gold -= 5
                        }
                    }
                )
            );

            AddSpecificEvent("GraveDigger", 
                new Node("\"Bring out yer dead\", says the handsome, muscular young man dressed in grey scraps. He's holding a silver shovel over his shoulder. \"I'll give a proper funeral to any of your dead companions, if you'd like\"... For a small fee, of course.\", he says, winking.",
                    new Option("Accept [20 gold]") {
                        Condition = p => p.Gold >= 20,
                        NextNode = new Node("\"Blimey!\", you exclaim, \"I hadn't even noticed that we were carrying their dead bodies with us all along. Please, do give them a resting place. The young man is happy to oblige. [+1 sanity to all]") {
                            Effect = c => {
                                c.PlayerStats.RemoveDead();
                                c.PlayerStats.Gold -= 20;
                            }
                        }
                    },
                    new Option("Refuse") {
                        NextNode = new Node("Deciding that 20 gold is an outrageous amount to dig a hole in the ground, and that the bodies aren't really bothering you (you hadn't even realized you still had them with you), you decide to decline and go onward. What's the worst that could happen, a Necromancer?")
                    }
                )
            ).TriggerCondition = p => p.DeadCharacters.Length > 0;

            AddSpecificEvent("NecromancerWithDead", 
                new Node("Well damn. The good news is that you've successfully crossed the village, and the entrance to the castle lies just ahead. The bad news is that a tall, slender woman wearing black robes stands between you and it. The dead rise restless around her. A Necromancer.",
                    new Option("Continue") {
                        NextNode = new Node("She lifts her finger towards your wagon, and the voices of those who had deceased can be heard again. As they stand again, you think \"20 gold, eh\", as you are surrounded by the dead, as the necromancer flees.") {
                            PostEffect = c => {
                                var enemies = c.PlayerStats.DeadCharacters.Concat(new [] { MobFactory.MakeSkeleton(), MobFactory.MakeSkeleton() }).ToArray();
                                c.PlayerStats.RemoveDead();
                                c.StartBattle(enemies);
                            }
                        }
                    }
                )
            ).TriggerCondition = p => p.DeadCharacters.Length > 0;

            AddSpecificEvent("NecromancerWithoutDead",
                new Node("Well damn. The good news is that you've successfully crossed the village, and the entrance to the castle lies just ahead. The bad news is that a tall, slender woman wearing black robes stands between you and it. The dead rise restless around her. A Necromancer.",
                    new Option("Continue") {
                        NextNode = new Node("She raises her finger towards your wagon, and then... Nothing. She seems disappointed, and commands her minions to attack you, and she flees from the battle.") {
                            PostEffect = c => c.StartBattle(MobFactory.MakeSkeleton(), MobFactory.MakeSkeleton())
                        }
                    }
                )
            ).TriggerCondition = p => p.DeadCharacters.Length == 0;

            AddSpecificEvent("City2",
                new Node("You stop in front of a big inn, wondering if you should sleep there for the night or not. It seems expensive, but you're so tired that maybe it would be worth it. It also has several guests from all over the continent, and one of them looks at you with admiration in his eyes.",
                    new Option("Rest [25 gold]") {
                        Condition = p => p.Gold >= 25,
                        NextNode = new Node("You have dinner with the other guests of the inn. The one who was looking at you before is a huge fan of your latest accomplishments, and gives you a tiny amulet to bring you luck. After that, you all go to sleep. Wow, this is what I call a comfy bed! [+5 health/+3 sanity to all, +1 speed to one character] [-25 gold]") {
                            Effect = c => {
                                c.PlayerStats.Gold -= 25;
                                c.PlayerStats.ForAllHeroes(h => {
                                    h.Heal(5);
                                    h.HealSanity(3);
                                });
                                c.PlayerStats.RandomHero().AddBuff(new Buff{ Attribute = MobAttribute.Cooldown, Value = -1 });
                            }
                        }
                    },
                    new Option("Stick with the old pillows inside the wagon") {
                        NextNode = new Node("They seem like a bunch of snobs anyway. [+1 health/-1 sanity to all]") {
                            Effect = c => c.PlayerStats.ForAllHeroes(p => {
                                p.Heal(1);
                                p.LoseSanity(1);
                            })
                        }
                    }
                )
            );

            AddSpecificEvent("City3",
                new Node("You come across a big lady with a smile on her face. \"Want to rest in my inn?\", she asks. You are so tired, you certainly consider it. But when you take a second look at the place, you start suspecting it won't be much better than the old pillows inside your wagon. The lady seems so nice, though.",
                    new Option("Rest [18 gold]") {
                        Condition = p => p.Gold >= 18,
                        NextNode = new Node("The sweet lady prepares you some soup and talks with you for a while. She thanks you for being after the big evil that has been tormenting her town and friends, and gives you a golden magnifier that has been in her family for decades. [+2 health/sanity to all, +1 range to one character] [-18 gold]") {
                            Effect = c => {
                                c.PlayerStats.Gold -= 18;
                                c.PlayerStats.ForAllHeroes(h => {
                                    h.Heal(2);
                                    h.HealSanity(2);
                                });
                                c.PlayerStats.RandomHero().AddBuff(new Buff { Attribute = MobAttribute.Range, Value = 1 });
                            }
                        }
                    },
                    new Option("Nah, we're fine") {
                        NextNode = new Node("It's better to not take any chances with suspicious places. [+1 health/-1 sanity to all]") {
                            Effect = c => c.PlayerStats.ForAllHeroes(p => {
                                p.Heal(1);
                                p.LoseSanity(1);
                            })
                        }
                    }
                )
            );
        }
    }
}
