<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\GalleryImage;
use App\Repositories\ResponseRepository;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

class GalleryImageController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = ['banner_name' => 'required'];
    }

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Gallery Image', 'store'), GalleryImage::create($request->all()), 200);
        } else {
            return $validate;
        }
    }

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Gallery Image', 'show'), $this->findGalleryImage($id), 200);
    }

    public function update(Request $request, $id) {
        $validate = $this->response->validate($request->all(), ['banner_name' => ['required']]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Gallery Image', 'update'), $this->findGalleryImage($id)->update($request->all()), 200);
        } else {
            return $validate;
        }
    }

    public function destroy($id) {
        $size = $this->findGalleryImage($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Gallery Image', 'destroy'), $size->delete(), 200);
        }
        return $this->response->jsonResponse(true, 'Gallery Image Not Exists', [], 201);
    }

    public function findGalleryImage($id) {
        return GalleryImage::find($id);
    }

    public function getAllGalleryImage() {
        return $this->response->jsonResponse(false, $this->response->message('Gallery Image', 'index'), GalleryImage::orderBy('id', 'desc')->get(), 200);
    }

    public function galleryImageSwitch($id) {
        $size = $this->findGalleryImage($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Gallery Image '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 200);
        }
        return $this->response->jsonResponse(true, 'Gallery Image Not Exists', [], 201);
    }

    public function imageUpdateGallery(Request $request) {
        $getBanner = $this->findGalleryImage($request['banner_id']);
        if($request->hasFile('banner_image')) {
            $file = $request->file('banner_image');
            $name = $this->response->generateSlug($getBanner['banner_name']);
            $uploadUrl = $this->response->cloudinaryImage($request->file('banner_image'), 'galleryImage', $name);
            return $this->response->jsonResponse(false, $this->response->message('Gallery Image', 'image'), $getBanner->update(['banner_image' => $uploadUrl]), 201);
        } else {
            return $this->response->jsonResponse(true, 'Image Banner is too high', [], 201);
        }
    }
}
