<?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;

class Foto_vazba extends Model
{
    protected $table = 'g5i_zak_foto_relation';
    
    public $timestamps = false;
    protected $fillable =['foto_id', 'link_type', 'link_id'];

    public function __construct($attributes = array()) {
        parent::__construct($attributes);
        if (isset(Auth()->user()->database)) {
            $this->connection = Auth()->user()->database;
        }
    }
    
    public function foto() {
        //$this->belongsTo($related, $foreignKey, $ownerKey, $relation)
        return $this->belongsTo('App\Foto', 'foto_id', 'id' );
    }
    
    
    /**
     * Načítání evidovaných fotek k objektu 
     * @param  $link_type - ZAK,POO
     * @param  $link_id - id_zakázky
     * @param  $delete - určuje zda před rozbalením souborů smazat úložiště 
     * @return Array s odkazy souborů 
     */
    public function GetFotosVazba($link_type, $link_id, $delete = false) {
        $fotosVazba = $this->where('link_type',$link_type)->where('link_id',$link_id)->get();
        
        $fotosArray = array();
        
        $id_user = Auth::user()->id;
        if (isset(Auth()->user()->master_id)) {
            $id_user = Auth()->user()->master_id;
        }

        //Výmaz původních souborů    
        if($delete){
            $filesStore = Storage::files('public/show/'.$id_user);
            foreach ($filesStore as $fileStore){
                Storage::delete($fileStore); 
            }
        }
        
        foreach ($fotosVazba as $fotoVazba) {
            $pathMicro = 'storage/show/'.$id_user.'/micro'.$fotoVazba->foto_id.".".$fotoVazba->foto->type;
            Storage::put('public/show/'.$id_user.'/micro'.$fotoVazba->foto_id.".".$fotoVazba->foto->type , $fotoVazba->foto->mikro);
            
            $pathFoto = 'storage/show/'.$id_user.'/'.$fotoVazba->foto_id.".".$fotoVazba->foto->type;
            Storage::put('public/show/'.$id_user.'/'.$fotoVazba->foto_id.".".$fotoVazba->foto->type , $fotoVazba->foto->foto);
            
            $fotosArray[] = array (
                'id' => $fotoVazba->foto_id,
                'pathFoto' => asset($pathFoto),
                'pathMicro' => asset($pathMicro)
            );
            //echo $foto->id;
       }
        return $fotosArray;
    }
    
    /**
     * Načítání evidovaných fotek k objektu 
     * @param  $link_type - ZAK,POO
     * @param  $link_id - id_zakázky
     * @param  $delete - určuje zda před rozbalením souborů smazat úložiště 
     * @return Array s odkazy souborů 
     */
    public function GetFotosVazbaApi($link_type, $link_id, $user, $delete = false) {
        $fotosVazba = $this->where('link_type',$link_type)->where('link_id',$link_id)->get();
        
        $fotosArray = array();
        
        $id_user = $user->master_id;

        //Výmaz původních souborů    
        if($delete){
            $filesStore = Storage::files('public/show/'.$id_user);
            foreach ($filesStore as $fileStore){
                Storage::delete($fileStore); 
            }
        }
        
        foreach ($fotosVazba as $fotoVazba) {
            $pathMicro = 'storage/show/'.$id_user.'/micro'.$fotoVazba->foto_id.".".$fotoVazba->foto->type;
            Storage::put('public/show/'.$id_user.'/micro'.$fotoVazba->foto_id.".".$fotoVazba->foto->type , $fotoVazba->foto->mikro);
            
            $pathFoto = 'storage/show/'.$id_user.'/'.$fotoVazba->foto_id.".".$fotoVazba->foto->type;
            Storage::put('public/show/'.$id_user.'/'.$fotoVazba->foto_id.".".$fotoVazba->foto->type , $fotoVazba->foto->foto);
            
            $fotosArray[] = array (
                    'id' => $fotoVazba->foto_id,
                    'path' => $pathMicro
                );
            //echo $foto->id;
        }
        return $fotosArray;
    }
}
