<?php

/**
 * Class ItemDAO
 * 
 * Mock database handler for portfolio items and services. 
 * Generates and manages categories like apps, books, pedagogy tools, WAD-specific content, and services.
 */
class ItemDAO
{
    /**
     * @var array Mapping of category short names to display names.
     */
    static $cats = [
        "app" => "Web Apps",
        "books" => "Books",
        "pedagogy" => "Pedagogical Tools",
        "wad" => "For WAD1 Students"
    ];

    /** @var array Portfolio category: Apps */
    static $app;

    /** @var array Portfolio category: Books */
    static $books;

    /** @var array Portfolio category: Pedagogical Tools */
    static $pedagogy;

    /** @var array Portfolio category: WAD1 Students */
    static $wad;

    /** @var array Services list */
    static $services;

    /**
     * Constructor to initialize mock database.
     */
    function __construct()
    {
        self::mockDB();
    }

    /**
     * Initialize the mock database with hardcoded data.
     * 
     * @return void
     */
    static function mockDB()
    {
        self::$app = [];
        self::$app[] = [
            'name' => "EZ PayPal",
            "url" => "//www.thulasidas.com/packages/ezpaypal/",
            "tagLine" => "The easiest way to sell your digital products online.",
            "desc" => "Looking to sell an application, PHP package, photograph, PDF book, or any other downloadable item? Overwhelmed by the complexities of setting up a shopping cart system like WooCommerce? EZ PayPal offers a simple, effective solution tailored for selling digital goods. Whether you need a standalone application or a WordPress plugin, EZ PayPal has you covered.",
            "buy" => "//buy.thulasidas.com/ezpaypal",
        ];
        self::$app[] = [
            'name' => "Theme Tweaker",
            "url" => "//www.thulasidas.com/plugins/theme-tweaker/",
            "tagLine" => "A WordPress plugin to customize the colors of your favorite theme.",
            "desc" => "Theme Tweaker showcases the current colors of your theme and offers a color picker for easy customization. You can also apply bulk changes, such as inverting all colors or switching to grayscale, with just a few clicks.",
            "buy" => "//buy.thulasidas.com/theme-tweaker",
        ];
        self::$app[] = [
            'name' => "Ads EZ",
            "url" => "//www.thulasidas.com/packages/ads-ez/",
            "tagLine" => "A feature-rich personal ad server designed for your needs.",
            "desc" => "Ads EZ is an intelligent solution for serving ads across multiple websites. It functions as both a standalone web application and a WordPress plugin. With Ads EZ, you can centralize your banner ads in one location, easily place them on your blog pages using shortcodes, or generate invocation codes to display them on other websites or blogs.",
            "buy" => "//buy.thulasidas.com/ads-ez",
        ];

        self::$books = [];
        self::$books[] = [
            'name' => "The Unreal Universe",
            "url" => "//TheUnrealUniverse.com",
            "tagLine" => "Exploring the true nature of our reality.",
            "desc" => "Are space and time merely constructs of our perception, or are they the fundamental conditions in which we exist? What makes the speed of light so uniquely significant in the fabric of our space and time?",
            "buy" => "//buy.thulasidas.com/unreal",
        ];
        self::$books[] = [
            'name' => "Linear Algebra for Computer Science",
            "url" => "//la4cs.com",
            "tagLine" => "Essential concepts and practices of Linear Algebra for computer science students.",
            "desc" => "A readable and engaging textbook that highlights the elegance and beauty of numerical techniques and algorithms rooted in Linear Algebra. Designed as a practical handbook for computer and data scientists, LA4CS primarily focuses on real-world applications and tractable concepts, avoiding deep, theoretical mathematics.",
            "buy" => "//buy.thulasidas.com/la4cs",
        ];
        self::$books[] = [
            'name' => "Principles of Quantitative Development",
            "url" => "//pqd.thulasidas.com.com",
            "tagLine" => "A practical guide to designing, building and deploying a trading platform.",
            "desc" => "A lucid and succinct expos&eacute; on the trade life cycle and the business groups involved in managing it, bringing together the big picture of how a trade flows through the systems, and the role of a quantitative professional in the organization.",
            "buy" => "//buy.thulasidas.com/pqd",
        ];

        self::$pedagogy = [];
        self::$pedagogy[] = [
            'name' => "Grader",
            "url" => "//ink.library.smu.edu.sg/sis_research/9164/",
            "urlText" => "Published Paper",
            "tagLine" => "A grading assistant application for lab tests.",
            "desc" => "The Grader efficiently manages various logistical aspects of lab tests, including file management, consistent rubric application, and auto-grading with test cases. It's developed using the same PHP-MySQL stack that our students learn in the course for which it is deployed. The zip download also includes several guides:
                <ul>
                <li><code>Grader-Student.pdf</code>: A guide for the WAD students on how Grader is coded.</li>
                <li><code>Grader-QuickStart.pdf</code>: A user guide on how to get started with running the Grader application.</li>
                <li><code>Grader-PublishedPaper.pdf</code>: The published paper (referring to a slightly older version of Grader).</li>
                </ul>",
            "buy" => "assets/zips/Grader.zip",
            "buyTitle" => "Download",
            "buyText" => "<code>Grader.zip</code>",
        ];
        self::$pedagogy[] = [
            'name' => "Secure Answer Book and Automatic Grading",
            "url" => "//ink.library.smu.edu.sg/sis_research/5559/",
            "urlText" => "Published Paper",
            "tagLine" => "A secure answer book and an automated grading tool.",
            "desc" => "A tamper-proof Excel answer book coupled with an automated system for grading large volumes of submissions, particularly beneficial for courses on spreadsheet modeling. In the <code>AnswerBook.zip</code>, there is a <code>README.md</code>, which specifies the various passwords to unlock and look at the innards of <code>AnswerBook.xlsm</code>. <code>Grader-v*.xlsm</code> is self-documented. Hope these files, along with the published paper, are enough to get you started.",
            "buy" => "assets/zips/AnswerBook.zip",
            "buyTitle" => "Download",
            "buyText" => "<code>AnswerBook.zip</code>",
        ];
        self::$pedagogy[] = [
            'name' => "Moderation Tool",
            "url" => "//ink.library.smu.edu.sg/sis_research/6885/",
            "urlText" => "Published Paper",
            "tagLine" => "The strategy and principles behind statistical moderation.",
            "desc" => "An Excel tool for implementing statistical moderation as a final safeguard in standardizing summative assessment scores, enabling comparison across different student cohorts, terms, sections, or schools. The zip download contains a self-documented Excel file <code>Moderation.xlsm</code>. Open it with macros enabled and follow the instructions in its two sheets. Note that <code>Moderation.xlsm</code> is customized to our use at SMU and our learning management system (built on <a href='//www.d2l.com/brightspace/' target='_blank'>Brightspace</a>). It may need further tweaking for your use.",
            "buy" => "assets/zips/Moderation.zip",
            "buyTitle" => "Download",
            "buyText" => "<code>Moderation.zip</code>",
        ];

        self::$wad = [];
        self::$wad[] = [
            'name' => "My Home",
            "url" => "old",
            "urlText" => "Visit Old Homepage",
            "tagLine" => "My first homepages from the mid-1990s.",
            "desc" => "The <a href='old/'>Old Pages from 2000</a> and really <a href='old/old'>Old Pages from the mid-1990s</a> serve as examples of basic HTML.",
            "buy" => "assets/zips/manoj.sg.zip",
            "buyTitle" => "Download",
            "buyText" => "<code>manoj.sg</code> Code",
        ];
        self::$wad[] = [
            'name' => "Other Apps",
            "url" => "//demo.thulasidas.com/",
            "urlText" => "Live Demo Site",
            "tagLine" => "More information and a demo site for my PHP/MySQL apps, including WordPress plugins.",
            "desc" => "Featuring additional tools like an ad server, games, AdSense plugins, and more.",
            "buy" => "//www.thulasidas.com/packages",
            "buyTitle" => "More Info",
            "buyText" => "About my PHP/MySQL apps",
        ];
        self::$wad[] = [
            'name' => "Grader",
            "url" => "//ink.library.smu.edu.sg/sis_research/9164/",
            "urlText" => "Published Paper",
            "tagLine" => "A grading assistant application for lab tests.",
            "desc" => "The Grader efficiently manages various logistical aspects of lab tests, including file management, consistent rubric application, and auto-grading with test cases. It's developed using the same PHP-MySQL stack that our students learn in the course for which it is deployed. The zip download also includes several guides:
                <ul>
                <li><code>Grader-Student.pdf</code>: A guide for the WAD students on how Grader is coded.</li>
                <li><code>Grader-QuickStart.pdf</code>: A user guide on how to get started with running the Grader application.</li>
                <li><code>Grader-PublishedPaper.pdf</code>: The published paper (referring to a slightly older version of Grader).</li>
                </ul>
            PS: This app is also featured in the <strong>Pedagogical Tools</strong> category.",
            "buy" => "assets/zips/Grader.zip",
            "buyTitle" => "Download",
            "buyText" => "<code>Grader.zip</code>",
        ];
        // self::$wad[] = [
        //     'name' => "My Blog",
        //     "url" => "//www.thulasidas.com",
        //     "tagLine" => "Unreal Blog on physics, philosophy, money, life, death, and everything in between.",
        //     "desc" => "A more sophisticated platform, the Unreal Blog has been around for nearly 20 years. During the peak of the blogging era, it was one of my most popular ventures.",
        //     "buyTitle" => "Visit",
        //     "buy" => "//www.thulasidas.com",
        //     "buyText" => "Unreal Blog",
        // ];

        self::$services = [];
        self::$services[] = [
            'name' => "Consultancy",
            "desc" => "Advisory services for starting a new business in Singapore, including assistance with legal and financial requirements, international hiring, and more.",
            "icon" => "briefcase"
        ];
        self::$services[] = [
            'name' => "Software Architecture",
            "desc" => "Consultation and support for the design and development of large-scale software packages, with a focus on web-based workflow automation.",
            "icon" => "card-checklist"
        ];
        self::$services[] = [
            'name' => "Student Motivation",
            "desc" => "Inspiring young learners to love their STEM subjects is my passion. Although I don't do private tutoring, you can try talking me into it.",
            "icon" => "person"
        ];
        self::$services[] = [
            'name' => "Writing Engagements",
            "desc" => "Columns or essays for newspapers, magazines, and books on a wide range of topics—from physics and philosophy to pedagogy, life, death, and everything in between.",
            "icon" => "journal-richtext"
        ];
        self::$services[] = [
            'name' => "Philosophizing",
            "desc" => "Alright, this one's on the cheap side. Take me out for dinner and good wine, and I'll talk at length about life, the universe, and everything. Don't you want to know why the answer is 42?",
            "icon" => "emoji-smile"
        ];
        self::$services[] = [
            'name' => "Anything Else?",
            "desc" => "Some people are born with the unshakable (and often mistaken) belief that they can do anything. I'm one of them. So, what do you need?",
            "icon" => "brightness-high"
        ];
    }

