<?php


namespace CCDN\Helpers\Modules\Calendar;


use CCDN\Helpers\Api\Response\Field\TypeField;
use CCDN\Helpers\Api\Response\FranchiseCalendar;
use CCDN\Helpers\Api\Response\FranchiseCalendarInterface;
use CCDN\Helpers\Entities\Post;
use GuzzleHttp\Psr7\Response as GuzzleResponse;

class Handler
{

    /**
     * @param  array  $waitResponses
     * @return array
     */
    public function responseHandler($waitResponses)
    {
        $response = [];
        $itemsUnq = [];
        foreach ($waitResponses as $waitRespons) {
            if ($waitRespons['state'] === 'rejected') {
                continue;
            }
            /**
             * @var GuzzleResponse $response
             */
            $waitRespons = $waitRespons['value'];
            if ($waitRespons->getStatusCode() !== 200) {
                continue;
            }
            $arr = json_decode($waitRespons->getBody()->getContents(), true);

            foreach ($arr['items'] as $item) {
                $type = new TypeField($item['type']);
                if ($type->isSeasons()) {
                    if (isset($item['season']) && !isset($item['episode'])) {
                        continue;
                    }
                    if (!isset($item['season']) && !isset($item['episode'])) {
                        continue;
                    }
                }

                $itemsUnq[$item['id']] = $item;
            }
            foreach ($itemsUnq as $item) {
                $response[$item['availability']][] = new FranchiseCalendar($item);
            }
        }
        return $response;
    }


    /**
     * @param  array  $response
     * @return array
     */
    public function getCCDNIdFromResponse($response)
    {
        $ids = [];

        foreach ($response as $item) {
            /** @var FranchiseCalendarInterface $value */
            foreach ($item as $value) {
                $id = $value->getId();
                $ids[$id] = $id;
            }
        }

        return $ids;
    }

    /**
     * @param  Post  $post
     * @return string
     */
    public function createNewsUlr(Post $post)
    {
        global $config;

        $category_id = (int) $post->category;

        if ($config['allow_alt_url']) {
            if ($config['seo_type'] === '1' || $config['seo_type'] === '2') {
                if ($category_id && $config['seo_type'] === '2') {
                    $c_url = get_url($category_id);
                    $full_link = "/{$c_url}/{$post->id}-{$post->alt_name}.html";

                } else {
                    $full_link = "/{$post->id}-{$post->alt_name}.html";
                }
            } else {
                $post->date = is_numeric($post->date) ? $post->date : strtotime($post->date);
                $date = date('Y/m/d/', $post->date);
                $full_link = '/'.$date.$post->alt_name.'.html';
            }
        } else {
            $full_link = '/index.php?newsid='.$post->id;
        }


        return $full_link;
    }
}
