<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Validator;
use App\Video;
use App\Aws\FileSystem;

class VideoController extends AdminController
{
     public function __construct() {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/videos";
        $this->breadcrumbs[1]['name'] = "Videos";
    }
    
    public function index() {

        $model = Video::get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["title"] = "Videos";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/videos/index', $data);
    }
    
     public function create() {
        $data["pageHeader"] = false;
        $data["model"] = new Video();
        $data["title"] = "Videos";
        $data["subTitle"] = "Create";
        $data["date"]=date("j F, Y");

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/videos/create', $data);
    }
    
     public function save(Request $request) {

         $input = $request->all();

        $rules = [
            'title' => 'required|max:255',
            //'facilities' => 'required',
            'file' => 'required|max:10000',
        ];
        
      
        $message = [];
        $validator = Validator::make($input, $rules, $message);
       
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            $input["publish_date"]=str_replace(","," ",$input["publish_date"]);
            $input["publish_date"] = date("Y-m-d", strtotime($input["publish_date"]));
            

            unset($input["_token"]);
            
            
           $input["created_at"] = date("Y-m-d H:i:s");


            $model = new Video();
            $id = $model->insertGetId($input);


            if ($request->hasFile('image')) {

                $file = $request->file('image');
                $fileName = rand(111, 999) . time();
                $input['image'] = FileSystem::storeImage($fileName, $file, 'uploads/videos/', 'uploads/videos/thumbnails/', 1800, 800);
                Video::where('id', $id)->update(array("image" => $input['image']));
            }
            if ($request->hasFile('file')) {

                $file = $request->file('file');
                $fileName = rand(111, 999) . time();
                  $extension = $file->getClientOriginalExtension();
                
                $input['file'] = FileSystem::StoreVideo($fileName, $file, 'uploads/videos/');
                Video::where('id', $id)->update(array("file" => $input['file']));
            }
            

            \Session::flash('success', 'Video added successfully!');
            return redirect('admin/videos');
        }
    }
    
     public function edit($id) {
        $data["model"] = Video::find($id);
        $data["pageHeader"] = false;
        $data["title"] = "Videos";
        $data["subTitle"] = "Edit";
        $data["id"] = $id;
        $data["date"]=date("j F, Y");

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/videos/edit', $data);
    }
    
     public function update($id, Request $request) {

        $input = $request->all();
        $validator = Validator::make($request->all(), [
            'title' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            $input["publish_date"]=str_replace(","," ",$input["publish_date"]);
            $input["publish_date"] = date("Y-m-d", strtotime($input["publish_date"]));
                        
            unset($input["_token"]);
           

            if (!isset($input["status"])) {
                $input["status"] = 0;
            }
            
            if ($request->hasFile('image')) {

                $file = $request->file('image');
                $fileName = rand(111, 999) . time();
                $input['image'] = FileSystem::storeImage($fileName, $file, 'uploads/videos/', 'uploads/videos/thumbnails/', 1800, 800);
//                Video::where('id', $id)->update(array("image" => $input['image']));
            }
            if ($request->hasFile('file')) {

                $file = $request->file('file');
                $fileName = rand(111, 999) . time();
                $extension = $file->getClientOriginalExtension();
                
                $input['file'] = FileSystem::StoreVideo($fileName, $file, 'uploads/videos/');
//                Video::where('id', $id)->update(array("file" => $input['file']));
            }

            $input["updated_at"] = date("Y-m-d H:i:s");
            Video::where('id', $id)->update($input);
            \Session::flash('success', 'Video updated successfully!');
            return redirect('admin/videos');
        }
    }
    
    public function show($id) {

        $q = new Video();
        $data['model'] = $q->find($id);
        $data["title"] = "Videos";

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->title;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/videos/show', $data);
    }

     public function delete($id) {


        Video::where('id', $id)->delete();
        \Session::flash('error', 'Video deleted successfully!');
        return redirect('admin/videos');
    }
}
