<?php

namespace rss\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

/**
 * DefaultController implements actions
 */
class DefaultController extends Controller
{

    public $defaultAction = 'rss';

	public function actionIndex()
    {
        return $this->redirect("rss");
    }


    /**
     * Displays the Yandex turbo-pages for frontend.
     *
     * @return string
     */
    public function actionTurbo() {
        $module = $this->module;
        if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache()) ) {
            $data = $cache->getOrSet(md5('yandex-turbo'), function () use ($module) {
                return [
                    'items' => $module->getTurboItems(),
                    'builded_at' => date('r')
                ];
            }, intval($module->cacheExpire));

        } else { 
            $data = [
                'items' => $module->getTurboItems(),
                'builded_at' => date('r')
            ];
        }
        $channel = [];
        if (is_array($module->channelOptions))
            $channel = $module->channelOptions;

        if (!isset($channel['title']))
            $channel['title'] = Yii::$app->name;

        if (!isset($channel['link']))
            $channel['link'] = Url::base(true);

        if (!isset($channel['language']))
            $channel['language'] = Yii::$app->language;

        Yii::$app->response->format = Response::FORMAT_XML;
        Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
        return $this->renderPartial('turbo', [
            'channel' => $channel,
            'items' => $data['items']
        ]);
    }

    public function actionDzen() {
		$topic = \Yii::$app->request->get("topic",0);
        $module = $this->module;
        if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache()) ) {
            $data = $cache->getOrSet(md5('yandex-dzen'.$topic), function () use ($module, $topic) {
                return [
                    'items' => ($topic)?$module->getTopicsItems($topic):$module->getTurboItems(),
                    'builded_at' => date('r')
                ];
            }, intval($module->cacheExpire));

        } else {
            $data = [
                'items' => ($topic)?$module->getTopicsItems($topic):$module->getTurboItems(),
                'builded_at' => date('r')
            ];
        }
        $channel = [];
        if (is_array($module->channelOptions))
            $channel = $module->channelOptions;

        if (!isset($channel['title']))
            $channel['title'] = Yii::$app->name;

        if (!isset($channel['link']))
            $channel['link'] = Url::base(true);

        if (!isset($channel['language']))
            $channel['language'] = Yii::$app->language;

        Yii::$app->response->format = Response::FORMAT_XML;
        Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
		$channel['clink'] = $channel['link'];
		if( $topic ){
			$mtopic = new \app\models\base\NewsTopic();
			$ptopic = $mtopic->GetById($topic);
			$channel['title'] = $ptopic->meta_title;
			$channel['clink'] = $ptopic->geturl(true);
			return $this->renderPartial('dzentopic', [
				'channel' => $channel,
				'items' => $data['items']
			]);
		}
        return $this->renderPartial('dzen', [
            'channel' => $channel,
            'items' => $data['items']
        ]);
    }

    public function actionRia() {
        $module = $this->module;
        if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache()) ) {
            $data = $cache->getOrSet(md5('yandex-ria'), function () use ($module) {
                return [
                    'items' => $module->getTurboItems(),
                    'builded_at' => date('r')
                ];
            }, intval($module->cacheExpire));

        } else {
            $data = [
                'items' => $module->getTurboItems(),
                'builded_at' => date('r')
            ];
        }
        $channel = [];
        if (is_array($module->channelOptions))
            $channel = $module->channelOptions;

        if (!isset($channel['title']))
            $channel['title'] = Yii::$app->name;

        if (!isset($channel['link']))
            $channel['link'] = Url::base(true);

        if (!isset($channel['language']))
            $channel['language'] = Yii::$app->language;

        Yii::$app->response->format = Response::FORMAT_XML;
        Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
        return $this->renderPartial('ria', [
            'channel' => $channel,
            'items' => $data['items']
        ]);
    }

    public function actionRss() {
        $module = $this->module;
        if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache() )) {
            $data = $cache->getOrSet(md5('rss'), function () use ($module) {
                return [
                    'items' => $module->getTurboItems(),
                    'builded_at' => date('r')
                ];
            }, intval($module->cacheExpire));

        } else {
            $data = [
                'items' => $module->getTurboItems(),
                'builded_at' => date('r')
            ];
        }
        $channel = [];
        if (is_array($module->channelOptions))
            $channel = $module->channelOptions;

        if (!isset($channel['title']))
            $channel['title'] = Yii::$app->name;

        if (!isset($channel['link']))
            $channel['link'] = Url::base(true);

        if (!isset($channel['language']))
            $channel['language'] = Yii::$app->language;

        Yii::$app->response->format = Response::FORMAT_XML;
        Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
        return $this->renderPartial('rss', [
            'channel' => $channel,
            'items' => $data['items']
        ]);
    }

}
