<?php

namespace common\modules\blog\models;

use common\models\Media;
use Yii;
use yii\web\UploadedFile;

/**
 * This is the model class for table "article_media".
 *
 * @property integer $article_media_id
 * @property integer $article_id
 * @property integer $media_id
 * @property string $type
 * @property string $media_alt
 * @property string $media_title
 * @property string $media_caption
 * @property integer $language_id
 *
 * @property Article $article
 * @property Media $media
 */
class ArticleMedia extends \yii\db\ActiveRecord
{
    const SCENARIO_FULL = 'full';
    const SCENARIO_PREVIEW = 'preview';
    const SCENARIO_ADDITIONAL = 'additional';
    public $imageFile;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'article_media';
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_FULL] = ['article_media_id', 'article_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile', 'language_id'];
        $scenarios[self::SCENARIO_PREVIEW] = ['article_media_id', 'article_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile', 'language_id'];
        $scenarios[self::SCENARIO_ADDITIONAL] = ['article_media_id', 'article_id', 'media_id', 'type', 'imageFile', 'language_id'];
        return $scenarios;
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['article_id', 'media_id'], 'required'],
            [['article_id', 'media_id', 'language_id'], 'integer'],
            [['media_alt', 'media_title', 'media_caption'], 'string'],
            [['type'], 'string', 'max' => 10],
            [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_FULL],
            [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_PREVIEW],
            [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'maxFiles' => 10, 'on' => self::SCENARIO_ADDITIONAL]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'article_media_id' => Yii::t('app', 'ID'),
            'article_id' => Yii::t('app', 'Article ID'),
            'media_id' => Yii::t('app', 'Media ID'),
            'type' => Yii::t('app', 'Type'),
            'media_alt' => Yii::t('app', 'Media Alt'),
            'media_title' => Yii::t('app', 'Media Title'),
            'media_caption' => Yii::t('app', 'Media Caption'),
            'imageFile' => Yii::t('app', 'Image File'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticle()
    {
        return $this->hasOne(Article::className(), ['article_id' => 'article_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMedia()
    {
        return $this->hasOne(Media::className(), ['media_id' => 'media_id']);
    }

    public function upload($article_id)
    {
        $this->article_id = $article_id;
        if(is_array($this->imageFile)) {
            $ok = true;
            foreach($this->imageFile as $image) {
                $media_article = clone $this;
                $media = new Media();
                $media->imageFile = $image;
                $media->upload();
                $media_article->media_id = $media->media_id;
                $ok = $media_article->save() && $ok;
                unset($media_article);
            }
            return $ok;
        } elseif(!empty($this->imageFile)) {
            $media = new Media();
            $media->imageFile = $this->imageFile;
            $media->upload();
            $this->media_id = $media->media_id;
            return $this->save();
        }
    }

    public function replace($article_id, $removeMedia = false)
    {
        $this->article_id = $article_id;
        if($removeMedia && !$this->getIsNewRecord()) {
            $article_media = ArticleMedia::find()->select('media_id')->where(['article_id' => $this->article_id, 'type' => $this->type, 'language_id' => $this->language_id])->column();
            $media = array();
            foreach($article_media as $media_id) {
                $media[] = Media::findOne(['media_id' => $media_id]);
            }
            $media = array_unique($media);
            foreach($media as $one_media) {
                if($one_media instanceof Media) {
                    $one_media->delete();
                }
            }
            unset($media);
            unset($article_media);
        }
        if(is_array($this->imageFile)) {
            $ok = true;
            foreach($this->imageFile as $image) {
                $media_article = clone $this;
                $media = new Media();
                $media->imageFile = $image;
                $media->upload();
                $media_article->media_id = $media->media_id;
                $ok = $media_article->save() && $ok;
                unset($media_article);
            }
            return $ok;
        } elseif(!empty($this->imageFile)) {
            ArticleMedia::deleteAll(['article_id' => $this->article_id, 'type' => $this->type, 'language_id' => $this->language_id]);
            $media = new Media();
            $media->imageFile = $this->imageFile;
            $media->upload();
            $this->media_id = $media->media_id;
            $this->setIsNewRecord(true);
            return $this->save();
        }
    }

}
