<?php

namespace common\components\artboximage;

use kartik\file\FileInput;
use Yii;
use yii\base\Component;
use yii\base\ErrorException;
use yii\helpers\Html;
use yii\image\drivers\Image;

//use common\components\artboximage\drivers\Image;

class ArtboxImage extends Component {

    public $driver;

    public $presets = [];

    public $rootPath;
    public $rootUrl;

    public $extensions = [
        'jpg' => 'jpeg',
        'jpeg' => 'jpeg',
        'png' => 'png',
        'gif' => 'gif',
        'bmp' => 'bmp',
    ];

    public $uploadUrl = '/admin/artboxfile/action/upload';

    public function load($file = null, $driver = null) {
        if(empty($file) || !realpath($file)) {
            throw new ErrorException('File name can not be empty and exists');
        }
        try {
            return Image::factory($file, $driver ? $driver : $this->driver);
        } catch (ErrorException $e) {
            return Image::factory(\Yii::getAlias('@storage') . '/no-image.png', $driver ? $driver : $this->driver);
        }
    }

    public function fileinputWidget($model, $modelField, $formField = 'fileUpload', $multiple = false, $imageOnly = true) {
        $options = [
            'multiple' => $multiple,
        ];
        if ($imageOnly) {
            $options['accept'] = 'image/*';
        }
        return FileInput::widget([
            'name' => $formField,
            'options' => $options,
            'pluginOptions' => [
                'allowedFileExtensions' => array_keys($this->extensions),
                // @todo set for multiple
                'initialPreview' => $model->{$modelField} ? Html::img($model->{$modelField}) : '',
                'overwriteInitial' => !$multiple,
                'showRemove' => true,
                'showUpload' => false,
                'uploadUrl' => $this->uploadUrl,
                'uploadExtraData' => [
                    'fileField' => $modelField,
                    'multiple' => intval($multiple),
                ],
            ],
            'pluginEvents' => [
                "change" => "function() { console.log('change'); }",
                "open" => "function() { console.log('open'); }",
                "save" => "function() { console.log('save'); }",
                "upload" => "function() { console.log('upload'); }",
                "uploaded" => "function() { console.log('uploaded'); }",
                "filepreupload" => "function() { console.log('filepreupload'); }",
                "fileuploaded" => "function(event, files, extra) { console.log(event, files, extra); }",
                "fileuploaderror" => "function() { console.log('fileuploaderror'); }",
                "filebatchuploaderror" => "function() { console.log('filebatchuploaderror'); }",
                "filebatchuploadsuccess" => "function() { console.log('filebatchuploadsuccess'); }",
                "filebatchuploadcomplete" => "function() { console.log('filebatchuploadcomplete'); }",
                'filebatchuploadsuccess' => "function(event, files, extra) {
                        console.log(event, files, extra);
                }",
            ],
        ]);
    }
}