<?php

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

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

    public function getMinParticipants()
    {
        return 0;
    }

    public function getMaxParticipants()
    {
        return 0;
    }

    public function getCost()
    {
        return 125;
    }

    public function isAvailable()
    {
        return $this->tribe->getCurrentSeason() === 'autumn';
    }

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

        $currentFood = $this->tribe->getFoodCount();
        $foodLost = 0;
        if ($currentFood > 0) {
            $percent = rand(30, 60);
            $foodLost = (int) floor($currentFood * ($percent / 100));
            if ($foodLost > 0) {
                $this->tribe->removeFood($foodLost);
            }
        }

        $harvestText = '';
        if (!empty($this->tribe->dataStore->wasTendedInSummer)) {
            $harvestText = "The flood ruined the ready harvest before it could be gathered.";
            $this->tribe->dataStore->wasTendedInSummer = false;
            $this->tribe->dataStore->wasSowedInSpring = false;
        } elseif (!empty($this->tribe->dataStore->wasSowedInSpring)) {
            $harvestText = "The flood washed out the planted fields before they could grow.";
            $this->tribe->dataStore->wasSowedInSpring = false;
        }

        $alive = array_values($this->tribe->getAliveKobolds());
        $drowned = [];
        if (!empty($alive)) {
            $percent = rand(10, 50);
            $count = (int) floor(count($alive) * ($percent / 100));
            if ($count > 0) {
                shuffle($alive);
                $drowned = array_slice($alive, 0, $count);
                foreach ($drowned as $kobold) {
                    $kobold->die();
                    $this->reportDeath($kobold);
                }
            }
        }

        $drownedNames = array_map(fn($k) => $k->getName(), $drowned);
        $lossText = $foodLost > 0 ? "The flood ruined {$this->tribe->foodTimeStr($foodLost)} of food." : "Food stores were battered but not fully ruined.";
        if ($harvestText !== '') {
            $lossText .= " {$harvestText}";
        }
        $drownText = empty($drownedNames)
            ? "Miraculously, no one was swept away."
            : "The waters took " . implode(', ', $drownedNames) . ", and the tribe grieved as the flood passed.";

        $variations = [
            "Autumn rains swelled into a massive flood that tore through the camp. {$lossText} {$drownText}",
            "The river burst its banks and swallowed the lower dens. {$lossText} {$drownText}",
            "A roaring flood swept across the tribe's ground and left ruin behind. {$lossText} {$drownText}"
        ];

        $this->story = $variations[array_rand($variations)];
    }
}
