<?php

namespace App\Controllers;

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();


        /*
                if (file_exists($selectedFile)) {
                    log_message('critical', "FILE **************************   --->0002");
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename="'.basename($selectedFile).'"');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize($selectedFile));
                    readfile($selectedFile);
                    exit;
                }
        */

        // echo $target_file;

        // return $target_file;

    }

    public function imageGate()
    {
        $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();
    }

    private function imageSets($selImg)
    {
        $image_set = [
            "test" => "test.jpg",
            "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];
    }

}
