<?php

namespace CCDN\Helpers\Modules\Module;

use CCDN\Helpers\Api\Response\Field\TypeField;
use CCDN\Helpers\Api\Response\FranchiseDetailsInterface;
use CCDN\Helpers\Entities\Config;
use CCDN\Helpers\Entities\Post;

class NotSeasonsFranchiseTitle
{

    /**
     * @param  Config  $config
     * @param  FranchiseDetailsInterface  $response
     * @param  Post  $post
     * @return string|null
     */
    public static function handler(Config $config, FranchiseDetailsInterface $response, Post $post)
    {
        if ($config->module_update_title !== '1') {

            return $post->title;
        }
        $segments = new PatterParser();

        $title = $config->module_title_pattern_not_season;

        if (empty($title)) {
            return $post->title;
        }

        $title = $segments->replaceEpisode($title, '');
        $title = $segments->replaceSeason($title, '');

        $title = $segments->replaceYear($title, $response->getYear());

        $title = $segments->replaceOriginName($title, $response->getNameEng());

        $title = $segments->replaceTitle($title, $response->getName());

        $franchiseTypeField = $response->getType();

        if ($franchiseTypeField->is(TypeField::FILM)) {
            $title = $segments->replaceFranchiseType($title, $config->module_franchise_type_film);
        }
        if ($franchiseTypeField->is(TypeField::SERIAL)) {
            $title = $segments->replaceFranchiseType($title, $config->module_franchise_type_series);
        }
        if ($franchiseTypeField->is(TypeField::CARTOON)) {
            $title = $segments->replaceFranchiseType($title, $config->module_franchise_type_cartoon);
        }
        if ($franchiseTypeField->is(TypeField::CARTOON_SERIAL)) {
            $title = $segments->replaceFranchiseType($title, $config->module_franchise_type_cartoon_series);
        }
        if ($franchiseTypeField->is(TypeField::TV_SHOW)) {
            $title = $segments->replaceFranchiseType($title, $config->module_franchise_type_tv_show);
        }
        if ($franchiseTypeField->is(TypeField::ANIME_FILM)) {
            $title = $segments->replaceFranchiseType($title, $config->module_franchise_type_anime_film);
        }
        if ($franchiseTypeField->is(TypeField::ANIME_SERIAL)) {
            $title = $segments->replaceFranchiseType($title, $config->module_franchise_type_anime_series);
        }

        return $title;
    }
}
