<?php

namespace Modules\Slider\Http\Controllers;

use App\Functions\Upload;
use App\Http\Controllers\BasicController;
use Illuminate\Http\Request;
use Modules\Category\Entities\Model as Category;
use Modules\Product\Entities\Model as Product;
use Modules\Slider\Entities\Model;

class Controller extends BasicController
{
    public function index(Request $request)
    {
        $Models = Model::with('linkable')->get();

        return view('slider::index', compact('Models'));
    }

    public function create()
    {
        $Categories = $this->renderCategoryOptions();
        $Products = Product::all();

        return view('slider::create', compact('Categories', 'Products'));
    }

    public function store(Request $request)
    {

        $linkableType = null;
        $linkableId = null;
        $validator = \Validator::make($request->all(), [
            'type' => 'required|in:product,category',
            'category' => 'required_if:type,category',
            'product' => 'required_if:type,product',
            'file' => 'required|file|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput();
        }

        if ($request->type == 'product') {
            $linkableType = Product::class;
            $linkableId = $request->product;
        } elseif ($request->type == 'category') {
            $linkableType = Category::class;
            $linkableId = $request->category;
        } else {
            alert()->error(__('trans.invalidType'));

            return redirect()->back();
        }

        if ($request->hasFile('file')) {
            $filePath = Upload::UploadFile($request->file, 'Sliders');
        } else {
            alert()->error(__('trans.fileRequired'));

            return redirect()->back();
        }

        $slider = new Model;
        $slider->title_ar = $request->title_ar;
        $slider->title_en = $request->title_en;
        $slider->file = $filePath;
        $slider->linkable_type = $linkableType;
        $slider->linkable_id = $linkableId;
        $slider->status = $request->get('status', 1);
        $slider->save();

        alert()->success(__('trans.addedSuccessfully'));

        return redirect()->back();
    }

    public function show($id)
    {
        $Model = Model::where('id', $id)->firstorfail();

        return view('slider::show', compact('Model'));
    }

    public function edit($id)
    {
        $Model = Model::where('id', $id)->firstorfail();
        $Categories = $this->renderCategoryOptions();
        $product_id = -1;
        if ($Model->linkable_type == Category::class) {
            $Categories = $this->renderCategoryOptions([$Model->linkable_id]);
        } elseif ($Model->linkable_type == Product::class) {
            $product_id = $Model->linkable_id;
        }

        $Products = Product::all();

        return view('slider::edit', compact('Model', 'Categories', 'Products', 'product_id'));
    }

    public function update(Request $request, $id)
    {
        $linkableType = null;
        $linkableId = null;

        if ($request->type == 'product') {
            $linkableType = Product::class;
            $linkableId = $request->product;
        } elseif ($request->type == 'category') {
            $linkableType = Category::class;
            $linkableId = $request->category;
        } else {
            alert()->error(__('trans.invalidType'));

            return redirect()->back();
        }

        $slider = Model::find($id);

        if ($request->hasFile('file')) {
            $filePath = Upload::UploadFile($request->file, 'Sliders');
            $slider->file = $filePath;
        }

        $slider->title_ar = $request->title_ar;
        $slider->title_en = $request->title_en;
        $slider->linkable_type = $linkableType;
        $slider->linkable_id = $linkableId;
        $slider->status = $request->get('status', 1);
        $slider->save();

        alert()->success(__('trans.updatedSuccessfully'));

        return redirect()->back();
    }

    public function destroy($id)
    {
        $Model = Model::where('id', $id)->delete();
    }

    public function renderCategoryOptions($selectedCategoryIds = [], $path = '', $Categories = null)
    {
        if ($Categories === null) {
            $Categories = Category::whereNull('parent_id')->orWhere('parent_id', 0)->get();
        }

        $html = '';
        foreach ($Categories as $Category) {
            $currentPath = $path ? $path.' - '.$Category->title() : $Category->title();

            if ($Category->children && $Category->children->isNotEmpty()) {
                $html .= $this->renderCategoryOptions($selectedCategoryIds, $currentPath, $Category->children);
            } else {
                $isSelected = in_array($Category->id, $selectedCategoryIds) ? 'selected' : '';
                $html .= '<option value="'.$Category->id.'" '.$isSelected.'>'.$currentPath.'</option>';
            }
        }

        return $html;
    }
}
