<?php

namespace IqContent\LaravelFilemanager\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class IQContentFile extends Model
{

    use Sluggable;

    //use the connection to sec. Datebase sterntours
    protected $connection = 'mysql_stern';

    protected $table = 'i_q_content_files';

    protected $fillable = [
        'folder_id', 'name', 'identifier', 'ext', 'mine', 'size', 'dimensions', 'content', 'color', 'pos', 'active'
    ];

    public function folder() {
        return $this->belongsTo('IqContent\LaravelFilemanager\Models\IQContentFolder', 'folder_id');
    }

    public function file_tags() {
        return $this->hasMany('IqContent\LaravelFilemanager\Models\IQContentFileTag', 'file_id');
    }

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }

    public function setIdentifierAttribute( $value ) {
        if(!isset($value) || $value == ""){
            $this->attributes['identifier'] = Str::slug(pre_slug($this->name), '-');
        }else{
            $this->attributes['identifier']   = Str::slug(pre_slug($value), '-');
        }
    }

    public function formatBytes($precision = 2)
    {
    	$size = $this->size;

        if ($size > 0) {
            $size = (int) $size;
            $base = log($size) / log(1024);
            $suffixes = array(' KB', ' MB', ' GB', ' TB');

            return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
        } else {
            return $size;
        }
    }

    public function hasTags()
    {
        if($this->file_tags()->count()){
            return true;
        }
        return false;
    }

   /* public function hasThumb(){
       if(\Storage::disk('local')->exists('thumb/'.$this->filename) || \Storage::disk('local')->exists('thumb/'.$this->filename.".jpg")){
            return true;
       }
       return false;
    }

      public function hasBig(){
       if(\Storage::disk('local')->exists('big/'.$this->filename) || \Storage::disk('local')->exists('big/'.$this->filename.".jpg")){
            return true;
       }
       return false;
    }*/

}
