<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use App\Foto_vazba;
use Image;

class Foto extends Model
{
    protected $table = 'g5i_zak_foto';
    protected $tableFotoAccess = 'g5i_zak_foto_relation';
    
    public $timestamps = false;
    protected $fillable =['foto', 'mikro', 'popis', 'name', 'size', 'created', 'type', 'width', 'height'];

    public function __construct($attributes = array()) {
        parent::__construct($attributes);
        if (isset(Auth()->user()->database)) {
            $this->connection = Auth()->user()->database;
        }
    }
   
    //public function fotosAccess() {
        //$this->hasMany($related, $foreignKey, $localKey)
    //    return $this->hasMany('App\Foto_vazba','id','foto_id');
    //}
    
    public function FotoSave($file,$fotoAccessArray=array()){
            $pathImage = 'public/upload/';
            $pathImageStorage = 'storage/upload/';
            $fileName = $file->getClientOriginalName();
            //$fileSize = $file->getClientSize();
            $fileSize = $file->getSize();
            $fileType =$file->getClientOriginalExtension();
            $file->storeAs($pathImage,$fileName);
            $imageSaved = Storage::get($pathImage.$fileName);
              
            //$img = Image::make($file->getRealPath());
            //$img = Image::make($pathImageStorage.$fileName);
            $img = Image::make($file);
            $imageWidth =  $img->width(); 
            $imageHeight =  $img->height(); 
                
            $imgMikcro = Image::make($file)->resize(100, 150)->save(public_path($pathImageStorage.'micro_'.$fileName));
            //$imgMikcro = Image::make($pathImageStorage.$fileName)->resize(100, 150)->save($pathImageStorage.'micro_'.$fileName);
            $imageMicro = Storage::get($pathImage.'micro_'.$fileName);
             //$imageSaved = file_get_contents('storage/upload/'.$fileName);
                
            $this->foto = $imageSaved;
            $this->width = $imageWidth;
            $this->height = $imageHeight;
            $this->mikro = $imageMicro;
            $this->name = $fileName;
            $this->popis = $fileName;
            $this->size = $fileSize;
            $this->type = $fileType;
            $this->created = date("Y-m-d");
            $this->save();
            
            $fotoAccess = new Foto_vazba();
            $fotoAccess->foto_id = $this->id;
            $fotoAccess->link_type = $fotoAccessArray['link_type'];
            $fotoAccess->link_id=  $fotoAccessArray['link_id'];
            $fotoAccess->save();
                       
            /*
                $fotoAccess = new fotoAccess();
                $fotoAccess->foto_id = $foto->id;
                $fotoAccess->link_type = 'POZ';
                $fotoAccess->link_id=  sprintf("%09u", $insertedIdStr);    
                $fotoAccess->save();
            */    
            return $this->id;
    }   

    /**
     * Uložení fotky pro API
     *
     * @param $file
     * @param $fotoAccessArray
     * @param $database
     * @return void
     */
    public function FotoSaveApi($file, $fotoAccessArray, $database) {
            $pathImage = 'public/upload/';
            $pathImageStorage = 'storage/upload/';
            $fileName = $file->getClientOriginalName();
            $fileSize = $file->getSize();
            $fileType =$file->getClientOriginalExtension();
            $file->storeAs($pathImage,$fileName);
            $imageSaved = Storage::get($pathImage.$fileName);

            $img = Image::make($pathImageStorage.$fileName);
            $imageWidth =  $img->width(); 
            $imageHeight =  $img->height(); 
                
            $imgMikcro = Image::make($pathImageStorage.$fileName)->resize(100, 150)->save($pathImageStorage.'micro_'.$fileName);
            $imageMicro = Storage::get($pathImage.'micro_'.$fileName);
                
            $this->foto = $imageSaved;
            $this->width = $imageWidth;
            $this->height = $imageHeight;
            $this->mikro = $imageMicro;
            $this->name = $fileName;
            $this->popis = $fileName;
            $this->size = $fileSize;
            $this->type = $fileType;
            $this->created = date("Y-m-d");
            $this->save();
            
            $fotoAccess = new Foto_vazba();
            $fotoAccess->setConnection($database);
            $fotoAccess->foto_id = $this->id;
            $fotoAccess->link_type = $fotoAccessArray['link_type'];
            $fotoAccess->link_id=  $fotoAccessArray['link_id'];
            $fotoAccess->save();

            return $this->id;
    }
    
    /*
    public function GetFotos($link_type, $link_id) {
        $fotos = DB::table($this->tableFotoAccess)
            ->leftjoin($this->table , $this->table.'.id', '=', $this->tableFotoAccess.'.foto_id')
            //->select($this->tableFotoAccess.'.*')
            ->where([
                [$this->tableFotoAccess.'.link_type' , '=', $link_type], 
                [$this->tableFotoAccess.'.link_id', '=', $link_id] 
                ])
            //->where($this->tableFotoAccess.'.link_type' , '=', $link_type .' AND '. $this->tableFotoAccess.'.link_id', '=', $link_id)
               ->get();
        
        $fotosArray = array();
               
        foreach ($fotos as $foto) {
            $pathMicro = 'storage/show/micro'.$foto->id.$foto->type;
            Storage::put('public/show/micro'.$foto->id.$foto->type , $foto->mikro);
            
            $pathFoto = 'storage/show/'.$foto->id.$foto->type;
            Storage::put('public/show/'.$foto->id.$foto->type , $foto->foto);
            
            $fotosArray[$foto->id] = array (
                'id' => $foto->id,
                'pathFoto' => $pathFoto,
                'pathMicro' => $pathMicro
            );
            //echo $foto->id;
       }
        return $fotosArray;
    }
    */
    
    public function isImage($file){
        $fileType =$file->getClientOriginalExtension();
        $isImage = stripos("jpeg,png,jpg,gif",$fileType)>0;
        return $isImage;
    }






}
