<?php

require_once __DIR__ . '/../../src/Events/Event.php';
require_once __DIR__ . '/../../src/Utilities/GameHelper.php';

class FarmingEvent extends Event
{
    public function getMinParticipants()
    {
        return 2;
    }

    public function getMaxParticipants()
    {
        return 10;
    }

    public function getCost()
    {
        switch ($this->tribe->getCurrentSeason()) {
            case 'spring':
                return 30;
            case 'summer':
                return 35;
            case 'autumn':
                return 40;
            case 'winter':
                return 90;
            default:
                return 60;
        }
    }

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

    public function execute()
    {
        $participantIds = $this->getParticipants();
        $explorers = [];
        foreach ($participantIds as $pid) {
            $k = $this->tribe->getKobold($pid);
            if ($k && !$k->isDead())
                $explorers[] = $k;
        }

        $leader = GameHelper::findBestKoboldForStat($explorers, "intelligence");
        $hardestWorker = GameHelper::findBestKoboldForStat($explorers, "vitality");

        $this->story = "The farmers tended to their crops. {$leader->getName()} took the lead.\n";

        for ($i = 0; $i < count($explorers); $i++) {
            $farmer = $explorers[$i];

            if (!$farmer->checkStat('will')) {
                $farmer->addInjury("cut");
                $this->story .= "{$farmer->getName()} had an accident plowing the field, cutting {$farmer->his()} leg with the hoe.\n";
                break;
            }
        }

        for ($i = 0; $i < count($explorers); $i++) {
            $farmer = $explorers[$i];

            if ($farmer->checkStat('intelligence')) {
                $herbs = GameHelper::loadItem('healing-herbs');
                $this->tribe->addToStorage($herbs, rand(1, 3));
                $this->story .= "{$farmer->getName()} managed to grow some useful healing herbs while tending the crops.\n";
                break;
            }
        }

        if (!$leader->checkStat('intelligence')) {
            $this->story .= "Unfortunately, {$leader->getName()} made several mistakes in planting and caring for the crops.\n";
            $this->story .= "Despite {$hardestWorker->getName()}'s best efforts, the crops had rotten and were lost.\n";
            return $this->story;
        } else {
            $totalFood = 0;
            $mostFood = 0;
            $mostFarmedBy = '';

            for ($i = 0; $i < count($explorers); $i++) {
                $farmer = $explorers[$i];

                $farmed = rand(0, $this->tribe->foodRequirements[$farmer->getAgeCategory()] * 3);
                $totalFood += $farmed;

                if ($farmed > $mostFood) {
                    $mostFood = $farmed;
                    $mostFarmedBy = $farmer->getName();
                }
            }

            if ($mostFarmedBy == $hardestWorker->getName()) {
                $this->story .= "{$hardestWorker->getName()} worked the hardest and managed to produce the most food for the tribe!\n";
            } else {
                $this->story .= "Despite {$hardestWorker->getName()} working the hardest out of everyone, it was {$mostFarmedBy} who produced the most food for the tribe.\n";
            }

            $months = $this->tribe->foodMonthsLeft($totalFood);
            $this->tribe->addFood($totalFood);

            if ($months < 1)
                $this->story .= "The tribe gained less than a month worth of food from the harvest.\n";
            else {
                if ($months == 1)
                    $this->story .= "The tribe gained a month worth of food from the harvest.\n";
                else
                    $this->story .= "The tribe gained " . $months . " months worth of food from the harvest!\n";
            }
            return $this->story;
        }
    }
}
