<?php

namespace common\behaviors;

use common\modules\comment\models\CommentModel;
use common\modules\product\models\ProductImage;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
/**
 * Class RatingBehavior
 * @property CommentModel $owner
 * @package common\behaviors
 */
class SaveMultipleImgBehavior extends Behavior
{

    public $fields;
    public $imagesUpload;
    //public $directory;

    public function events()
    {
        return [
            ActiveRecord::EVENT_BEFORE_UPDATE=> 'downloadImages',
            ActiveRecord::EVENT_BEFORE_INSERT => 'downloadImages',
        ];
    }

    public function downloadImages($event)
    {
        foreach($this->fields as $field){


            $this->imagesUpload = UploadedFile::getInstances($this->owner, $field['name']);

            if ( ($images = $this->imagesUpload($field)) !== FALSE) {

                foreach ($images as $image) {
                    $imageModel = new ProductImage();
                    $imageModel->product_id = $this->owner->product_id;
                    $imageModel->image = $image;
                    $imageModel->save();
                }


            }

        }
    }



    /**
     * @param $field array ['directory','name']
     * @return array
     */

    public function imagesUpload($field)
    {
        $images = [];

        /**
         * @var $image UploadedFile
         */

        foreach ($this->imagesUpload  as $image) {

            $imageName = $image->baseName . '.' . $image->extension;
            $i = 0;

            while (file_exists(\Yii::getAlias('@imagesDir/'.$field['directory'].'/' . $imageName))) {
                $i++;
                $imageName = $image->baseName . '_' . $i . '.' . $image->extension;
            }

            $imgDir = \Yii::getAlias('@imagesDir/'.$field['directory'].'/');

            if (!is_dir($imgDir)) {
                mkdir($imgDir, 0755, true);
            }

            $image->saveAs($imgDir . $imageName);

            $images[] = $imageName;

        }
        return $images;
    }




    public function getImageFile($image = 'image') {
        return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image;
    }

    public function getImageUrl($image = 'image') {
        return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image;
    }

    public function getImagesConfig() {
        $op = [];
        if ($this->owner->images) {
            foreach ($this->owner->images as $image) {
                $op[] = [
                    'caption' => $image->owner->image,
                    'url' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $image->product_image_id]),
                    'key' => $image->primaryKey,
                    'extra' => [
                        'id' => $image->primaryKey,
                    ],
                ];
            }
        }
        return $op;
    }

    public function getImagesHTML() {
        $op = [];
        if ($this->images) {
            foreach ($this->images as $image) {
                $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb');
            }
        }
        return $op;
    }







}