<?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 NotSeasonsFranchiseMetaTitle
{


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

        $segments = new PatterParser();

        $metaTitle = $config->module_title_two_pattern_not_season;

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

        $metaTitle = $segments->replaceEpisode($metaTitle, '');

        $metaTitle = $segments->replaceSeason($metaTitle, '');

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

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

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

        $franchiseTypeField = $response->getType();

        if ($franchiseTypeField->is(TypeField::FILM)) {
            $metaTitle = $segments->replaceFranchiseType($metaTitle, $config->module_franchise_type_film_two);
        }
        if ($franchiseTypeField->is(TypeField::SERIAL)) {
            $metaTitle = $segments->replaceFranchiseType($metaTitle, $config->module_franchise_type_series_two);
        }
        if ($franchiseTypeField->is(TypeField::CARTOON)) {
            $metaTitle = $segments->replaceFranchiseType($metaTitle, $config->module_franchise_type_cartoon_two);
        }
        if ($franchiseTypeField->is(TypeField::CARTOON_SERIAL)) {
            $metaTitle = $segments->replaceFranchiseType($metaTitle,
                $config->module_franchise_type_cartoon_series_two);
        }
        if ($franchiseTypeField->is(TypeField::TV_SHOW)) {
            $metaTitle = $segments->replaceFranchiseType($metaTitle, $config->module_franchise_type_tv_show_two);
        }
        if ($franchiseTypeField->is(TypeField::ANIME_FILM)) {
            $metaTitle = $segments->replaceFranchiseType($metaTitle, $config->module_franchise_type_anime_film_two);
        }
        if ($franchiseTypeField->is(TypeField::ANIME_SERIAL)) {
            $metaTitle = $segments->replaceFranchiseType($metaTitle,
                $config->module_franchise_type_anime_series_two);
        }


        return $metaTitle;

    }
}
