<?php

require_once __DIR__ . '/../../src/Events/MonthEndEvent.php';

class BigStormEvent extends MonthEndEvent
{
    public function getMinAge()
    {
        return 0;
    }

    public function getMinParticipants()
    {
        return 0;
    }

    public function getMaxParticipants()
    {
        return 0;
    }

    public function getCost()
    {
        return 55;
    }

    public function isAvailable()
    {
        if (!empty($this->tribe->dataStore->draught)) {
            return false;
        }
        return true;
    }

    public function execute()
    {
        if (!isset($this->tribe->dataStore)) {
            $this->tribe->dataStore = new stdClass();
        }

        $foodLost = 0;
        $currentFood = $this->tribe->getFoodCount();
        if ($currentFood > 0 && rand(1, 100) <= 70) {
            $percent = rand(5, 20);
            $foodLost = (int) floor($currentFood * ($percent / 100));
            if ($foodLost > 0) {
                $this->tribe->removeFood($foodLost);
            }
        }

        $lightningVictim = null;
        if (rand(1, 100) <= 5) {
            $alive = array_values($this->tribe->getAliveKobolds());
            if (!empty($alive)) {
                $lightningVictim = $alive[array_rand($alive)];
                $lightningVictim->die();
                $this->reportDeath($lightningVictim);
            }
        }

        $intros = [
            "A violent storm rolled over the tribe, tearing at tents and shaking the ground.",
            "Dark clouds gathered quickly, and a brutal storm swept across the camp.",
            "Wind and rain arrived in force, and the tribe huddled through a fierce storm."
        ];

        $details = [];
        if ($foodLost > 0) {
            $details[] = "Some stores were ruined in the downpour, costing the tribe {$this->tribe->foodTimeStr($foodLost)} of food.";
        } else {
            $details[] = "The stores held up better than expected, and no food was lost.";
        }

        if ($lightningVictim) {
            $details[] = "Tragedy struck when lightning found {$lightningVictim->getName()}, and the tribe mourned the loss.";
        } else {
            $details[] = "No one was lost to the storm, though the camp will remember the night.";
        }

        $this->story = $intros[array_rand($intros)] . ' ' . $details[0] . ' ' . $details[1];
    }
}
