<?php

namespace App\Http\Controllers;


use App\Models\MobilResponsiveBanner;
use App\Repositories\ResponseRepository;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class MobilResponsiveBannerController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = ['mobile_banner_name' => 'required',];
    }
     // All Data Get Function
     public function getAllMobilResponsiveBanner() {
        return $this->response->jsonResponse(false,"Banner Details Fetched Successfully", MobilResponsiveBanner::orderBy('id', 'desc')->get(), 100);
    }
    // Data Store Function
    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'store'), MobilResponsiveBanner::create($request->all()), 101);
        } else {
            return $validate;
        }
    }
    // Particular id data get function 
    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Banner', 'show'), $this->findBanner($id), 102);
    }
    // Update Function 
    public function updateMobilResponsiveBanner(Request $request) {
        $validate = $this->response->validate($request->all(), ['mobile_banner_name' => ['required']]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'update'), $this->findBanner($request->id)->update($request->all()), 103);
        } else {
            return $validate;
        }
    }
    //  Delete Function
    public function destroy($id) {
        $size = $this->findBanner($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'destroy'), $size->delete(), 104);
        }
        return $this->response->jsonResponse(true, 'Banner Not Exists', [], 105);
    }
    // Id Find Function
    public function findBanner($id) {
        return MobilResponsiveBanner::find($id);
    }
    // Active Deactive Function
    public function mobilBannerActiveDeactiveData($id) {
        $size = $this->findBanner($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Banner '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 106);
        }
        return $this->response->jsonResponse(true, 'HomeBanner Not Exists', [], 107);
    }
    // Image Update Function
    public function imageUpdateMobilResponsiveBanner(Request $request) {
        $getBanner = $this->findBanner($request['banner_id']);
        if($request->hasFile('mobile_banner_image')) {
            $file = $request->file('mobile_banner_image');
            $name = $this->response->generateSlug($getBanner['mobile_banner_name']);
            $uploadUrl = $this->response->cloudinaryImage($request->file('mobile_banner_image'), 'Banner', $name);
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'image'), $getBanner->update(['mobile_banner_image' => $uploadUrl]), 108);
        } else {
            return $this->response->jsonResponse(true, 'Image Banner is too high', [], 109);
        }
    }
    //  Search Function
    public function searchMobileBannerData($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), [], 110);
        }
        return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), MobilResponsiveBanner::where('mobile_banner_name', 'LIKE', '%'.$search.'%')->get(), 111);
    }
}



