<?php

namespace common\modules\blog\models;

use common\modules\bids\models\Bid;
use common\modules\triggers\models\TriggerLogs;
use common\modules\triggers\models\TriggerSchedule;
use Yii;
use yii\helpers\Url;

use common\modules\languages\models\Languages;
use common\modules\blog\models\PostLang;
use common\modules\blog\models\PostTag;
use common\modules\users\models\User;
use common\models\MetaTags;
use common\modules\sessions\models\SessionUrl;
use frontend\modules\sitemap\behaviors\SitemapBehavior;

/**
 * This is the model class for table "posts".
 *
 * @property integer $id
 * @property string $url
 * @property integer $active
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property PostsLang[] $postsLangs
 * @property PostsTagsAssign[] $postsTagsAssigns
 * @property PostsTags[] $tags
 */
class Post extends \common\components\ActiveRecordModel
{
    const ACTIVE_FALSE = 0;
    const ACTIVE_TRUE = 1;

    const PAGE_SIZE = 2;

    const MESSAGE_TEMPLATE_ID = 1;

    public static $active_title = [
        self::ACTIVE_FALSE => 'Скрыта',
        self::ACTIVE_TRUE => 'Доступна',
    ];

    /**
     * @param bool $insert
     * @param array $changedAttributes
     */
    public function afterSave($insert, $changedAttributes){
        parent::afterSave($insert, $changedAttributes);

        $users = User::find()->all();
        foreach($users as $user) {
            if (Bid::find()->where(['email' => $user->email])->exists()==true && TriggerLogs::find()->where(['user_id' => $user->id, 'action' => TriggerLogs::USER_SUBSCRIBE_TO_BLOG])==true) {
                $templateData = [];
                TriggerSchedule::sendMessage($user->email, self::MESSAGE_TEMPLATE_ID, $templateData);
            }
        }
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'posts';
    }

    public function name()
    {
        return 'Посты';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['url', 'active'], 'required'],
            [['active', 'created_at', 'updated_at', 'author_id'], 'integer'],
            [['url'], 'string', 'max' => 255],
            [['url'], 'unique'],
            [['preview', 'unlinkFile', 'tags'], 'safe'],
            [['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'meta' => [
                'class' => 'common\components\activeRecordBehaviors\MetaTagBehavior',
                'actions' => ['create', 'update']
            ],
            'langs' => [
                'class' => 'common\modules\languages\components\LanguageHelperBehavior',
                'field' => 'post_id',
                'langClass' => 'common\modules\blog\models\PostLang',
                'actions' => ['create', 'update']
            ],
            'tags' => [
                'class' => 'common\modules\blog\components\TagBehavior',
            ],
            'Timestamp'      => [
                'class' => '\yii\behaviors\TimestampBehavior',
            ],
            'file' => [
                'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior',
                'thumb' => true,
                'thumbSize' => ['280', '141'],
                'path' => '@frontend/web',
                'folder' => '/uploads/blog/',
                'field' => 'preview'
            ],
            'sitemap' => [
                'class' => SitemapBehavior::className(),
                'scope' => function ($model) {
                    /** @var \yii\db\ActiveQuery $model */
                    $model->select(['url', 'updated_at']);
                    $model->andWhere(['active' => 1]);
                },
                'dataClosure' => function ($model) {
                    return [
                        'loc' => Url::to('blog/'.$model->url),
                        'lastmod' => date('c', $model->updated_at),
                        'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
                        'priority' => 0.5
                    ];
                }
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'author_id' => 'Автор',
            'url' => 'Ссылка',
            'active' => 'Видимость',
            'file' => 'Изображение',
            'tags' => 'Теги',
            'preview' => 'Изображение',
            'unlinkFile' => 'Удалить изображение',
            'created_at' => 'Дата добавления',
            'updated_at' => 'Дата обновления',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMetaTags($lang_id = null) 
    {
        $query = $this->hasMany(MetaTags::className(), ['object_id' => 'id'])->where(['model_id'  => get_class($this)]);

        if($lang_id)
        {
            $query->andWhere(['lang_id' => $lang_id]);
        }

        return $query;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMetaTag($lang_id = null) 
    {
        $lang_id = ($lang_id === null)? Languages::getCurrent()->id: $lang_id;

        return $this->hasOne(MetaTags::className(), ['object_id' => 'id'])->where([
            'model_id'  => get_class($this),
            'lang_id' => $lang_id
        ]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLang($lang_id = null)
    {
        $lang_id = ($lang_id === null)? Languages::getCurrent()->id: $lang_id;

        return $this->hasOne(PostLang::className(), ['post_id' => 'id'])->where('lang_id = :lang_id', [':lang_id' => $lang_id]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLangs()
    {
        return $this->hasMany(PostLang::className(), ['post_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPostTags()
    {
        $lang = Languages::getCurrent();

        $query = $this->hasMany(PostTag::className(), ['id' => 'tag_id'])->viaTable('posts_tags_assign', ['post_id' => 'id']);

        if($lang->default)
        {
            $query = $query->andWhere('lang_id = '.$lang->id.' OR lang_id IS NULL');
        }
        else
        {
            $query = $query->andWhere([
                'lang_id' => $lang->id
            ]);
        }
        
        return $query;        
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPostTagAssigns()
    {
        return $this->hasMany(PostTagAssign::className(), ['post_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAuthor()
    {
        return $this->hasOne(User::className(), ['id' => 'author_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getViews()
    {
        return SessionUrl::find()->where(['url' => $this->getFullUrl()]);
    }

    public function getFullUrl()
    {
        return Url::to(['/blog/' . $this->url]);
    }

    public function getThumbnailUrl()
    {
        $path = pathinfo($this->preview);

        return $path['dirname'] . '/' . $path['filename'] . '.thumb.' . $path['extension'];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) 
        {
            $this->author_id = Yii::$app->user->identity->id;
            return true;
        } 
        else 
        {
            return false;
        }
    }
}
