<?php

/**
 * Class Item
 * 
 * Represents a portfolio or service item with properties such as image, description, 
 * external URL, buy link, and methods to render them as HTML blocks.
 */
class Item
{
    /** @var string Category of the item */
    private $cat;
    /** @var string Unique ID of the item */
    private $id;
    /** @var string Image path for display */
    private $img;
    /** @var string Name of the item */
    private $name;
    /** @var string Description of the item */
    private $desc;
    /** @var string External URL for more details */
    private $url;
    /** @var string Text for URL link */
    private $urlText;
    /** @var string Tagline or short description */
    private $tagLine;
    /** @var string Buy URL */
    private $buy;
    /** @var string Text for the buy link */
    private $buyText;
    /** @var string Title for the buy link */
    private $buyTitle;
    /** @var string Icon name for services */
    private $icon;
    /** @var string Formatted name of category */
    private $catName;

    /**
     * Constructor for Item class.
     * 
     * @param string $cat Category of the item.
     * @param string $id Unique ID of the item.
     */
    public function __construct($cat, $id)
    {
        $this->cat = $cat;
        $this->id = $id;
        $this->img = "assets/img/portfolio/$cat-$id.jpg";
        $this->catName = ucfirst($this->cat);
        $this->name = ucfirst($this->cat) . " " . $this->id;
        $this->tagLine = "More detailed information about {$this->name} will be available soon";
        $this->urlText = "Learn More";
        $this->buyText = "Get it Now!";
        $this->buyTitle = "Buy Link";
    }

    /**
     * Creates a URL link HTML.
     * 
     * @return string HTML anchor tag.
     */
    private function mkUrlLink()
    {
        $urlLink = "<strong>URL</strong>: <a href='{$this->url}'>$this->urlText</a>";
        return $urlLink;
    }

    /**
     * Creates a Buy link HTML.
     * 
     * @return string HTML anchor tag.
     */
    private function mkBuyLink()
    {
        $buyLink = "<strong>$this->buyTitle</strong>: <a href='{$this->buy}'>$this->buyText</a>";
        return $buyLink;
    }

    /**
     * Renders the detailed view of the item in HTML format.
     * 
     * @return void
     */
    public function showDetails()
    {
        $url = $this->mkUrlLink();
        $buy = $this->mkBuyLink();

        echo <<<RENDER
<!-- Portfolio Details Section -->
<section id="portfolio-details" class="portfolio-details section">

    <div class="container" data-aos="fade-up" data-aos-delay="100">

        <div class="row gy-4">

            <div class="col-lg-8">
                <div class="portfolio-details-slider swiper init-swiper">

                    <script type="application/json" class="swiper-config">
                        {
                        "loop": true,
                        "speed": 600,
                        "autoplay": {
                            "delay": 5000
                        },
                        "slidesPerView": "auto",
                        "pagination": {
                            "el": ".swiper-pagination",
                            "type": "bullets",
                            "clickable": true
                        }
                        }
                    </script>

                    <div class="swiper-wrapper align-items-center">

                        <div class="swiper-slide">
                            <img src="{$this->img}" alt="">
                        </div>

                    </div>
                    <div class="swiper-pagination"></div>
                </div>
            </div>

            <div class="col-lg-4">
                <div class="portfolio-info" data-aos="fade-up"
                    data-aos-delay="200">
                    <h3>$this->name</h3>
                    <ul>
                        <li><strong>Category</strong>: $this->catName</li>
                        <li>$url</li>
                        <li>$buy</li>
                    </ul>
                </div>
                <div class="portfolio-description" data-aos="fade-up"
                    data-aos-delay="300">
                    <h5>{$this->tagLine}</h5>
                    <p>
                        {$this->desc}
                    </p>
                </div>
            </div>

        </div>

    </div>

</section><!-- /Portfolio Details Section -->
RENDER;
    }

