<?php

namespace App\Application\Admin;

use App\Domain\Media\ImageSlot;
use App\Domain\Media\ImageSlotRepository;
use App\Framework\Attributes\Route;
use App\Framework\Auth\Auth;
use App\Framework\Http\Method;
use App\Framework\Http\Request;
use App\Framework\Meta\MetaData;
use App\Framework\Router\Result\ViewResult;

class ShowImageSlots
{
    public function __construct(
        private ImageSlotRepository $imageSlotRepository,
    ) {}

    #[Auth]
    #[Route('/admin/imageslots')]
    public function show()
    {
        $slots = $this->imageSlotRepository->getSlots();

        /** @var ImageSlot $slot */
        foreach($slots as $slot) {
            #echo $slot->slotName . '<br/>';

            if($slot->image !== null) {
            #    echo $slot->image->filename . '<br/>';
            }

            $slotName = $slot->slotName;

        }

        return new ViewResult('imageslots', new MetaData('Image Slots', 'Image Slots'), [
            'slotName' => $slotName,
            'slots' => $slots,
        ]);
    }

    #[Auth]
    #[Route('/admin/imageslots/{slotName}', method: Method::POST)]
    public function update(string $slotName): ViewResult
    {
        $slot = $this->imageSlotRepository->findBySlotName(urldecode($slotName));

        $slotName = $slot->slotName;

        #echo "<input type='text' value='$slotName' />";

        return new ViewResult('imageslot', new MetaData('Image Slot', 'Image Slots'), [
            'slotName' => $slotName,
            'id' => $slot->id,
        ]);
    }

    #[Auth]
    #[Route('/admin/imageslots/create', method: Method::POST)]
    public function create(Request $request)
    {
        $name = $request->parsedBody->get('slotName');

        $slot = new ImageSlot(0, $name, '');

        $this->imageSlotRepository->save($slot);

        debug($name);
    }

    #[Auth]
    #[Route('/admin/imageslots/edit/{id}', method: Method::PUT)]
    public function edit(Request $request, string $id)
    {
        $name = $request->parsedBody->get('slotName');

        $slot = $this->imageSlotRepository->findById((int)$id);


        $slot = new ImageSlot($slot->id, $name, $slot->imageId);

        $this->imageSlotRepository->save($slot);
    }
}
