<?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');
        }
        return Image::factory($file, $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),
                'initialPreview' => $model->{$modelField} ? Html::img($model->{$modelField}) : '',
                'overwriteInitial' => !$multiple,
                'showRemove' => true,
                'showUpload' => true,
                'showCaption' => false,
                'uploadUrl' => $this->uploadUrl,
                'uploadExtraData' => [
                    'fileField' => $modelField,
                    'multiple' => intval($multiple),
                ],
            ],
            'pluginEvents' => [
                "change" => "function() {
                alert('_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);
                }",
            ],
        ]);
    }
}