<?php

namespace App\Controllers;

use JetBrains\PhpStorm\NoReturn;

class Media extends BaseController
{
    public function index(): void
    {

        $target_file = "/var/www/html/SITEFILES/TEST/test.jpg";
        $filename = $target_file;
        $file_extension = strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            // case "mp4": $ctype="video/mp4"; break;
            default:
        }

        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();


    }

    #[NoReturn] public function businessImageGate(): void
    {
        $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $segLen = count($uriSegments);
        //var_dump($uriSegments);

        $filename = $this->imageBizSets($uriSegments[$segLen - 1]);
        $target_file = "/var/www/html/SITEFILES/business/" . $filename;
        $file_extension = strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            // case "mp4": $ctype="video/mp4"; break;
            default:
        }
        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();
    }

    #[NoReturn] public function personalImageGate(): void
    {
        $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $segLen = count($uriSegments);
        //var_dump($uriSegments);

        $filename = $this->webImageSets($uriSegments[$segLen - 1]);
        $target_file = "/var/www/html/SITEFILES/personal/" . $filename;
        $file_extension = strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            // case "mp4": $ctype="video/mp4"; break;
            default:
        }
        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();
    }

    #[NoReturn] public function imageGate(): void
    {
        $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $segLen = count($uriSegments);
        //var_dump($uriSegments);

        $filename = $this->imageSets($uriSegments[$segLen - 1]);
        $target_file = "/var/www/html/SITEFILES/TEST/" . $filename;
        $file_extension = strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            // case "mp4": $ctype="video/mp4"; break;
            default:
        }

        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();
    }

    //
    #[NoReturn] public function WebsiteGate(): void
    {

        $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $segLen = count($uriSegments);
//        var_dump($uriSegments);
//        0 => string '' (length=0)
//        1 => string 'website' (length=7)
//        2 => string '4bdc1594-a0d4-4eba-9dff-9e62f5c28d69' (length=36)
//        3 => string 'coronary-artery-disease.png' (length=27)

        $filename = $uriSegments[$segLen - 1];
        $webFileUid = $uriSegments[$segLen - 2];
        $fileDetailResult = $this->getWebFileDetails($webFileUid);

        if (count($fileDetailResult) == 0) {
            log_message('info', "WEB FILE DATA NOT  FOUND:::UID-> $webFileUid");
            $this->notFound();
            exit();
        }
        $fileDetails = $fileDetailResult[0];
        //var_dump($fileDetails);
        $target_file = $_ENV['UPLOAD_FOLDER'] . "/" . $fileDetails['file_group'] . "/W" . str_pad($fileDetails['member_id'], 10, '0', STR_PAD_LEFT) . "/" . $fileDetails['save_filename'];

        // Check if the file exists
        if (!file_exists($target_file)) {
            log_message('info', "WEB FILE  NOT  FOUND:::UID-> $target_file");
            $this->notFound();
            exit();
        }
        log_message('info', "TARGET:::FILE-> $target_file");
        $file_extension = $fileDetails['file_type']; // strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            default:
        }

        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();
    }

    private function getWebFileDetails($webFileUid): array
    {
        try {
            $sqlP = "SELECT * FROM members_webfiles WHERE uid::text = '$webFileUid' ";
            $query = $this->db->query($sqlP);
            $row = $query->getResultArray();
            if (isset($row)) {
                return $row;
            }
        } catch (Exception $e) {
            // Handle the specific exception
            //echo "Caught exception: " . $e->getMessage() . "\n";
            log_message('error', 'Error in getWebFileDetails :: :: ' . $e->getMessage());
        } finally {
            // Cleanup code, always runs
            log_message('info', "Consider CACHE IN getWebFileDetails - $webFileUid");
        }
        return [];
    }


    private function webImageSets($selImg): string
    {
        $image_set = [
            "p1-home-banner" => "banner-1.png",
            "p1-home-about" => "img-10.png",
            "p2-home-banner" => "hero-2-img.png",
            "p2-home-about" => "img-13.png",
            "p3-home-banner" => "img-18.png",
            "p3-home-about" => "img-05.png",
            "p4-home-banner" => "img-18.png",
            "p4-home-about" => "img-05.png",
            "p5-home-banner" => "hero-11-img.png",
            "p5-home-about" => "img-03.png",
            "p6-home-banner" => "banner-6.png",
            "p6-home-about" => "img-09.png",
        ];
        return $image_set[$selImg];
    }


    private function imageSets($selImg): string
    {
        $image_set = [
            "merms-profile" => "merms-profile.png",
            "test" => "test.jpg",
            "about-kintcare" => "about-kintcare.png",
            "about8-image" => "about8-image.png",
            "about7-image" => "about7-image.png",
            "about6-image1" => "about6-image1.png",
            "about6-image2" => "about6-image2.png",
            "about5-image3" => "about5-image3.png",
            "about5-image2" => "about5-image2.png",
            "about4-image3" => "about4-image3.png",
            "about5-image1" => "about5-image1.png",
            "about3-image1" => "about3-image1.png",
            "about3-image2" => "about3-image2.png",
            "about4-image1" => "about4-image1.png",
            "about10-image" => "about10-image.png",
            "about2-image1" => "about2-image1.png",
            "about2-image2" => "about2-image2.png",
            "about-page-sec1-image1" => "about-page-sec1-image1.png",
            "about-page-sec1-image2" => "about-page-sec1-image2.png",
            "about-choose-sec-image2" => "about-choose-sec-image2.png",
            "about-choose-sec-image3" => "about-choose-sec-image3.png",
            "about-choose-sec-image1" => "about-choose-sec-image1.png",
        ];
        return $image_set[$selImg];
    }


    private function imageBizSets($selImg): string
    {
        $image_set = [
            "merms-profile" => "merms-profile.png",
            "test" => "test.jpg",
            "about-page-main" => "about-page-sec1-image2.png",
            "about8-image" => "about8-image.png",
            "about7-image" => "about7-image.png",
            "about6-image1" => "about6-image1.png",
            "about6-image2" => "about6-image2.png",
            "about5-image3" => "about5-image3.png",
            "about5-image2" => "about5-image2.png",
            "about4-image3" => "about4-image3.png",
            "about5-image1" => "about5-image1.png",
            "about3-image1" => "about3-image1.png",
            "about3-image2" => "about3-image2.png",
            "about4-image1" => "about4-image1.png",
            "about10-image" => "about10-image.png",
            "about2-image1" => "about2-image1.png",
            "about2-image2" => "about2-image2.png",
            "about-page-sec1-image1" => "about-page-sec1-image1.png",
            "about-page-sec1-image2" => "about-page-sec1-image2.png",
            "about-choose-sec-image2" => "about-choose-sec-image2.png",
            "about-choose-sec-image3" => "about-choose-sec-image3.png",
            "about-choose-sec-image1" => "about-choose-sec-image1.png",
        ];
        return $image_set[$selImg];
    }

    private function notFound()
    {
        $ctype = "image/png";
        $target_file = "/var/www/html/SITEFILES/not-found.png";;
        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();
    }
}
