<?php
namespace sammo\Command\General;

use \sammo\{
    DB, Util, JosaUtil,
    General,
    ActionLogger,
    GameConst, GameUnitConst,
    LastTurn,
    Command,
    Json,
    KVStorage,
    StaticEventHandler,
    TurnExecutionHelper
};


use \sammo\Constraint\Constraint;
use \sammo\Constraint\ConstraintHelper;
use sammo\CityConst;
use sammo\Enums\EventTarget;
use sammo\Event\EventHandler;

use function sammo\refreshNationStaticInfo;
use function sammo\deleteNation;

class che_해산 extends Command\GeneralCommand{
    static protected $actionName = '해산';

    protected function argTest():bool{
        $this->arg = [];

        return true;
    }

    protected function init(){

        $general = $this->generalObj;
        $env = $this->env;

        $this->setCity();
        $this->setNation(['gennum']);

        $this->fullConditionConstraints=[
            ConstraintHelper::BeLord(),
            ConstraintHelper::WanderingNation(),
        ];
    }

    public function getCost():array{
        return [0, 0];
    }

    public function getPreReqTurn():int{
        return 0;
    }

    public function getPostReqTurn():int{
        return 0;
    }

    public function run(\Sammo\RandUtil $rng):bool{
        if(!$this->hasFullConditionMet()){
            throw new \RuntimeException('불가능한 커맨드를 강제로 실행 시도');
        }

        $db = DB::db();
        $env = $this->env;

        $general = $this->generalObj;
        $logger = $general->getLogger();
        $date = $general->getTurnTime($general::TURNTIME_HM);

        $initYearMonth = Util::joinYearMonth($env['init_year'], $env['init_month']);
        $yearMonth = Util::joinYearMonth($env['year'], $env['month']);
        if($yearMonth <= $initYearMonth){
            $logger->pushGeneralActionLog("다음 턴부터 해산할 수 있습니다. <1>$date</>");
            $this->alternative = new che_인재탐색($general, $this->env, null);
            return false;
        }

        $generalName = $general->getName();
        $josaYi = JosaUtil::pick($generalName, '이');

        $nation = $this->nation;
        $nationID = $nation['nation'];
        $nationName = $nation['name'];
        $josaUl = JosaUtil::pick($nationName, '을');

        $db->update('general', [
            'gold'=>GameConst::$defaultGold
        ], 'nation=%i AND gold>%i', $nationID, GameConst::$defaultGold);
        $db->update('general', [
            'rice'=>GameConst::$defaultRice
        ], 'nation=%i AND gold>%i', $nationID, GameConst::$defaultRice);

        $general->increaseVarWithLimit('gold', 0, 0, GameConst::$defaultGold);
        $general->increaseVarWithLimit('rice', 0, 0, GameConst::$defaultRice);

        refreshNationStaticInfo();


        $logger->pushGeneralActionLog("세력을 해산했습니다. <1>$date</>");
        $logger->pushGlobalActionLog("<Y>{$generalName}</>{$josaYi} 세력을 해산했습니다.");
        $logger->pushGeneralHistoryLog("<D><b>{$nationName}</b></>{$josaUl} 해산");
        $this->setResultTurn(new LastTurn(static::getName(), $this->arg));

        $nationGenerals = deleteNation($general, false);
        $general->setVar('makelimit', 12);
        foreach($nationGenerals as $oldGeneral){
            $oldGeneral->applyDB($db);
        }
        StaticEventHandler::handleEvent($this->generalObj, $this->destGeneralObj, $this::class, $this->env, $this->arg ?? []);
        $general->applyDB($db);

        // 이벤트 핸들러 동작
        TurnExecutionHelper::runEventHandler($db, null, EventTarget::OccupyCity);

        return true;
    }


}