    /**
     * Get an item object for a given category and ID.
     * 
     * @param string $cat Category key (e.g., "app", "books").
     * @param int $id Index of the item in the category.
     * @param string $catName Human-readable category name.
     * 
     * @return Item Populated item object.
     */
    public function getItem($cat, $id, $catName)
    {
        $item = new Item($cat, $id);
        $item->setCatName($catName);
        $details = self::$$cat[$id];
        $item->setName($details['name']);
        $item->setDesc($details['desc']);
        $item->setTagLine($details['tagLine']);
        $item->setUrl($details['url']);
        $item->setBuy($details['buy']);
        if (isset($details['urlText'])) {
            $item->setUrlText($details['urlText']);
        }
        if (isset($details['buyText'])) {
            $item->setBuyText($details['buyText']);
        }
        if (isset($details['buyTitle'])) {
            $item->setBuyTitle($details['buyTitle']);
        }
        return $item;
    }

    /**
     * Get all items across all categories.
     * 
     * @return Item[] List of all item objects.
     */
    public function getItems($catKeys = [])
    {
        $items = [];
        if (empty($catKeys)) { // show all items
            $cats = self::$cats;
        } else { // get the specified categories
            $cats = array_intersect_key(self::$cats, array_flip($catKeys));
        }
        foreach ($cats as $cat => $catName) {
            foreach (self::$$cat as $id => $value) {
                $items[] = $this->getItem($cat, $id, $catName);
            }
        }
        return $items;
    }

    /**
     * Get a specific service object.
     * 
     * @param int $id Index of the service.
     * @return Item Populated service object.
     */
    public function getService($id)
    {
        $service = new Item("services", $id);
        $details = self::$services[$id];
        $service->setName($details['name']);
        $service->setDesc($details['desc']);
        $service->setIcon($details['icon']);
        return $service;
    }

    /**
     * Get all services.
     * 
     * @return Item[] List of all service objects.
     */
    function getServices()
    {
        $services = [];
        foreach (self::$services as $id => $details) {
            $services[] = $this->getService($id);
        }
        return $services;
    }
}
