<?php

namespace common\modules\blog\models;

use common\models\Media;
use Yii;

/**
 * This is the model class for table "article_category_media".
 *
 * @property integer $article_category_media_id
 * @property integer $article_category_id
 * @property integer $media_id
 * @property string $media_alt
 * @property string $media_title
 * @property string $media_caption
 * @property string $type
 * @property string $language_id
 *
 * @property ArticleCategory $category
 * @property Media $media
 * @property Language $lang
 */
class ArticleCategoryMedia 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_category_media';
    }

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['article_category_id', 'media_id'], 'required'],
            [['article_category_id', 'media_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_category_media_id' => Yii::t('app', 'ID'),
            'article_category_id' => Yii::t('app', 'Category ID'),
            'media_id' => Yii::t('app', 'Media ID'),
            'media_alt' => Yii::t('app', 'Media Alt'),
            'media_title' => Yii::t('app', 'Media Title'),
            'media_caption' => Yii::t('app', 'Media Caption'),
            'type' => Yii::t('app', 'Type'),
            'imageFile' => Yii::t('app', 'Image File'),
            'language_id' => Yii::t('app', 'Language ID'),
        ];
    }

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

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

    public function upload($category_id)
    {
        $this->article_category_id = $category_id;
        if(is_array($this->imageFile)) {
            $ok = true;
            foreach($this->imageFile as $image) {
                $media_category = clone $this;
                $media = new Media();
                $media->imageFile = $image;
                $media->upload();
                $media_category->media_id = $media->media_id;
                $ok = $media_category->save() && $ok;
                unset($media_category);
            }
            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($category_id, $removeMedia = false)
    {
        $this->article_category_id = $category_id;
        if($removeMedia) {
            $category_media = ArticleCategoryMedia::find()->select('media_id')->where(['article_category_id' => $this->article_category_id, 'type' => $this->type])->column();
            $media = array();
            foreach($category_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($category_media);
        }
        if(is_array($this->imageFile)) {
            $ok = true;
            foreach($this->imageFile as $image) {
                $media_category = clone $this;
                $media = new Media();
                $media->imageFile = $image;
                $media->upload();
                $media_category->media_id = $media->media_id;
                $ok = $media_category->save() && $ok;
                unset($media_category);
            }
            return $ok;
        } elseif(!empty($this->imageFile)) {
            ArticleCategoryMedia::deleteAll(['category_id' => $this->article_category_id, 'type' => $this->type]);
            $media = new Media();
            $media->imageFile = $this->imageFile;
            $media->upload();
            $this->media_id = $media->media_id;
            $this->setIsNewRecord(true);
            return $this->save();
        }
    }

}
