<?php
namespace sammo\Event\Action;
use \sammo\GameConst;
use \sammo\Util;
//기존 event_3.php
class CreateManyNPC extends \sammo\Event\Action{
    protected $npcCount;
    protected $avgGen;
    public function __construct($npcCount = 10){
        $this->npcCount = $npcCount;
    }

    protected function generateNPC($env){
        $pickTypeList = ['무'=>1, '지'=>1];

        $pickType = Util::choiceRandomUsingWeight($pickTypeList);

        $totalStat = GameConst::$defaultStatNPCTotal;
        $minStat = GameConst::$defaultStatNPCMin;
        $mainStat = GameConst::$defaultStatNPCMax - Util::randRangeInt(0, GameConst::$defaultStatNPCMin);
        $otherStat = $minStat + Util::randRangeInt(0, Util::toInt(GameConst::$defaultStatNPCMin/2));
        $subStat = $totalStat - $mainStat - $otherStat;
        if ($subStat < $minStat) {
            $subStat = $otherStat;
            $otherStat = $minStat;
            $mainStat = $totalStat - $subStat - $otherStat;
            if ($mainStat) {
                throw new \LogicException('기본 스탯 설정값이 잘못되어 있음');
            }
        }

        if($pickType == '무'){
            $leadership = $subStat;
            $strength = $mainStat;
            $intel = $otherStat;
        }
        else if($pickType == '지'){
            $leadership = $subStat;
            $strength = $otherStat;
            $intel = $mainStat;
        }
        else{
            $leadership = $otherStat;
            $strength = $subStat;
            $intel = $mainStat;
        }

        $leadership = Util::round($leadership);
        $strength = Util::round($strength);
        $intel = Util::round($intel);

        $age = Util::randRangeInt(20, 25);
        $birthYear = $env['year'] - $age;
        $deathYear = $env['year'] + Util::randRangeInt(10, 50);

        $cityID = Util::choiceRandom(array_keys(\sammo\CityConst::all()));
        $newNPC = new \sammo\Scenario\NPC(
            Util::randRangeInt(1, 150),
            \sammo\getRandGenName(),
            null,
            0,
            $cityID,
            $leadership,
            $strength,
            $intel,
            0,
            $birthYear,
            $deathYear,
            null,
            null
        );
        $newNPC->npc = 3;
        $newNPC->setMoney(1000, 1000);
        $newNPC->setExpDed(0, 0);
        $newNPC->setSpecYear(
            Util::round((GameConst::$retirementYear - $age) / 12) + $age,
            Util::round((GameConst::$retirementYear - $age) / 6) + $age
        );

        $newNPC->build($env);
        return [$newNPC->realName, $newNPC->generalID];
    }
    

    public function run($env=null){
        if($this->npcCount <= 0){
            return [__CLASS__, []];   
        }
        $result = [];
        foreach(Util::range($this->npcCount) as $idx){
            $result[] = $this->generateNPC($env);
        }

        $logger = new \sammo\ActionLogger(0, 0, $env['year'], $env['month']);
        $genCnt = count($result);
        if($genCnt == 1){
            $npcName = $result[0][0];
            $josaRa = \sammo\JosaUtil::pick($npcName, '라');
            $logger->pushGlobalActionLog("<Y>$npcName</>{$josaRa}는 장수가 <S>등장</>하였습니다.");
        }
        else{
            $logger->pushGlobalActionLog("장수 <C>{$genCnt}</>명이 <S>등장</>하였습니다.");
        }
        $logger->pushGlobalHistoryLog("장수 <C>{$genCnt}</>명이 <S>등장</>했습니다.", \sammo\ActionLogger::NOTICE_YEAR_MONTH);
        $logger->flush();

        return [__CLASS__, $result];   
    }
}