    /**
     * Renders the portfolio card view of the item in HTML format.
     * 
     * @return void
     */
    public function show()
    {
        $name = $this->name ?: ucfirst($this->cat) . " " . $this->id;
        $tagLine = $this->tagLine ?: "More detailed information about $name will be available soon";
        $card = "[<a href='portfolio.php?cat={$this->cat}&id={$this->id}'>Details</a>]";
        $link = $this->url ? "[<a href='{$this->url}' target='_blank'>Even More Details</a>]" : "";
        echo <<<RENDER
<div
    class="col-lg-4 col-md-6 portfolio-item isotope-item filter-{$this->cat}">
    <div class="portfolio-content h-100">
    <img src="{$this->img}" class="img-fluid"
        alt="">
    <div class="portfolio-info">
        <h4>$name</h4>
        <p>$tagLine</p>
        <a href="{$this->img}" title="$name: $tagLine $card $link"
        data-gallery="portfolio-gallery-{$this->cat}"
        class="glightbox preview-link"><i
            class="bi bi-zoom-in"></i></a>
        <a href="portfolio.php?cat={$this->cat}&id={$this->id}"
        title="More Details" class="details-link"><i
            class="bi bi-link-45deg"></i></a>
    </div>
    </div>
</div><!-- End Portfolio Item -->
RENDER;
    }

    /**
     * Renders the item as a service card.
     * 
     * @return void
     */
    public function showService()
    {
        echo <<<RENDER
<div class="col-lg-4 col-md-6 service-item d-flex" data-aos="fade-up"
    data-aos-delay="100">
    <div class="icon flex-shrink-0"><i class="bi bi-{$this->icon}"></i>
    </div>
    <div>
        <h4 class="title"><a href="service.php"
            class="stretched-link">{$this->name}</a></h4>
        <p class="description">
        {$this->desc}
        </p>
    </div>
</div><!-- End Service Item -->
RENDER;
    }

    /**
     * Set category name.
     * 
     * @param string $catName
     * @return self
     */
    public function setCatName($catName): self
    {
        $this->catName = $catName;
        return $this;
    }

    /**
     * Get image path.
     * 
     * @return string
     */
    public function getImg()
    {
        return $this->img;
    }

    /**
     * Get name.
     * 
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set name.
     * 
     * @param string $name
     * @return self
     */
    public function setName($name): self
    {
        $this->name = $name;
        return $this;
    }

    /**
     * Get description.
     * 
     * @return string
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * Set description.
     * 
     * @param string $desc
     * @return self
     */
    public function setDesc($desc): self
    {
        $this->desc = $desc;
        return $this;
    }

    /**
     * Get URL.
     * 
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set URL.
     * 
     * @param string $url
     * @return self
     */
    public function setUrl($url): self
    {
        $this->url = $url;
        return $this;
    }

    /**
     * Set tagline.
     * 
     * @param string $tagLine
     * @return self
     */
    public function setTagLine($tagLine): self
    {
        $this->tagLine = $tagLine;
        return $this;
    }

    /**
     * Get buy URL.
     * 
     * @param string $buy
     * @return self
     */
    public function getBuy()
    {
        return $this->buy;
    }

    /**
     * Set buy URL.
     * 
     * @param string $buy
     * @return self
     */
    public function setBuy($buy): self
    {
        $this->buy = $buy;
        return $this;
    }

    /**
     * Set icon.
     * 
     * @param string $icon
     * @return self
     */
    public function setIcon($icon): self
    {
        $this->icon = $icon;
        return $this;
    }

    /**
     * Set text for URL link.
     * 
     * @param string $urlText
     * @return self
     */
    public function setUrlText($urlText): self
    {
        $this->urlText = $urlText;
        return $this;
    }

    /**
     * Set text for buy link.
     * 
     * @param string $buyText
     * @return self
     */
    public function setBuyText($buyText): self
    {
        $this->buyText = $buyText;
        return $this;
    }

    /**
     * Set title for buy link.
     * 
     * @param string $buyTitle
     * @return self
     */
    public function setBuyTitle($buyTitle): self
    {
        $this->buyTitle = $buyTitle;
        return $this;
    }
}
