<?php
namespace common\modules\blog\controllers;

use common\models\Language;
use common\modules\blog\models\ArticleCategory;
use common\modules\blog\models\ArticleCategoryLang;
use common\modules\blog\models\ArticleCategoryMedia;
use common\modules\blog\models\ArticleLang;
use common\modules\blog\models\ArticleMedia;
use yii\base\InvalidParamException;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;

class AjaxController extends Controller
{
    public function beforeAction($action)
    {
        if(!\Yii::$app->request->getIsAjax()) {
            //throw new ForbiddenHttpException('Permission denied');
        }

        if(!parent::beforeAction($action)) {
            return false;
        }

        return true;
    }

    public function actionCategoryForm($language_id, $widget_id)
    {
        $model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['status' => 1, 'language_id' => $language_id])->one();
        if(!$model) {
            throw new NotFoundHttpException('Language not found');
        }
        $category_lang = new ArticleCategoryLang();
        return $this->renderAjax('_category_form', ['model' => $model, 'category_lang' => $category_lang, 'widget_id' => $widget_id]);
    }

    public function actionArticleForm($language_id, $widget_id)
    {
        $model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['status' => 1, 'language_id' => $language_id])->one();
        if(!$model) {
            throw new NotFoundHttpException('Language not found');
        }
        $article_lang = new ArticleLang();
        return $this->renderAjax('_article_form', ['model' => $model, 'article_lang' => $article_lang, 'widget_id' => $widget_id]);
    }

    public function actionArticleMediaForm($language_id, $widget_id, $type)
    {
        $model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['status' => 1, 'language_id' => $language_id])->one();
        if(!$model) {
            throw new NotFoundHttpException('Language not found');
        }
        if(!in_array($type, ['full', 'preview'])) {
            throw new InvalidParamException('Type must only be full/preview');
        }
        $article_lang = new ArticleMedia();
        return $this->renderAjax('_article_media_form', ['model' => $model, 'article_lang' => $article_lang, 'widget_id' => $widget_id, 'type' => $type]);
    }

    public function actionArticleCategoryMediaForm($language_id, $widget_id, $type)
    {
        $model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['status' => 1, 'language_id' => $language_id])->one();
        if(!$model) {
            throw new NotFoundHttpException('Language not found');
        }
        if(!in_array($type, ['full', 'preview'])) {
            throw new InvalidParamException('Type must only be full/preview');
        }
        $article_lang = new ArticleCategoryMedia();
        return $this->renderAjax('_article_media_form', ['model' => $model, 'article_lang' => $article_lang, 'widget_id' => $widget_id, 'type' => $type]);
    }

    public function actionRemoveImage()
    {
        $post = \Yii::$app->request->post();
        if(!empty($post['article_media_id'])) {
            $article_media = ArticleMedia::findOne($post['article_media_id']);
            if($post['remove_media']) {
                $media = $article_media->media->delete();
            }
            if(!empty($article_media)) {
                $article_media->delete();
            }
            return true;
        } else {
            return false;
        }
    }

    public function actionRemoveCategoryImage()
    {
        $post = \Yii::$app->request->post();
        if(!empty($post['category_media_id'])) {
            $category_media = ArticleCategoryMedia::findOne($post['category_media_id']);
            if($post['remove_media']) {
                $media = $category_media->media->delete();
            }
            if(!empty($category_media)) {
                $category_media->delete();
            }
            return true;
        } else {
            return false;
        }
    }

    public function actionRemoveImageCategory()
    {
        $post = \Yii::$app->request->post();
        if(!empty($post['category_media_id'])) {
            $category_media = ArticleCategoryMedia::findOne($post['category_media_id']);
            if($post['remove_media']) {
                $media = $category_media->media->delete();
            }
            if(!empty($category_media)) {
                $category_media->delete();
            }
            return true;
        } else {
            return false;
        }
    }

    public function actionMultilangForm($model, $ajaxView, $widget_id, $language_id = NULL)
    {
        $model = new $model(['language_id' => $language_id]);
        return $this->renderAjax($ajaxView, ['model' => $model, 'widget_id' => $widget_id]);
    }

}
