<?php

   namespace App\Http\Controllers;

   use Gate;
   use Request;
   use File;
   use App\Device;
   use App\Component;
   use App\Http\Requests;
   use App\DeviceTemplate;
   use App\StatCollection;
   use App\Http\Controllers\Controller;
   use Illuminate\Http\Request as HttpRequest;
   use App\Http\Requests\ComponentRequest;

   class ComponentsController extends Controller
   {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
         if (Gate::denies('admin')) {
            abort(403, 'You cannot edit components.');
         }
         return view('components.index');
      }

      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function readings()
      {
         if (Gate::denies('viewer')) {
            abort(403, 'You cannot view components.');
         }
         return view('tabs');
      }


      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create()
      {
         if (Gate::denies('admin')) {
            abort(403, 'You cannot edit components.');
         }
         return view('components.create');
      }

      /**
       * Store a newly created resource in storage.
       *
       * @param ComponentRequest $request
       *
       * @return \Illuminate\Http\Response
       */
      public function store(ComponentRequest $request)
      {
         if (Gate::denies('admin')) {
            abort(403, 'You cannot edit components.');
         }

         $vue_name = str_replace(
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ' ', '_', '/'],
            ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', '', '', '', ''],
            $request->name);

         $request['vue_name'] = $vue_name;

         $component = Component::create($request->all());
         if($request['tenant_list'] === NULL || $request['tenant_list'] === '' ) {
            $request['tenant_list'] = [];
         }
         $component->tenants()->sync($request['tenant_list']);

         return redirect('components');
      }

      /**
       * Show the form for editing the specified resource.
       *
       * @param Component $component
       *
       * @return \Illuminate\Http\Response
       *
       */
      public function edit(Component $component)
      {
         if (Gate::denies('admin')) {
            abort(403, 'You cannot edit component templates.');
         }
         return view('components.edit', compact('component'));
      }

      /**
       * Update the specified resource in storage.
       *
       * @param ComponentRequest $request
       * @param Component $component
       *
       * @return \Illuminate\Http\Response
       */
      public function update(ComponentRequest $request, Component $component)
      {
         if (Gate::denies('admin')) {
            abort(403, 'You cannot edit components.');
         }

         $vue_name = str_replace(
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ' ', '_', '/'],
            ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', '', '', '', ''],
            $request->name);

         $request['vue_name'] = $vue_name;

         $component->update($request->all());
         if($request['tenant_list'] === NULL || $request['tenant_list'] === '' ) {
            $request['tenant_list'] = [];
         }
         $component->tenants()->sync($request['tenant_list']);
         return redirect('components');
      }


      /**
       * Remove the specified resource from storage.
       *
       * @param Component $component
       *
       * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
       * @throws \Exception
       */
      public function destroy(Component $component)
      {
         if (Gate::denies('admin')) {
            abort(403, 'You cannot delete components.');
         }

         $component->tenants()->detach($component->tenants->lists('id')->all());

         $component->delete();

         return redirect('/components');
      }
   }