<?php

namespace App\Repositories;

use App\Models\File;
use Request;
use Response;
use Storage;
use Util;
use Validator;


class FileRepository extends BaseRepository {


    protected $rules;
    protected $messages;

    protected $disk;
    protected $dir;
    protected $user_id;
    protected $identifier;

    public function __construct(File $model){

        $this->model = $model;

        $this->rules = [
            'file' => 'required|mimes:pdf,jpeg,png|max:32768'
        ];
        $this->messages = [
            'file.mimes' => 'Datei ist kein PDF Format',
            'file.required' => 'PDF-Datei wird benötigt'
        ];
    }

    public function _set($name, $value){
        $this->{$name} = $value;
    }

    public function uploadFile( $form_data )
    {

        $validator = Validator::make($form_data, $this->rules, $this->messages);

        if ($validator->fails()) {
            return Response::json([
                'error' => true,
                'message' => $validator->messages()->first(),
                'code' => 400
            ], 400);
        }
        $file = $form_data['file'];

        $originalName = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $mine = $file->getClientMimeType();
        $size = $file->getSize();


        $originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - strlen($extension) - 1);
        $filename = Util::sanitize($originalNameWithoutExt, true, false, true);
        $allowed_filename = uniqid() . '_' . $filename.".".$extension;

        //$dir = $this->model->getInvoiceStorageAttDir();

        if(!Storage::disk($this->disk)->exists( $this->dir )){
            Storage::disk($this->disk)->makeDirectory($this->dir); //creates directory
        }
        Storage::disk($this->disk)->put($this->dir.$allowed_filename, file_get_contents($file->getRealPath()));

        File::create([
            'user_id' => $this->user_id,
            'identifier' => $this->identifier,
            'filename' => $allowed_filename,
            'dir' => $this->dir,
            'original_name' => $originalName,
            'ext' => $extension,
            'mine' => $mine,
            'size' => $size
        ]);

        return Response::json([
            'error' => false,
            'filename' => $allowed_filename,
            'filedata' => 'pdf',
            'redirect' => route('wizard_register'),
            'code'  => 200
        ], 200);
    }

   /* public function createFile(Request $request)
    {
        $locale = \App::getLocale();
        $data = [
            'step' => 2,
            'locale' => $locale,
        ];
        $rules = array(
            'network_name' => 'required|max:255',
            'input_file_now' => 'required|mimes:png,pdf,jpg,jpeg|max:30000'
        );
        $validator = Validator::make(Request::all(), $rules);

        if ($validator->fails()) {
            // get the error messages from the validator
            $messages = $validator->messages();
            // redirect our user back to the form with the errors from the validator
            return view('user.register_complete', $data)->withErrors($validator);

        } else {
            $user = \Auth::user();
            if(!$user->account->network_name){
                $file = request()->file('input_file_now');
                //$ext = $file->guessClientExtension();
                //$file->storeAs('user/' . $user->id .'/verification');
                $data = $file->store('user/' . $user->id .'/verification');
                $account = $user->account;
                $account->network_name = Input::get('network_name');
                $account->network_verification = basename($data);
                $account->save();

                $user->role_id = 2; //set as User by default!
                $user->save();


            }

            return redirect('complete_register');
            //return view('user.register_complete', $data);
        }
    }*/


}