<?php
  
namespace App\Http\Controllers;
  
use App\Video;
use Illuminate\Http\Request;
use File;
  
class VideoController extends Controller
{
    
    protected $rules = [
        'title' => ['required']
    ];

    public function index()
    {
        $videos = Video::orderBy('position')->get(); //->paginate(50);
  
        return view('videos.index',compact('videos'))
            ->with('i', (request()->input('video', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('videos.create');
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;

        if(request()->file)
        {

            $file = request()->file;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/videos"))
                File::makeDirectory(public_path()."/files/videos");
            
            $path = public_path('files/videos');

            request()->file->move($path, $filename);

            $input["file"] = $filename;

        }

        if(request()->image)
        {

            $file = request()->image;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/videos"))
                File::makeDirectory(public_path()."/files/videos");
            
            $path = public_path('files/videos');

            request()->image->move($path, $filename);

            $input["image"] = $filename;

        }

        $final_date = null;
        if ($input["date"] != '')
        {
            list($dt, $time) = explode(" ", $input["date"]);
            list($day, $month, $year) = explode("/", $dt);
            $final_date = $year . "-" . $month . "-" . $day . " " . $time;
        }
        $input["date"] = $final_date;

        Video::create($input);
   
        return redirect()->route('videos.index')
                        ->with('success','Video created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Video  $video
     * @return \Illuminate\Http\Response
     */
    public function show(Video $video)
    {
        return view('videos.show',compact('video'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Video  $video
     * @return \Illuminate\Http\Response
     */
    public function edit(Video $video)
    {
        if ($video->date != null)
        {
            list($dt, $time) = explode(" ", $video->date);
            list($year, $month, $day) = explode("-", $dt);
            $final_date = $day . "/" . $month . "/" . $year . " " . $time;
            $video->date = $final_date;
        }
        return view('videos.edit',compact('video'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Video  $video
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Video $video)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;

        if(request()->file)
        {

            $file = request()->file;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/videos"))
                File::makeDirectory(public_path()."/files/videos");
            
            $path = public_path('files/videos');

            request()->file->move($path, $filename);

            $input["file"] = $filename;

        }

        if(request()->image)
        {

            $file = request()->image;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/videos"))
                File::makeDirectory(public_path()."/files/videos");
            
            $path = public_path('files/videos');

            request()->image->move($path, $filename);

            $input["image"] = $filename;

        }

        $final_date = null;
        if ($input["date"] != '')
        {
            list($dt, $time) = explode(" ", $input["date"]);
            list($day, $month, $year) = explode("/", $dt);
            $final_date = $year . "-" . $month . "-" . $day . " " . $time;
        }
        $input["date"] = $final_date;
        
        $video->update($input);
  
        return redirect()->route('videos.index')
                        ->with('success','Video updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Video  $video
     * @return \Illuminate\Http\Response
     */
    public function destroy(Video $video)
    {
        $video->delete();
  
        return redirect()->route('videos.index')
                        ->with('success','Video deleted successfully');
    }
}