<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\ContentvideoUpdateRequest;
use App\Http\Requests\ContentvideoDeleteRequest;
use App\Contentvideo;
use App\Contenu;
//use App\Key;
use Exception;

class ContentvideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        /*try {

            $key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();


            //On vérifie si l'id du contenu existe
            if (Contenu::on($key->bd)->where('id', '=', $request->contenu_id)->first()) {

                $contenu = Contenu::on($key->bd)->where('id', '=', $request->contenu_id)->first();

                //Création du contentvideo
                $new_contentmedia = new Contentmedia($key->bd);
                $new_contentmedia->contenu()->associate($contenu);
                $new_contentmedia->url = $request->url;
                $new_contentmedia->statut = "PUBLISHED";
                $new_contentmedia->save();

                return response()->json([
                    'status' => 'success',
                    'contentvideo' => $new_contentmedia
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else {

                return response()->json([
                    'status' => 'error',
                    'message' => 'This contenu_id does not exist.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            }
        } catch (Exception $e) {

            return response()->json([
                'status' => 'error',
                'message' => $e->getTrace()
            ], 404)->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);
        }*/
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ContentvideoUpdateRequest $request, $id)
    {

        try {

            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();

            $contentvideo = Contentvideo::on('tenant')->findOrFail($id);

            if(isset($request->url)){

                $contentvideo->url = $request->url;
            }

            if(isset($request->description_video)){

                $contentvideo->description = $request->description_video;
            }

            $contentvideo->update();

            //On vérifie si le contenu existe
            /*if ((Contenu::on($key->bd)->where('id', '=', $request->id_contenu)->first())) {

                //On vérifie si le statut du content existe
                $statut_array = ['PUBLISHED', 'BLOCKED', 'PENDING', 'SIGNALED'];
                foreach ($statut_array as $statut) {

                    if ($request->statut == $statut) {

                        $contenu = Contenu::on($key->bd)->where('id', '=', $request->id_contenu)->first();

                        //Modification du contentvideo
                        $contentvideo = Contentvideo::on($key->bd)->findOrFail($id);
                        $contentvideo->contenu()->associate($contenu);
                        $contentvideo->description = $request->description_video;
                        $contentvideo->statut = $request->statut;
                        $contentvideo->url = $request->url;
                        $contentvideo->update();

                        return response()->json([
                            'status' => 'success',
                            'contentvideo' => $contentvideo
                        ])->withHeaders([
                            'Accept' => 'application/json',
                            'Content-Type' => 'application/json'
                        ]);
                    }
                }

                return response()->json([
                    'status' => 'error',
                    'message' => 'This statut does not exist.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else {

                return response()->json([
                    'status' => 'error',
                    'message' => 'This contenu id does not exist.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            }*/
            return response()->json([
                'status' => 'success',
                'message' => 'CONTENTVIDEO_MODIFIED_SUCCESSFULLY'
            ])->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);

        } catch (Exception $e) {

            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage()
            ], 404)->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(ContentvideoDeleteRequest $request, $id)
    {

        try {

            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();

            //Obtenir le contentvideo
            $contentvideo = Contentvideo::on('tenant')->findOrFail($id);

            if ($request->mode == "soft") {

                $contentvideo->statut = 'DELETED';
                $contentvideo->update();

                return response()->json([
                    'status' => 'success',
                    'article' => 'SOFT_DELETE_DONE'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else if ($request->mode == "strict") {

                $contentvideo->delete();
                return response()->json([
                    'status' => 'success',
                    'page' => 'STRICT_DELETE_DONE'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else {

                return response()->json([
                    'status' => 'error',
                    'message' => 'This mode does not exist.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            }
        } catch (Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage()
            ], 404)->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);
        }
    }
}
