<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Http\Requests\ThemeStoreRequest;
use App\Http\Requests\ThemeUpdateRequest;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ThemeController extends Controller
{
    //public function __construct()
    //{
     //   $this->middleware('verify_super_admin', ['except' => ['index']]);
    //}

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        try {
            return response()->json([
                'success' => true,
                'data' => DB::table('themes')->get(),
                'message' => 'THEMES_GOT_SUCCESSFULLY'
            ]);
        } catch (Exception $e) {
            return response()->json([
                'success' =>  false,
                'message' => $e->getMessage()
            ], 400);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ThemeStoreRequest $request)
    {
        try {
            $lastId = DB::table('themes')->insertGetId([
                'etablissement_id' => $request->etablissement_id,
                'type' => $request->type,
                'color' => $request->color,
                'backgroundColor' => $request->backgroundColor,
                'police' => $request->police,
                'taillePolice' => $request->taillePolice,
                'alignement' => $request->alignement,
                'position' => $request->position
            ]);

            return response()->json([
                'success' => true,
                'data' => DB::table('themes')
                    ->where('id', $lastId)
                    ->first(),
                'message' => 'THEME_CREATED_SUCCESSFULLY'
            ]);
        } catch (Exception $e) {
            return response()->json([
                'success' =>  false,
                'message' => $e->getMessage()
            ], 400);
        }
    }

    public function store_default(ThemeStoreRequest $request)
    {
        try {
            foreach ($request->themes as $key => $theme) {
                DB::table('themes')->insert($theme);
            }
            return response()->json([
                'success' => true,
                'message' => 'DEFAULT_THEME_CREATED_SUCCESSFULLY'
            ]);
        } catch (Exception $e) {
            return response()->json([
                'success' =>  false,
                'message' => $e->getMessage()
            ], 400);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try {
            return response()->json([
                'success' => true,
                'data' => DB::table('themes')->where('id', $id)->first(),
                'message' => 'THEME_GET_SUCCESSFULLY'
            ]);
        } catch (Exception $e) {
            return response()->json([
                'success' =>  false,
                'message' => $e->getMessage()
            ], 400);
        }
    }


    //show_all
    public function show_all($id)
    {
        try {
            return response()->json([
                'success' => true,
                'data' => DB::table('themes')->where('etablissement_id', $id)->get(),
                'message' => 'ETABLISSEMENT_THEME_GET_SUCCESSFULLY'
            ]);
        } catch (Exception $e) {
            return response()->json([
                'success' =>  false,
                'message' => $e->getMessage()
            ], 400);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ThemeUpdateRequest $request, $id)
    {
        try {
            if ($request->type != null) {
                DB::table('themes')->where('id', $id)->update([
                    'type' => $request->type
                ]);
            }

            if ($request->color != null) {
                DB::table('themes')->where('id', $id)->update([
                    'color' => $request->color
                ]);
            }

            if ($request->backgroundColor != null) {
                DB::table('themes')->where('id', $id)->update([
                    'backgroundColor' => $request->backgroundColor
                ]);
            }
            if ($request->police != null) {
                DB::table('themes')->where('id', $id)->update([
                    'police' => $request->police
                ]);
            }
            if ($request->taillePolice != null) {
                DB::table('themes')->where('id', $id)->update([
                    'taillePolice' => $request->taillePolice
                ]);
            }
            if ($request->alignement != null) {
                DB::table('themes')->where('id', $id)->update([
                    'alignement' => $request->alignement
                ]);
            }
            if ($request->position != null) {
                DB::table('themes')->where('id', $id)->update([
                    'position' => $request->position
                ]);
            }

            return response()->json([
                'success' => true,
                'data' => DB::table('themes')->where('id', $id)->first(),
                'message' => 'THEME_UPDATED_SUCCESSFULLY'
            ]);
        } catch (Exception $e) {
            return response()->json([
                'success' =>  false,
                'message' => $e->getMessage()
            ], 400);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            DB::table('themes')->where('id', $id)->delete();

            return response()->json([
                'success' => true,
                'message' => 'THEME_DELETED_SUCCESSFULLY'
            ]);
        } catch (Exception $e) {
            return response()->json([
                'success' =>  false,
                'message' => $e->getMessage()
            ], 400);
        }
    }
}
