<?php

/**
 * 行動パターンクラス
 *
 */
class BehaviorPattern
{
    /** @var PersonalityStrategy $personalityStrategy 性格行動パターンストラテジー */
    private PersonalityStrategy $personalityStrategy;

    /** @var BehaviorWeightStrategy 重みストラテジー */
    private BehaviorWeightStrategy $weightStrategy;

    /**
     * constructor
     *
     * @param Personality $personality 性格
     * @param Emotion $emotion 感情
     */
    public function __construct(Personality $personality, Emotion $emotion)
    {
        $this->setPersonalityStrategyFromPersonality($personality);
        $this->setWeightStrategyFromEmotion($emotion);
    }

    /**
     * 性格を設定
     *
     * @param Personality $personality
     * @return void
     */
    public function setPersonalityStrategyFromPersonality(Personality $personality): void
    {
        $this->personalityStrategy = match ($personality) {
            Personality::PRUDENT => new PrudentPersonality(),
            Personality::ASSERTIVE => new AssertivePersonality(),
            Personality::CAPRICIOUS => new CapriciousPersonality(),
            Personality::TIMID => new TimidPersonality(),
        };
    }

    /**
     * 重みストラテジーを感情から設定
     *
     * @param Emotion $emotion
     * @return void
     */
    public function setWeightStrategyFromEmotion(Emotion $emotion): void
    {
        $this->weightStrategy = match ($emotion) {
            Emotion::COMMON => new CommonBehaviorWeightStrategy(),
            Emotion::ARROGANCE => new ArroganceBehaviorWeightStrategy(),
            Emotion::CAUTION => new CautionBehaviorWeightStrategy(),
            Emotion::FEAR => new FearBehaviorWeightStrategy(),
            Emotion::ANGER => new AngerBehaviorWeightStrategy(),
        };
    }

    /**
     * 行動を抽選
     *
     * @return Behavior
     */
    public function lotteryBehavior(): Behavior
    {
        $weights = $this->weightStrategy->getWeights($this->personalityStrategy);
        return Behavior::from(RandomUtil::weightLottery($weights));
    }
}