<?php
namespace sammo\Command\Nation;

use \sammo\{
    DB, Util, JosaUtil,
    General, DummyGeneral,
    ActionLogger,
    GameConst,
    LastTurn,
    GameUnitConst,
    Command,
    MessageTarget,
    Message,
    CityConst
};

use function \sammo\{
    getDomesticExpLevelBonus,
    CriticalRatioDomestic, 
    CriticalScoreEx,
    GetImageURL,
    getNationStaticInfo 
};

use \sammo\Constraint\Constraint;
use \sammo\Constraint\ConstraintHelper;
use sammo\Event\Action;

class che_수몰 extends Command\NationCommand{
    static protected $actionName = '수몰';
    static public $reqArg = false;

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

        return true;
    }

    protected function init(){
        $general = $this->generalObj;

        $env = $this->env;

        $this->setCity();
        $this->setNation(['strategic_cmd_limit']);
        
        $this->runnableConstraints=[
            ConstraintHelper::OccupiedCity(),
            ConstraintHelper::BeChief(),
            ConstraintHelper::NotNeutralDestCity(),
            ConstraintHelper::NotOccupiedDestCity(),
            ConstraintHelper::BattleGroundCity(),
            ConstraintHelper::AvailableStrategicCommand()
        ];
    }
    
    public function getCost():array{
        return [0, 0];
    }
    
    public function getPreReqTurn():int{
        return 2;
    }

    public function getPostReqTurn():int{
        $genCount = Util::valueFit($this->nation['gennum'], GameConst::$initialNationGenLimit);
        $nextTerm = Util::round(sqrt($genCount*8)*10);    

        $nextTerm = $this->generalObj->onCalcStrategic($this->getName(), 'delay', $nextTerm);
        return $nextTerm;
    }

    public function getBrief():string{
        $commandName = $this->getName();
        $destCityName = CityConst::byID($this->arg['destCityID'])->name;
        $josaUl = JosaUtil::pick($destCityName, '을');
        return "【{$destCityName}】{$josaUl} {$commandName}";
    }

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

        $db = DB::db();

        $general = $this->generalObj;
        $generalID = $general->getID();
        $generalName = $general->getName();
        $date = $general->getTurnTime($general::TURNTIME_HM);

        $year = $this->env['year'];
        $month = $this->env['month'];

        $destCity = $this->destCity;
        $destCityID = $destCity['city'];
        $destCityName = $destCity['name'];

        $destNationID = $destCity['nation'];
        $destNationName = getNationStaticInfo($destNationID)['name'];
        
        $nationID = $general->getNationID();
        $nationName = $this->nation['name'];

        $logger = $general->getLogger();
        $logger->pushGeneralActionLog("수몰 발동! <1>$date</>");

        $general->increaseVar(
            'experience',
            $general->onCalcStat($general,
            'experience', 5 * ($this->getPreReqTurn() + 1)
        ));
        $general->increaseVar(
            'dedication',
            $general->onCalcStat($general,
            'dedication', 5 * ($this->getPreReqTurn() + 1)
        ));

        $josaYi = JosaUtil::pick($generalName, '이');

        $broadcastMessage = "<Y>{$generalName}</>{$josaYi} <G><b>{$destCityName}</b></>에 <M>수몰</>을 발동하였습니다.";

        $targetGeneralList = $db->queryFirstColumn('SELECT no FROM general WHERE nation=%i AND no != %i', $nationID, $generalID);
        foreach($targetGeneralList as $targetGeneralID){
            $targetLogger = new ActionLogger($targetGeneralID, $nationID, $year, $month);
            $targetLogger->pushGeneralActionLog($broadcastMessage, ActionLogger::PLAIN);
            $targetLogger->flush();
        }

        $destBroadcastMessage = "<G><b>{$destCityName}</b></>에 <M>수몰</>이 발동되었습니다.";

        $targetGeneralList = $db->queryFirstColumn('SELECT no FROM general WHERE nation=%i', $destNationID);
        $destNationLogged = false;
        foreach($targetGeneralList as $targetGeneralID){
            $targetLogger = new ActionLogger($targetGeneralID, $destNationID, $year, $month);
            $targetLogger->pushGeneralActionLog($destBroadcastMessage, ActionLogger::PLAIN);
            if(!$destNationLogged){
                $targetLogger->pushNationalHistoryLog(
                    "<D><b>{$nationName}</b></>의 <Y>{$generalName}</>{$josaYi} 아국의 <G><b>{$destCityName}</b></>에 <M>수몰</>을 발동", ActionLogger::PLAIN
                );
                $destNationLogged = true;
            }
            $targetLogger->flush();
        }

        $db->update('city', [
            'def' => $db->sqleval('def * 0.2'),
            'wall' => $db->sqleval('wall * 0.2'),
        ], 'city=%i', $destCityID);
        

        $logger->pushGeneralHistoryLog('<M>수몰</>을 발동');
        $logger->pushNationalHistoryLog("<Y>{$generalName}</>{$josaYi} <D><b>{$destNationName}</b></>의 <G><b>{$destCityName}</b></>에 <M>수몰</>을 발동");

        $db->update('nation', ['strategic_cmd_limit' => $this->getPostReqTurn()], 'nation=%i', $nationID);

        $general->setResultTurn(new LastTurn($this->getName(), $this->arg, 0));
        $general->applyDB($db);

        return true;
    }
}