<?php
    
    namespace frontend\controllers;
    
    use common\models\blog\Article;
    use common\models\blog\Category;
    use common\models\blog\Tag;
    use frontend\helpers\Url;
    use yii\data\ActiveDataProvider;
    use yii\db\ActiveQuery;
    use yii\helpers\ArrayHelper;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use frontend\microdata\MicrodataFabric;
    use Yii;
    use common\models\User;

    /**
     * Class BlogController
     *
     * @package frontend\controllers
     */
    class BlogController extends Controller
    {
        public function actionIndex()
        {

            $categories = Category::find()
                                  ->with(
                    [
                        'language',
                    ]
                )
                                  ->where(['status' => true])
                                  ->orderBy([ 'sort' => SORT_ASC ])
                                  ->all();
            $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => Article::find()
                                           ->orderBy(
                                               [
                                                   'created_at' => SORT_DESC,
                                               ]
                                           )
                                           ->with(
                                               [
                                                   'categories.language',
                                               ]

                                           )->with(['comments' => function (ActiveQuery $query){
                                               $query->andWhere(['status' => true]);
                        }])
                        ->with(['language','alias'])
                        ->where([ 'blog_article.status' => true ])
                        ->distinct(),
                    'pagination' => [
                        'pageSize' => 6,

                    ],
                ]
            );

	        return $this->render(
                'index',
                [
                    'categories' => $data,
                    'dataProvider' => $dataProvider,

                ]
            );
        }
    
        public function actionView($id)
        {

	        /**
	         *






	         */






            $model = $this->findModel($id);

	        $jsonLdMicrodata = [
		        'context' => 'http://schema.org',
		        "type" => "Article",
		        "mainEntityOfPage" => [
			        "@type" => "WebPage",
			        "@id" => "https://google.com/article"
		        ],
		        "headline" => "Article headline",
		        "datePublished" => date('d-m-Y', $model->created_at),
		        "dateModified" => date('d-m-Y', $model->created_at),
		        'image' => [
			        #	'https://google.com/logo.jpg'
			        ($model->image) ? Yii::$app->request->hostInfo . $model->image->getPath() : 'https://google.com/logo.jpg'
		        ],

		        "author" => [
			        "@type" => "Person",
			        "name" => 'Admin'
		        ],

		        "publisher" => [
			        "@type" => "Organization",
			        "name" => Yii::t('app', 'ABC short'),
			        "description" => ($model->language->body_preview) ? $model->language->body_preview : $model->language->title,
			        "logo" => [
				        "@type" => "ImageObject",
				        "url" => "https://google.com/logo.jpg"
			        ]
		        ]
	        ];


	        $resultMicrodata = new MicrodataFabric();
	        $resultMicrodata = $resultMicrodata::createJsonFromArticle($jsonLdMicrodata)->toJson();


            $model->views +=1;
            $model->save();
            
            $tags = Tag::find()
                       ->with([ 'language' ])
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();
            
            return $this->render(
                'view',
                [
                    'tags'  => $tags,
                    'model' => $model,
	                'jsMicrodata' => $resultMicrodata
                ]
            );
        }
    
        public function actionCategory($id)
        {
            $categories = Category::find()
                                  ->with(
                                      [
                                          'language',
                                      ]
                                  )
                                  ->where(['status' => true])
                                  ->orderBy([ 'sort' => SORT_ASC ])
                                  ->all();
            $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
    
            /**
             * @var Category $model
             */
            $model = Category::find()
                             ->where(
                                 [
                                     'id' => $id,
                                 ]
                             )
                             ->with(
                                 [
                                     'articles',
                                 ]
                             )
                             ->where(['status' => true])
                             ->orderBy([ 'sort' => SORT_ASC ])
                             ->one();
    
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $model->getArticles()
                                          ->with(
                                              [
                                                  'language',
                                                  'categories.language',
                                              ]
                                          )
                        ->where(['blog_article.status' => true])
                                          ->orderBy(
                                              [
                                                  'created_at' => SORT_DESC,
                                              ]
                                          ),
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
    
            return $this->render(
                'category',
                [
                    'dataProvider' => $dataProvider,
                    'model'        => $model,
                    'categories'   => $data
                ]
            );
        }
    
        public function actionTag($id)
        {
            $categories = Category::find()
                                  ->with(
                                      [
                                          'language',
                                      ]
                                  )
                                  ->where(['status' => true])
                                  ->orderBy([ 'sort' => SORT_ASC ])
                                  ->all();
            $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
    
            /**
             * @var Category $model
             */
            $model = Tag::find()
                        ->where(
                            [
                                'id' => $id,
                            ]
                        )
                        ->with(
                            [
                                'articles',
                            ]
                        )
                        ->one();
    
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $model->getArticles()
                                          ->with(
                                              [
                                                  'language',
                                                  'categories.language',
                                              ]
                                          )
                        ->where(['blog_article.status' => true])
                                          ->orderBy(
                                              [
                                                  'created_at' => SORT_DESC,
                                              ]
                                          ),
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
    
            return $this->render(
                'tag',
                [
                    'dataProvider' => $dataProvider,
                    'model'        => $model,
                    'categories' => $data
                ]
            );
        }
    
        /**
         * @param $id
         *
         * @return Article
         * @throws \yii\web\NotFoundHttpException
         */
        protected function findModel($id)
        {
            /**
             * @var Article | null $model
             */
            $model = Article::find()
                            ->where([ 'id' => $id ])
                            ->with(
                                [
                                    'language',
                                    'categories.language',
                                    'tags.language',
                                ]
                            )
                            ->andWhere([ 'status' => true ])
                            ->one();
    
            if (empty($model)) {
                throw new NotFoundHttpException(\Yii::t('app', 'Article not found'));
            } else {
                return $model;
            }
        }
    }